简体   繁体   中英

Extract number from first line of a file in linux

I have a file which has contents like the below

SPEC.2.ATTRID=REVISION&
SPEC.2.VALUE=5&
SPEC.3.ATTRID=NUM&
SPEC.3.VALUE=VS&

I am using the below command to extract only the numbers from the first line. Is this way efficient or you guys think of an alternate way ?

cat ticketspecdata | tr -d " " | tr -s "[:alpha:]" "~" | tr -d "[=.=]" | cut -d "~" -f2

Using :

$ grep -om1 '[0-9]\+' file
2

Or

head -n1 file | tr -cd '[:digit:]'

You may also want to read about UUOC:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM