Prasanna
Prasanna

Reputation: 11

compare subsequent lines in a file using awk

I have a following below entry in the file

UK99_08
UK99_08
UK99_08
UK99_08
UK99_08
UK99_17
UK99_17
UK99_17
UK99_17
UK99_17
UK99_19
UK99_19
UK99_17
UK99_17
UK99_17
UK99_20
UK99_17
UK99_17
UK99_17

I need to display the first entry of the duplicate and rest of the lines that matches the first entry should have NULL, should look like this using awk

UK99_08



UK99_17



UK99_19

UK99_17


UK99_20
UK99_17

,

Upvotes: 1

Views: 71

Answers (2)

James Brown
James Brown

Reputation: 37394

In awk:

$ awk '{print ($0==p?"":$0); p=$0}' file

Explained:

{
    print ( $0==p ? "" : $0 )  # if current record is the same as p print "", else $0
    p=$0                       # store current record to p 
                               # for comparing on next iteration
}

Upvotes: 4

fzd
fzd

Reputation: 845

while read line
do
  if [ $line = $previousLine ]
  then
    echo;
  else 
    echo $line
    previousLine=$line
  fi
done < my_file

Upvotes: -2

Related Questions