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
using uniq and awk?? amatuer_lee_3 Shell Programming and Scripting 13 05-17-2008 02:10 PM
How to uniq third field in a file babycakes UNIX for Dummies Questions & Answers 1 02-01-2008 03:52 AM
help on UniQ vishal_ranjan HP-UX 0 06-21-2007 11:33 AM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 06:39 AM
sort/uniq jimmyflip UNIX for Dummies Questions & Answers 3 10-17-2002 06:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-15-2008
Digby Digby is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 21
Uniq using only the first field

Hi all,

I have a file that contains a list of codes (shown below).
I want to 'uniq' the file using only the first field. Anyone know an easy way of doing it?

Cheers,

Dave

##### Input File #####
1xr1 1xws 1yxt 1yxu 1yxv 1yxx 2o3p 2o63 2o64 2o65
1xr1 1xws 1yxt 1yxv 1yxx 2o3p 2o63 2o64 2o65
1xr1 1yxt 1yxv 1yxx 2o3p 2o63 2o64 2o65
1y8y 2c68 2c69 2c6i 2c6k 2c6m 2c6o
1y8y 2c69 2c6i 2c6k
1zlt 2br1 2brb 2brn 2bro 2c3k 2cgu 2cgv 2cgx
1zlt 2br1 2brb 2brn 2bro 2cgu 2cgv 2cgx
1zlt 2br1 2brb 2brn 2bro 2cgv 2cgx
1zlt 2br1 2brb 2brn 2bro 2cgx
1zlt 2br1 2brb 2bro

##### Desired Output File #######
1xr1 1xws 1yxt 1yxu 1yxv 1yxx 2o3p 2o63 2o64 2o65
1y8y 2c68 2c69 2c6i 2c6k 2c6m 2c6o
1zlt 2br1 2brb 2brn 2bro 2c3k 2cgu 2cgv 2cgx
  #2 (permalink)  
Old 01-15-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,892
Yes:

Code:
awk '!x[$1]++' filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
  #3 (permalink)  
Old 01-15-2008
Digby Digby is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 21
Thanks for the quick response radoulov.
It works a treat!
  #4 (permalink)  
Old 01-15-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
The neat nawk solution will not always provide the same solution independent of line ordering though...

something like:

Code:
awk '{if ( length($0) > length(a[$1]) ) {a[$1]=$0}} END {for (i in a){print a[i]}}' file
will ensure you get the longest line (if that is a requirement).
  #5 (permalink)  
Old 01-15-2008
smallu smallu is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
Tytalus

Can you pls explain how it works..whats $1 and $0 and how does it work..Thanks a bunch!
  #6 (permalink)  
Old 01-16-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
Quote:
Originally Posted by smallu View Post
Tytalus

Can you pls explain how it works..whats $1 and $0 and how does it work..Thanks a bunch!
Sure.

$1 is the first field. $0 is the entire record. So...

Basic jist is that you run through each record, and add it to an array (using the first field as a reference) IF the length of the record is greater than the current array entry:

Code:
if ( length($0) > length(a[$1]) ){a[$1]=$0}}
Then you just loop through and print out the contents of the array:

Code:
{for (i in a){print a[i]}}'
HTH
  #7 (permalink)  
Old 01-16-2008
ilan ilan is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 101
simply it would be like...

uniq -w 4 filename

note: the catch here is that assumes your first feild would be always 4 :-)

-ilan
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 07:27 AM.


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