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
Help removing lines with duplicated columns yahyaaa Shell Programming and Scripting 14 05-17-2008 07:33 AM
Remove lines, Sorted with Time based columns using AWK & SORT karthikn7974 Shell Programming and Scripting 1 05-09-2008 11:04 PM
How to remove duplicate records with out sort svenkatareddy SUN Solaris 2 02-28-2008 08:38 AM
remove duplicated columns kamel.seg Shell Programming and Scripting 6 02-21-2008 07:36 AM
remove duplicated xml record in a file under unix happyv Shell Programming and Scripting 8 09-20-2006 01:36 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 08-20-2007
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
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?
  #2 (permalink)  
Old 08-20-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Are the duplicated lines adjacent? If not which one do you remove?
  #3 (permalink)  
Old 08-20-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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
  #4 (permalink)  
Old 08-20-2007
lalelle lalelle is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 43
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 10:06 PM.. Reason: wrong spelling
  #5 (permalink)  
Old 08-21-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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.
  #6 (permalink)  
Old 08-21-2007
bigearsbilly bigearsbilly is offline
Registered User
  
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
Code:
perl -ne '$H{$_}++ or print'
remove duplicate without sorting
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 12: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