loveclassic
loveclassic

Reputation: 25

Printing the same contiguous lines only once using shell/awk

I have an input as below:

Sep 9 09:22:11
Hello
Hello
Sep 9 10:23:11
Hello
Hello
Hello
Sep 10 11:23:11

I expect the output as below: (the same contiguous lines are replaced by only one line)

Sep 9 09:22:11
Hello
Sep 9 10:23:11
Hello
Sep 10 11:23:11

Could anyone help me solving this one fast using shell or awk ?

Upvotes: 1

Views: 33

Answers (3)

Paul Hodges
Paul Hodges

Reputation: 15418

Just because you asked for shell too, though the given answers are all better solutions -

last=''
while read line
do if [[ "$line" -eq "$last" ]]
   then continue
   else echo "$line"
        last="$line"
   fi
done < infile

This is simple, clear, and likely slower than either awk or uniq.

Upvotes: 0

John1024
John1024

Reputation: 113934

To remove repeats of lines, use uniq:

uniq File

With your sample input, for example:

$ uniq File
Sep 9 09:22:11
Hello
Sep 9 10:23:11
Hello
Sep 10 11:23:11

Although its name may imply that uniq concerns itself with unique lines, it does not: it looks for adjacent repeated lines and, by default, removes the repeats.

Upvotes: 1

anubhava
anubhava

Reputation: 785731

Using awk you can do this:

awk '$0 != prev; {prev=$0}' file

Sep 9 09:22:11
Hello
Sep 9 10:23:11
Hello
Sep 10 11:23:11

Command Breakup:

$0 != prev;   # if previous line is not same as current then print it
{prev=$0}     # store current line in a variable called prev

Upvotes: 1

Related Questions