Lets say I have the following file:
$ cat test.txt
a
-----
b
-----
-----
c
-----
-----
-----
d
-----
e
-----
-----
Now I want to remove all the -----
, but only if they're repeating after each other. So the result should look like this:
a
-----
b
-----
c
-----
d
-----
e
-----
I tried grep -Pvz -- "-----\n-----"
, but that didn't work.
That's exactly what the
uniq
command is made for:So
Alternatively, you can use this sed one-liner 69. Delete duplicate, consecutive lines from a file (emulates "uniq") from Sed One-Liners Explained, Part III: Selective Deletion of Certain Lines and Special Applications
which might be preferred if you want to edit
test.txt
in place (by adding the-i
or--in-place
option).just do
uniq filename.txt
As the name states only unique lines are left and the repetitions are merged