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
print line before column 1 transitions ajp7701 Shell Programming and Scripting 2 04-17-2008 11:05 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
Can we use 'tr' command to print 5th column of output of 'ls -l' Nidhi2177 Shell Programming and Scripting 4 09-17-2007 06:53 AM
to print column using awk cdfd123 Shell Programming and Scripting 2 07-26-2007 01:15 PM
can awk print column using a variable ?? jambesh Shell Programming and Scripting 36 09-26-2006 07:39 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 03-17-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Print row if value in column 1 is the first occurence

Hi All,

I would like to have a script which is able to perform the below.
Print the whole row if column1 which is "0001" for the below example is the first occurrence. Subsequent "0001" occurrence will not be printed out and so on.

Can any expert help ?

Input:

0001 k= 40
0001 k= 2
0002 k= 1
0003 k= 1
0004 k= 77
0004 k= 1
0005 k= 88
0005 k= 6


Output:

0001 k= 40
0002 k= 1
0003 k= 1
0004 k= 77
0005 k= 88
  #2 (permalink)  
Old 03-17-2008
agn agn is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 172
Code:
$ cat buf                                                                                                                                                               
0001 k= 40
0001 k= 2
0002 k= 1
0003 k= 1
0004 k= 77
0004 k= 1
0005 k= 88
0005 k= 6
$ perl -n -e '($num) = split /=/; next if $found[$num]; print; $found[$num] = 1' buf  
0001 k= 40
0002 k= 1
0003 k= 1
0004 k= 77
0005 k= 88
  #3 (permalink)  
Old 03-17-2008
Shivdatta Shivdatta is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 73
$cat test
0001 k= 40
0001 k= 2
0002 k= 1
0003 k= 1
0004 k= 77
0004 k= 1
0005 k= 88
0005 k= 6



for i in `cat test|cut -d" " -f1`
do
grep "$i" test | head -1 >> out.txt
done
sort -u out.txt

0001 k= 40
0002 k= 1
0003 k= 1
0004 k= 77
0005 k= 88
  #4 (permalink)  
Old 03-17-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,924
And the Python approach

Code:
#!/usr/local/bin/python

keys = {}

input = file('test')
for line in input:
   key = line.split(' ', 1)[0]
   if key not in keys:
      keys[key] = 1
      print line,
  #5 (permalink)  
Old 03-17-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,860
Like a FAQ
Awk:

Code:
awk '!x[$1]++' file
Use nawk or /usr/xpg4/bin/awk on Solaris.

Perl:

Code:
perl -ane'print unless $x{$F[0]}++' file
  #6 (permalink)  
Old 03-17-2008
agn agn is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 172
Brilliant, radoulov!
  #7 (permalink)  
Old 03-17-2008
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Quote:
Originally Posted by radoulov View Post
Like a FAQ
Awk:

Code:
awk '!x[$1]++' file
Use nawk or /usr/xpg4/bin/awk on Solaris.

Perl:

Code:
perl -ane'print unless $x{$F[0]}++' file
Hi radoulov,

The perl code seems to work but not the awk.
Can you help ? I am using solaris by the way.

Also, can you explain your perl code so that i can understand better. What is the function of "-ane"

Code:
$ nawk '!x[$1]++' file
x[$1]++': Event not found
$ awk '!x[$1]++' file
x[$1]++': Event not found
$ /usr/xpg4/bin/awk '!x[$1]++' file
x[$1]++': Event not found

Last edited by Raynon; 03-17-2008 at 09:12 PM..
Closed Thread

Bookmarks

Tags
solaris

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:42 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