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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2009
sam_2921 sam_2921 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 13
Smile remove duplicate words in a line

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
  #2 (permalink)  
Old 03-18-2009
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321

Code:
awk '{ while(++i<=NF) printf (!a[$i]++) ? $i FS : ""; i=split("",a); print "" }' file

  #3 (permalink)  
Old 03-18-2009
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252

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

  #4 (permalink)  
Old 03-19-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
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

  #5 (permalink)  
Old 03-19-2009
sam_2921 sam_2921 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 13
Thanks summer_cherry, ShawnMilo and Rubin.

The nawk and Python codes are running perfect,

but Rubin the awk one liner is giving the error " a[: Event not found. " can u please guide why this error is coming?

Thanks again.
Sam
  #6 (permalink)  
Old 03-19-2009
sam_2921 sam_2921 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 13
Thanks summer_cherry, ShawnMilo and Rubin.

The nawk and Python codes are running perfect,

but Rubin the awk one liner is giving the error " a[: Event not found. " can u please guide why this error is coming?

Thanks again.
Sam
  #7 (permalink)  
Old 03-19-2009
rubin's Avatar
rubin rubin is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2007
Posts: 321
Quote:
Originally Posted by sam_2921 View Post
...but Rubin the awk one liner is giving the error " a[: Event not found. " can u please guide why this error is coming?...
I cannot reproduce the same error, obviously use nawk or /usr/xpg4/bin/awk on Solaris. The code works fine either on Solaris or Linux with no error messages.
HTH.
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 06:56 AM.


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