The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 05:27 AM
Unix file operations(shell script) nivas Shell Programming and Scripting 6 02-07-2008 07:11 AM
command for a series of operations phiber_optik Shell Programming and Scripting 1 12-21-2007 06:57 AM
String Operations Rohini Vijay Shell Programming and Scripting 9 04-21-2006 11:32 AM
File operations chiragmistry21 Shell Programming and Scripting 2 03-27-2006 05:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2006
monks monks is offline
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!
  #2 (permalink)  
Old 04-25-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
try:
Code:
sort -u -t=| -k1.1,1.8 filename
  #3 (permalink)  
Old 04-26-2006
thestevew thestevew is offline
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0