Shell – want the last line when duplicate awk

awkshelltext processing

I have the file:

key value

blah blah
blah blah
blahblah
man1 boy1
blah blah
man1 boy2
man1 boy1

I do this to remove duplicate lines:

awk '/man1/ { print $1,$2} ' file | awk '!x[$0]++'

and the command take the first line and ignore other lines

man1 boy1 
man1 boy2

but I want to ignore all lines except the last line:

man1 boy2 
man1 boy1

as ramesh said I want something like:

cat filename
blah blah
blah blah
blahblah
man1 boy1
blah blah
man1 boy2
man1 boy1
man1 boy2
man1 boy3
man1 boy4
man1 boy2

the desired output

man1 boy1
man1 boy3
man1 boy4
man1 boy2

Best Answer

you can do this using this shell script:

#!/bin/bash
awk '/man1/{pos[$0] = NR}
END {
  for(key in pos) reverse[pos[key]] = key
  for(nr=1;nr<=NR;nr++)
    if(nr in reverse) print reverse[nr]
}' yourfile

Output:

[root@host ~]# sh shell.sh
man1 boy1
man1 boy3
man1 boy4
man1 boy2

Source

Related Question