Reputation: 1019
input file is:
line1 [time-1] abcdef
line2 [time-1] absde1
line3 [time-1] abcdef
line4 [time-1] zzzzzz
this command is working fine:
$ str="abcdef|zzzzzz"
$ awk '!($0~/('"$str"')$/)' test_input
line2 [time-1] absde1
following command fails:
$ str="[time-1] abcdef|[time-1] zzzzzz"
$ echo "${str}"
[time-1] abcdef|[time-1] zzzzzz
$ awk '!($0~/('"$str"')$/)' test_input
awk: fatal: Invalid range end: /([time-1] abcdef|[time-1] zzzzzz)$/
$
Is it possible to pass variable with such string to awk too?
Upvotes: 2
Views: 141
Reputation: 136
You just have to escape the square brackets
$ str="\[time-1\] abcdef|\[time-1\] zzzzzz"
$ awk '!($0~/('"$str"')$/)' test_input
line2 [time-1] absde1
--- Edit --------------------------
To create the str variable with escaped characters
str="[time-1] abcdef|[time-1] zzzzzz" | sed -e 's/[]\/$*.^[]/\\&/g'
This way, the pipe-separated list can be an automatically generated list.
Upvotes: 0
Reputation: 785128
One more awk:
awk -v str="$str" 'BEGIN {
n = split(str, a, "|")
}
{
for (i=1; i<=n; i++)
if (index($0, a[i]))
next
print
}' file
line2 [time-1] absde1
Reason why you cannot just use $str
as regex is that you have regex meta characters such as [
, ]
etc.
Upvotes: 1