简体   繁体   中英

Elegant way of using AWK

I have a file that has field separated by multiple characters. For Ex:

abc sometext def;ghi=123; 
abc sometext def;ghi=123; 
abc sometext def;ghi=123; 

Now I want to parse the file in AWK to extract the fields. for example to get all the values of 'ghi',

awk '{print $3}' | awk 'BEGIN {FS = "="} { print $NF }' inputFile.txt

Is there any way to parse the file in one shot instead of using multiple pipes and AWK commands.

Yes, you can use the split function in awk

awk '{split($3,a,"=");print a[2]}'
123;
123;
123;

This divides filed nr 3 using = as separator in to an array a , then print second value of array a[2]


If there are variation of fields in filed number 3 and you like the last, do like this:

awk '{n=split($3,a,"=");print a[n]}'
123;
123;
123;

In your case, this will do too:

awk -F= '{print $NF}'

This can also be accomplished using multiple field separators in awk :

$ awk -F"[=;]" '{print $3}' file
123
123
123

This tells awk to use field separators = or ; . Based on that, the numbers you want are in the 3rd position.

If you expect the ghi part to be changeable and important, you can also use grep with a look-behind:

$ grep -Po '(?<=ghi=)\d+' file
123
123
123

This will print all digits after ghi= .

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