The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help removing lines with duplicated columns yahyaaa Shell Programming and Scripting 14 05-17-2008 04:33 AM
Remove lines, Sorted with Time based columns using AWK & SORT karthikn7974 Shell Programming and Scripting 1 05-09-2008 08:04 PM
How to remove duplicate records with out sort svenkatareddy SUN Solaris 2 02-28-2008 04:38 AM
remove duplicated columns kamel.seg Shell Programming and Scripting 6 02-21-2008 03:36 AM
remove duplicated xml record in a file under unix happyv Shell Programming and Scripting 8 09-20-2006 10:36 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-2007
Registered User
 

Join Date: Aug 2007
Posts: 40
Stumble this Post!
remove duplicated lines without sort

Hi
Just wondering whether or not I can remove duplicated lines without sort

For example, I use the command who, which shows users who are logging on. In some cases, it shows duplicated lines of users who are logging on more than one terminal.

Normally, I would do

who | cut -d" " -f1 | sort |uniq

But I realised that I do not want data to be sorted.

Is there any other ways to do it?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-20-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Stumble this Post!
Are the duplicated lines adjacent? If not which one do you remove?
Reply With Quote
  #3 (permalink)  
Old 08-20-2007
Registered User
 

Join Date: Jun 2007
Posts: 377
Stumble this Post!
hi

Try follow one, use awk array to remember the appearance of the line:

Code:
awk '{
 if (arr[$0]=="")
 arr[$0]=1
 }
 END{
 for (i in arr)
 print i
 }' filename
Reply With Quote
  #4 (permalink)  
Old 08-20-2007
Registered User
 

Join Date: Aug 2007
Posts: 40
Stumble this Post!
I am new to awk or you can say UNIX. Honestly, I do not understand awk at the moment.
I will try to study it.
Thank you for your reply anyway.

Duplicated lines are not adjacent and I can remove any of them.

Cheers,

Last edited by lalelle; 08-20-2007 at 07:06 PM. Reason: wrong spelling
Reply With Quote
  #5 (permalink)  
Old 08-20-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,536
Stumble this Post!
Code:
$ who
oracle     pts/1        Aug 21 04:57    (...)
oracle     pts/4        Aug 21 04:57    (...)
$ who|awk '!x[$1]++'
oracle     pts/1        Aug 21 04:57    (...)

If you want only the first field to be displayed:

Code:
who|awk '!x[$1]++{print $1}'

Use nawk on Solaris.
Reply With Quote
  #6 (permalink)  
Old 08-21-2007
Registered User
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
Stumble this Post!
Code:
perl -ne '$H{$_}++ or print'
remove duplicate without sorting
Reply With Quote
  #7 (permalink)  
Old 08-21-2007
Registered User
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
Stumble this Post!
Code:
 who | perl -ane '$H{$F[1]}++ or print'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:42 AM.


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

Content Relevant URLs by vBSEO 3.2.0