Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing duplicate lines depending on value of $1 2

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
I have a file that looks like this:
Code:
apple4 yellow
apple1 red
apple6 blue
apple3 green
grape red
grape purple
apple68 yellow
apple32 red
apple98 orange
grape yellow

I would like to remove duplicate lines using the following:
Code:
awk '!x[$2]++'

However I only want to remove duplicates if $1 equals grape.

Desired output:
Code:
apple4 yellow
apple1 red
apple6 blue
apple3 green
[s]grape red[/s]
grape purple
apple68 yellow
apple32 red
apple98 orange
[s]grape yellow[/s]

Not sure where to go from there..

The log is if $1 equals "grape" and I have seen $2 before then don't print.



 
Hi

Based on your sample data, this seems to be what you are looking for :
Code:
awk '[teal]![/teal]x[teal][[/teal][navy]$2[/navy][teal]]++[/teal] [teal]||[/teal] [navy]$1[/navy][teal]!=[/teal][green][i]"grape"[/i][/green]' /input/file[left][/left]

Feherke.
[link feherke.github.com/][/url]
 
Thanks for your suggestions..

I ended up just filtering out the "grapes" into a 2nd file. Then doing something like

awk 'NR==FNR && !x[$4,$5]++' wanted_info.txt print_this_if_not_in_first_file.txt

Thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top