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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search for all the unique file extension riverside Shell Programming and Scripting 1 04-09-2008 04:47 PM
Need to find only unique values for a given tag across the files sudheshnaiyer UNIX for Dummies Questions & Answers 8 09-03-2007 12:53 AM
to retrieve unique values mahalakshmi Shell Programming and Scripting 3 02-05-2007 09:30 AM
sorting file and unique commnad.. amon Shell Programming and Scripting 2 02-16-2006 05:19 AM
Unique cell vaules in a file rahulrathod Shell Programming and Scripting 3 01-17-2006 07:42 AM

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 10-14-2008
Legend986 Legend986 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 171
Getting Unique values in a file

Hi,

I have a file like this:

Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321

I would like to find only the unique values in the files and get the following output:

Some_String_Here 123 321 3432 3221 557 886

I am trying to get this done using awk. Can someone please point me towards the right direction?

Edit: Ahaa... I figured out one solution:

Code:
{
        for(i=1;i<=NF;i++) {
                uni[$i]++;
        }
        for(i in uni) {
                print uni[i], i;
        }

}
Posting it so that someone else might find use of it...

Last edited by Legend986; 10-14-2008 at 01:34 PM..
  #2 (permalink)  
Old 10-14-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
{
for(i=1;i<=NF;i++) {
uni[$i]++;
}
for(i in uni) {
print uni[i], i;
}

}
Printing only the unique values
Code:
{
        for(i=1;i<=NF;i++) {
                uni[$i]++;
        }
        
        for(i in uni) {
                if ( uni[i] == 1 ) {
                 print i;
                }
        }
}
  #3 (permalink)  
Old 10-14-2008
Legend986 Legend986 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 171
Ah... thanks for the pointer But again, if I am printing out just the keys, wouldn't that be same as your solution? Just for clarification though...
  #4 (permalink)  
Old 10-14-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

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

Code:
$ print Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321|
awk 'END{printf "\n"}!_[$1]++' ORS=" " RS=" " 
Some_String_Here 123 321 3432 3221 557 886
With Perl:

Code:
$ print Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321|
perl -lane'print join" ",grep!$_{$_}++,@F'
Some_String_Here 123 321 3432 3221 557 886
  #5 (permalink)  
Old 10-14-2008
Legend986 Legend986 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 171
Thank You so much...
  #6 (permalink)  
Old 10-15-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
nawk '{
for(i=1;i<=NF;i++)
	arr[$i]=1
}
END{
for(i in arr)
	printf("%s ",i)
print ""
}' filename
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 11:51 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