How to Remove Duplicate Lines Ignoring Tabs or Spaces

awkperlsedtext processing

I want to remove duplicate lines from /etc/fstab, so I did this:

 awk '!NF || !seen[$0]++'   /etc/fstab > /etc/fstab.update

UUID=3de0d101-fba7-4d89-b038-58fe07295d96 /grid/sdb ext4 defaults,noatime 0 0
UUID=683ed0b3-51fe-4dc4-975e-d56c0bbaf0bc /grid/sdc ext4 defaults,noatime 0 0
UUID=1cf79946-0ba6-4cd8-baca-80c0a2693de1 /grid/sdd ext4 defaults,noatime 0 0
UUID=fa9cc6e8-4df8-4330-9144-ede46b94c49e /grid/sde ext4 defaults,noatime 0 0
UUID=3de0d101-fba7-4d89-b038-58fe07295d96   /grid/sdb                      ext4 defaults,noatime 0 0
UUID=683ed0b3-51fe-4dc4-975e-d56c0bbaf0bc   /grid/sdc                      ext4 defaults,noatime 0 0

But as we can see, the last two lines are the same with the first two lines, but last two lines are with spaces.

Is it possible to ignore the space and remove the duplicate lines anyway?

Best Answer

Force the rebuild of the record with $1=$1! This squeezes all contiguous spaces into a single one.

awk '{$1=$1};!NF||!seen[$0]++'
Related Question