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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove duplicate lines in log files karthikn7974 Shell Programming and Scripting 4 03-21-2009 06:41 PM
Remove Duplicate lines from File Nysif Steve UNIX for Dummies Questions & Answers 18 09-09-2007 08:57 AM
how to remove duplicate lines fredao Shell Programming and Scripting 3 12-13-2006 12:51 PM
Remove Duplicate Lines in File Teh Tiack Ein Shell Programming and Scripting 5 01-12-2006 08:30 AM
reconstructing a record in a diffrent order r1500 UNIX for Dummies Questions & Answers 1 10-16-2003 05:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-02-2008
abhi.roy03 abhi.roy03 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 1
How to remove duplicate lines of a record without changing the order

Hi all,
I have to remove duplicate lines in a file without chainging the order.for eg if i have a record

pqr
def
abc
lmn
pqr
abc
mkh
hgf


the output should be
pqr
def
abc
lmn
mkh
hgf

Plz help me.It is urgent

Abhishek
  #2 (permalink)  
Old 08-02-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
Is this a homework?
Try searching the forums.
  #3 (permalink)  
Old 08-02-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
Code:
#!/usr/bin/perl -w

while (<STDIN>)
{
        push (@lines, $_);
}

print "-\n";

foreach my $i (@lines)
{
        if (scalar (grep { /$i/ } @lines) == 1)
        {
                print $i;
        }
}
Usage:
Code:
Tsunami repeated_lines # perl repeat.pl 
pqr
def
abc
lmn
pqr
abc
mkh
hgf
-
def
lmn
mkh
hgf
Tsunami repeated_lines #
To stop input, just hit Ctrl-D and the script will give you all the non-repeated strings in the input order.

You can also do something like:

Code:
Tsunami repeated_lines # cat lines |perl repeat.pl 
-
def
lmn
mkh
hgf
Tsunami repeated_lines # cat lines 
pqr
def
abc
lmn
pqr
abc
mkh
hgf
Tsunami repeated_lines #
  #4 (permalink)  
Old 08-02-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
Using awk ...
Code:
$ awk '! a[$0]++' file
pqr
def
abc
lmn
mkh
hgf
$
  #5 (permalink)  
Old 08-02-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
As I said,
you just need to search the forums to find the following solutions:


Code:
awk '!_[$0]++' input
Code:
perl -ne'print unless $_{$_}++' input
or:

Code:
perl -ne'$_{$_}++||print' input
  #6 (permalink)  
Old 08-03-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If the requirement is to keep the last instead of the first occurrence, it's only marginally harder.

Code:
perl -ne '$n{$_} = $.; END { print sort { $n{$a} <=> $n{$b} } keys %n }'
If the last line lacks a newline, that will count as a unique line. It's not terribly hard to fix, but I didn't want to complicate the script.
  #7 (permalink)  
Old 08-03-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
Quote:
If the requirement is to keep the last instead of the first occurrence
[...]
With GNU Awk:

Code:
awk 'END { for (k in r) t[sprintf("%10d", r[k])] = k
  n = asorti(t, _)
  while (++i <= n) print t[_[i]] }
{ r[$0] = NR }' filename
Sponsored Links
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 10:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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