regex - awk lines not match -
using awk
, need avoid print lines specific word in specific field... before call it's duplicate, did search, , found this: match string not containg phrase, think it's other context, because here need match in specific column.
given data:
abc abc def aaa ghi abc
i want filter print full lines text not containing abc in fist field(lines 2 , 3).
as first try, tried (?!...)
, expected work way return def
, ghi
:
echo -e "abc\tabc\ndef\taaa\nghi\tabc" | awk '$1 ~ /(?!abc)/ {print}'
but return nothing.
second try... using not before expression !/.../
:
~$ echo -e "abc\tabc\ndef\taaa\nghi\tabc" | awk '$1 ~ !/abc/ {print}'
return nothing too.
i don't understand why...
so shot fails (as expected time):
~$ echo -e "abc\tabc\ndef\taaa\nghi\tabc" | awk '!/abc/ {print}' def aaa
as expected time, second field filtered too. so, seems work $0
.
then did homework , got working way:
~$ echo -e "abc\tabc\ndef\taaa\nghi\tabc" | awk '{fl=$0;$0=$1}; !/abc/ {print fl}; { $0=fl }' def aaa ghi abc
anyway, looks workaround me, , have bad feeling i'm not doing in right way.
someone know better way, or @ least explain why first , second tries not worked ?
you can use following idiom:
awk '$1 !~ /abc/' file
if first field not contain abc
line gets printed. (the matches not operator !~
). note command contains condition , no print
action. can work since default action in awk
print
, therefore not need specified explicitly.
Comments
Post a Comment