![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
| How can I remove those duplicate sequence in UNIX?What command line I should type? | patrick chia | Shell Programming and Scripting | 4 | 01-23-2009 01:18 AM |
| remove first few words from a line | shellscripter | Shell Programming and Scripting | 4 | 10-08-2008 02:34 AM |
| Remove duplicate entry in one line | kharen11 | UNIX for Dummies Questions & Answers | 5 | 07-05-2007 03:56 PM |
| Identify duplicate words in a line using command | srinivasan_85 | UNIX for Dummies Questions & Answers | 8 | 05-01-2007 02:29 AM |
| Remove Duplicate line | Student37 | UNIX for Dummies Questions & Answers | 1 | 02-22-2005 03:00 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Hi,
Please help! I have a file having duplicate words in some line and I want to remove the duplicate words. The order of the words in the output file doesn't matter. INPUT_FILE pink_kite red_pen ball pink_kite ball yellow_flower white no white no cloud nine_pen pink cloud pink nine_pen brown_ball white red_bear green red_bear white no OUTPUTFILE pink_kite red_pen ball yellow_flower white no cloud nine_pen pink brown_ball white red_bear green white no Your help is highly appreciated. Thanks in advance ![]() Last edited by sam_2921; 03-18-2009 at 06:05 AM.. Reason: formatting |
|
||||
|
Code:
#!/usr/bin/env python
for line in open('temp.txt', 'r'):
seen = []
words = line.rstrip('\n').split()
for word in words:
if not word in seen:
print word,
seen.append(word)
print
Output: Code:
# cat temp.txt pink_kite red_pen ball pink_kite ball yellow_flower white no white no cloud nine_pen pink cloud pink nine_pen brown_ball white red_bear green red_bear white no # python temp.py pink_kite red_pen ball yellow_flower white no cloud nine_pen pink brown_ball white red_bear green white no |
|
||||
|
hi perl shoudl be easy. But you may try below awk Code:
nawk '
function re_dup(arr,n)
{
for(i=1;i<num;i++){
for(j=i+1;j<=num;j++){
if (arr[i]==arr[j])
arr[j]=""
}
}
}
{
num=split($0,arr," ")
re_dup(arr,num)
for(i=1;i<=num;i++){
if(arr[i]!="")
printf("%s ",arr[i])
}
printf "\n"
}' filename
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|