Below awk
command removes all duplicate lines as explained here:
awk '!seen[$0]++'
If the text contains empty lines, all but one empty line will be deleted.
How can I keep all empty lines whilst deleting all non-empty duplicate lines, using only awk
?
Please, also include a brief explanation.
'!($0 in a) {if (NF) a[$0]; print}'
. - more readably:!($0 in a) {if (NF) {a[$0]}; print}'
. By ensuring the$0
is only stored if the line is non-empty,($0 in a)
will always be false for empty lines. – AdminBee May 24 '22 at 11:16