I need help with understanding awk search feature.
Here I am trying to search by 4th field in this file:
$ cat xx
471013,A20,asd,100,FEATURE UNKNOWN
171014,A22,sdf,101,FEATURE WITH SUB-ELEMENT
172822,20X,xcv,102,SUB-ELEMENT
101102,22X,asd,103,CODED
When I try the search feature I get nothing:
$ awk -F, '/$4 ~ "101"/{print}' xx
$
When I just use IF statement it works as expected:
$ awk -F, '{ if($4 == 101)print; }' xx
171014,A22,sdf,101,FEATURE WITH SUB-ELEMENT
$
Please help me understand what do I do wrong in using the search // syntax
Thanks
Hi
This way you won't succeed too
awk -F, '{ if(/$4 ~ "101"/)print; }' xx
RudiC
3
In awk
, /.../
is (sort of) a regex constant. man awk
:
So, your expression /$4 ~ "101"/
will try to match exactly this string : $4 ~ "101"
- which it won't find in your sample file.
Try
awk -F, '$4 ~ /101/' xx
which might do exactly what you targeted for.
2 Likes