Since bash
is complaining (and not awk
) and you have single quotes around your !
the problem is obvious: you are exiting the command block of awk
.
awk '{$1=$1} /^#/ || !seen[$0]++' file
I.e. do the operation, then the checks. Disadvantage: Will remove/reduce spaces in comments, too, however not remove such duplicates. Avoid that by first buffering the line:
awk '{a=$0 ; $1=$1} /^#/ || !seen[$0]++ {print a}' file
Input:
#comment
duplicate line
#comment
duplicate line
not duplicate
not duplicate 2
duplicate line
#comment2
#comment2
#comment 3
#comment 3
Output (first code)
#comment
duplicate line
#comment
not duplicate
not duplicate 2
#comment2
#comment2
#comment 3
#comment 3
Output (second code)
#comment
duplicate line
#comment
not duplicate
not duplicate 2
#comment2
#comment2
#comment 3
#comment 3
duplicate a
with one space, andduplicate a
with two spaces. – Stephen Kitt Mar 04 '21 at 14:13