LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2013, 12:07 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
deleting selected lines


looking for a one liner that will delete a line if the first term of the line is the same as the first term in a previous line.

Quote:
example;
original list;
xxx / 123
zzz / 123
zzz / 234
www / 123

processed list;
xxx / 123
zzz / 123
www / 123
Because "zzz" is seen in the second line the next occurrence seen in the third line is extracted. The "/" and numbers following the first term are not relevant.

This looks like a problem made for awk or sed but I'm not sure how to make it work.

I prefer a one liner because this problem is part of a bash script. However, beggars can't be choosers so I'm open to any suggestions.

Thanks in advance.
 
Old 09-18-2013, 12:36 PM   #2
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Forgot to mention, it is safe to assume that the original list is sorted alphabetically.
 
Old 09-18-2013, 12:53 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Try this ...
Code:
awk '{if (1==++a[$1]) print}' $InFile >$OutFile
Daniel B. Martin

Last edited by danielbmartin; 09-18-2013 at 01:12 PM. Reason: Tighten the code, slightly
 
Old 09-18-2013, 01:05 PM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by danielbmartin View Post
Try this ...
Code:
awk -F/ '{if (1==++a[$1]) print}' $InFile >$OutFile
Daniel B. Martin
Bammo! That worked like a charm.
 
Old 09-18-2013, 01:20 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Another solution, possibly faster for large input files ...
Code:
awk '{if (prev!=$1) print; prev=$1}' $InFile >$OutFile
Daniel B. Martin
 
Old 09-18-2013, 01:34 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Another solution. I don't like this one because it is unreadable.
Code:
sed -r '$!N; /^(.*\/)(.*)\n\1/!P; D' $InFile >$OutFile
Daniel B. Martin
 
Old 09-22-2013, 05:16 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by danielbmartin View Post
Another solution, possibly faster for large input files ...
Code:
awk '{if (prev!=$1) print; prev=$1}' $InFile >$OutFile
Daniel B. Martin
Can be made slightly shorter:

Code:
awk 'p!=p=$1' $InFile >$OutFile
 
1 members found this post helpful.
Old 09-22-2013, 11:02 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by millgates View Post
Code:
awk 'p!=p=$1' $InFile >$OutFile
Excellent! This wins a prize for being concise.

Daniel B. Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
XEmacs deleting/overwriting selected text hyperriven Linux - Desktop 1 09-29-2011 11:22 AM
deleting duplicate lines without deleting first instance of the duplicated line jkeertir Linux - Newbie 2 02-07-2011 06:55 AM
Hw to clear the selected lines from History? linuxxx123 Linux - Newbie 6 01-08-2009 03:56 PM
deleting the selected extra lines adidassharma Programming 17 10-17-2008 07:10 PM
getting selected lines in a file viveksnv Programming 9 02-28-2008 10:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration