I would like to print all lines of a file where the second field $2 is abc. Could that be done using awk? If not please let me know what can be used. Thanks.
because first of all, $ doesn't work inside single quotes...
and second, if $ would work inside single quotes, you'd have some trouble getting the $ character in $2 and you'd be comparing $2 to the awk variable with name as specified by $awk ...
so:
wrong: awk '$2==$var' /path/to/file
(amounts to awk '$2==$0' /path/to/file)
wrong: awk "\$2==$var" /path/to/file
(if in shell var="abc", amounts to awk "$2==abc" /path/to/file
which is probably awk "$2==0" /path/to/file)
some solutions:
right: awk "\$2==\"$var\"" /path/to/file (use double quotes, escape special chars where needed)
right: awk '$2=="'$var'"' /path/to/file (use single quotes, but interrupt the single quoted string where needed)
right: awk -v var=$var '$2==var' /path/to/file (use -v to preload an awk variable)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.