Prafull
Prafull

Reputation: 17

match mutiple patterns using awk command

My file looks like

10.183.227.46|242066391737|73633662;244809|com.com|com.com|2001|CCA-I|0|[29/Dec/2016:00:00:40]|26|RULE_31893406,RULE_31893405,RULE_416241598|4106,4105,4000|2006,2005,5000|0|0|0|0|2621440|3000|-|-|1003:0,1013:0,1010:Home|244809|0|117,115,40|-|-|

I want to see files which contains 117 in 24th field and 2001 in 6th field

I am using

awk -F "|" '{if($6==2001 && $24==117)print }' 29_DEC_2016.1

but as 24th field can contain more than 1 value seperated by comma I am not getting the correct result

Upvotes: 0

Views: 78

Answers (3)

Claes Wikner
Claes Wikner

Reputation: 1517

awk -F'[|,]' '{print $6,$(NF-5)}'  file

2001 117

Upvotes: 0

James Brown
James Brown

Reputation: 37394

$ awk -F\| '$6=="2001" && $25 ~ /(^|,)117($|,)/' file

$6 equals "2001" (do not use just 2001 because in case you were searching for 0, $6==0 would fail the implicit print) and $25 includes exactly 117 (preceeded and followed by start-of-string ^ or (|) end-of-string or comma , (you could throw in space just in case)).

Testing the latter part:

$ cat foo
117,2,3   # good
1,117,3   # good
1,2,117   # good
1117,2,3  # bad
1,1117,3  # bad
1,2,1177  # bad
$ awk '$1~/(^|,)117($|,)/' foo
117,2,3   # good
1,117,3   # good
1,2,117   # good

Upvotes: 3

Inian
Inian

Reputation: 85530

Or use GNU awk, split function on the column with , de-limiter to extract the words and do a check on that value

awk -F "|" '{split($25,array1,","); if ( $6 == "2001" && array1[1] == "117" ){print} }' file

If the element can occur anywhere in the column, just a ~ regex match would be sufficient.

awk -F "|" '$6 == "2001" && $25 ~ /117/' file

Refer this James Brown's answer for even more rigorous regEx match.

Upvotes: 2

Related Questions