Reputation: 290025
In Print lines in file from the match line until end of file I learnt that you can use 0
in range expressions in awk
to match until the end of the file:
awk '/matched/,0' file
However, today I started playing around this 0
and I found that this also works:
awk '/matched/,/!./' file
I guess 0
is working because in the range expression /regex1/,/regex2/
the lines are printed until the regex2
evaluates as True. And since 0
evaluates as False, this never happens and it ends up printing all the lines. Is this right?
However, with /!./
I would imagine this to work until a line without any character is found, like if we said awk '/matched/,!NF'
. But it does not: instead, it always evaluates to False. What is the reasons for this?
$ cat a
1
matched
2
3
4
end
Both awk '/matched/,/!./' a
and awk '/matched/,0' a
return:
matched
2
3
4
end
Whereas awk '/matched/,!NF' a
returns:
matched 2
Upvotes: 5
Views: 3993
Reputation: 785491
You have exclamation at wrong place. Keep it outside the regex like this to negate the match:
awk '/matched/,!/./' a
matched 2
Your regex /!./
is matching a literal !
followed by any character.
If your file is this:
cat a
1
matched
2
3
!4
end
Then
awk '/matched/,/!./' a
matched
2
3
!4
And:
awk '/matched/,!/./' a
matched 2
Upvotes: 4