shell - How to use awk to filter a complex line? -


i have file syntax: key1 | key2 | key31:value31, key32:value32, ...| key4 want filter lines specific key , value in third section.

for example, in following test, want filter lines age=13, more want user key in $3 filter such key age , age >= 15. get|20150715|age:13,height:11,width:12|mike post|20150715|age:13,width:11|tom get|20150715|height:11,width:11|lily

can use awk this?

quick , dirty way:

 awk -f'|' '$3~/age:13/' file 

it "dirty" because, if old enough, age:135, listed too, if there no age:13 in $3 package:13 line outputted well. one-liner quick shot, can try if works real data.

update new requirement:

since op said has gnu awk available, line should work:

awk -f'|' -v value="12"     '$3~/age:/{v=gensub(".*age:([0-9]*).*","\\1","g",$3);if(v>value)print}' file 
  • you can change -v value="12" value condition want check, e.g. 15, 17....
  • this one-liner output lines age:value (in 3rd column) greater value given
  • if want check smaller, equals... change if(v... value) check.

here test 1 line age value <13:

kent$  cat f get|20150715|age:13,height:11,width:12|mike xxxget|20150715|age:10,height:11,width:12|mike post|20150715|age:13,width:11|tom get|20150715|height:11,width:11|lily  kent$  awk -f'|' -v value="12" '$3~/age:/{v=gensub(".*age:([0-9]*).*","\\1","g",$3);if(v>value)print}' f get|20150715|age:13,height:11,width:12|mike post|20150715|age:13,width:11|tom 

Comments