The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix File operations nivas Shell Programming and Scripting 14 02-11-2008 01:27 AM
Unix file operations(shell script) nivas Shell Programming and Scripting 6 02-07-2008 03:11 AM
command for a series of operations phiber_optik Shell Programming and Scripting 1 12-21-2007 02:57 AM
String Operations Rohini Vijay Shell Programming and Scripting 9 04-21-2006 08:32 AM
File operations chiragmistry21 Shell Programming and Scripting 2 03-27-2006 02:00 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2006
Registered User
 

Join Date: Apr 2006
Posts: 1
Exclamation File operations

Hi
I have a tab delimited file with 3 fields. I need to sort this file on the first field and remove all the records where the first field has dulplicates. For eg my file is

133|arrfdfdg|sdfdsg
234|asfsdgfs|aasdfs
133|affbfsde|dgfg

When this file gets sorted I need the result to be
234|asfsdgfs|aasdfs

So if there are duplicate entries in the first column, all those records should be removed. How can I do this in unix? I am able to sort it to get single records based on unique first field using

sort -u -k 1,1 filename

but this is not what I am looking for. Any help will be appreciated!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-25-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,861
try:
Code:
sort -u -t=| -k1.1,1.8 filename
Reply With Quote
  #3 (permalink)  
Old 04-26-2006
Registered User
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
To just print non-repeated lines in the file turns out to be a bit of a pain. The uniq command would do it but only if the key is on the right hand side of the line, so I've put it there.

The code sorts the file on the first column (delimited by pipes) then appends the key to the end of the line uses and uses uniq to remove line with non-repeated keys before stripping off the added key!

Looks a bit klunky - I'm sure that someone could do something more elegant

Code:
Code
sort -t'|' -k1,1 < yourfile.txt | while read x
do
  print $x ${x%%'|'*}
done | uniq -f1 -u | cut -d' ' -f1
cheers
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0