简体   繁体   中英

how to use awk to match the complete word

I have this script which takes the value of variable $userId as input & uses it in below awk command.

gzip -cd "input.csv.gz"|/usr/xpg4/bin/awk -v search="$userId" -F, 'BEGIN{ OFS=","} { if( match($4, search)) print $0 }' >>$outputFileNameUser

format of input.csv.gz is like below:

gzip -cd input.csv.gz|head -4
Circle,Date,Time,SubscriberId,OperatorId,VoucherNumber,Status
UPE,01-JUN-15,20:23:39,9936596081,,1161504025632821,Used
UPE,01-JUN-15,20:23:39,7755802655,,1161504038349788,Used
UPE,01-JUN-15,20:23:39,9793948511,,1161504027670339,Used

This awk command is matching 4th field of input file (ie SubscriberId) with variable $UserId. In the first row value of $4 is 9936596081. So even if I provide "6596" (which is a part of 9936596081 ) as $UserId it will match the first row. I want to match the complete number(9936596081) not any part of the number.. I tried like this..

gzip -cd "${outputFileName}"|/usr/xpg4/bin/awk -v search="$userId" -F, 'BEGIN{ OFS=","} { if( $4 ==  "search")) print $0 }' >>$outputFileNameUser

but didn't work.. could you please help me on this?

试试这样:

awk -v search="$userId" -F, '$4==search'

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