-1

This is a file . I want to remove the repetition in the name of patch

[ppande@server-1 —]$egrep 'Patch[0-9].*.*:' content1
Patch1001 : snmp fixl.org
Patch1002 : dhcp tmp fix
Patch1003 : qemu-img-9.0.58
Patch001 : snmp fixl.org
Patch002 : dhcp installation
Patch003 : qemu
Patch004 : snmp fixl.org

I used 'sort -u' but here the order of the patch is changed . All I need is the output with out repetitions and order remains same , or in other words if there is a repetition the second/last occurrence must not be displayed .

[ppande@server-1 —]$egrep 'Patch[0-9].*.*:' content1 | sort -u -k3
Patch002 : dhcp installation
Patch1002 : dhcp tmp fix
Patch003 : qemu
Patch1003 : qemu-img-0.0.58
Patch1001 : snmp fixl.org
Patch001 : snmp fixl.org

Desired output:

Patch1001 : snmp fixl.org
Patch1002 : dhcp tmp fix
Patch1003 : qemu-img-9.0.58
Patch002 : dhcp installation
Patch003 : qemu
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Unixquest945
  • 103
  • 2
  • 10

2 Answers2

1

You can do that in a single awk command.

awk -F ':\\s*' '/^Patch[0-9]+\s*:/ && !a[$2]++' content1
oguz ismail
  • 1
  • 16
  • 47
  • 69
1

EDIT: Since oguzismail added same solution few secs before me so adding perl solution now if you are ok with it.

perl -aF': ' -lne 'print if ! $seen{$F[1]}++'  Input_file


Could you please try following. You need not to use multiple commands along with awk here.

awk -F': '  '/Patch[0-9].*.*/ && !a[$2]++'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Ravinder , this Perl oneliner wont work as I tried it make more noise of unwanted lines those that have a string followed by colon . The lines with 'Patch[[:digit:]]:' is fetched but few more lines with ':' is also displayed . – Unixquest945 Dec 17 '18 at 09:09