Sunday, April 28, 2024
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 952  / 1 Year ago, wed, march 8, 2023, 3:38:23

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 -- "-----
-----"
, but that didn't work.


More From » command-line

 Answers
4

That's exactly what the uniq command is made for:


NAME
uniq - report or omit repeated lines

SYNOPSIS
uniq [OPTION]... [INPUT [OUTPUT]]

DESCRIPTION
Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT
(or standard output).

With no options, matching lines are merged to the first occurrence.

So


$ uniq test.txt 
a
-----
b
-----
c
-----
d
-----
e
-----

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


sed '$!N; /^(.*)
1$/!P; D' test.txt

which might be preferred if you want to edit test.txt in place (by adding the -i or --in-place option).


[#1290] Friday, March 10, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
coffekne

Total Points: 114
Total Questions: 122
Total Answers: 126

Location: Mauritania
Member since Sun, Oct 17, 2021
3 Years ago
coffekne questions
Thu, Apr 14, 22, 00:46, 2 Years ago
Thu, Mar 23, 23, 12:05, 1 Year ago
Sun, Jun 26, 22, 13:07, 2 Years ago
;