Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script - how to extract from line

Tags:

shell

unix

awk

Hi please help me to find only numbers. My file have only one line data as below:

53-Brand|5556-Color Family|10984-Fit|10313-Combo

Looking for output 53, 5556, 10984, 10313

Thanks


I tried

awk -F',' '{print $2}' /cat_formula       > 1
    53-Brand|5556-Color Family|10984-Fit|10313-Combo

awk -F'|' '{print $1}{print $2}{print $3}{print $4}' 1 >2
    53-Brand
5556-Color Family
10984-Fit
10313-Combo


awk -F'-' '{print $1}' 2
    53
5556
10984
10313

But looking in one command line.

like image 763
Amit Avatar asked Feb 01 '26 12:02

Amit


1 Answers

grep -oP "\d+" filename

Output:

53
5556
10984
10313

brief explanation:

-P : tells it's a perl regexp
\d+: to match just numbers
-o : to capture just matched numbers

like image 69
Rahul Verma Avatar answered Feb 04 '26 02:02

Rahul Verma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!