![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
Code:
#!/usr/bin/perl -w
while (<STDIN>)
{
push (@lines, $_);
}
print "-\n";
foreach my $i (@lines)
{
if (scalar (grep { /$i/ } @lines) == 1)
{
print $i;
}
}
Code:
Tsunami repeated_lines # perl repeat.pl pqr def abc lmn pqr abc mkh hgf - def lmn mkh hgf Tsunami repeated_lines # 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 # |
|
||||
|
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 }'
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|