Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To remove a duplicate word from a string

Status
Not open for further replies.

123jk

Programmer
Mar 22, 2001
7
0
0
IN

hi,

I need to remove duplicate word and also invalid data from the string

for eg, x="1 2 3.4 5 6 1 3"
the o/p should be
1 2 5 6 3

removing all the duplicates and invalid data( valid data - numbers no floating values)

How to do this..

Thanks,
jaya
 
This would be quite easy using perl:
Code:
#!/usr/bin/perl -w

%seen = ();
foreach( @ARGV ) {
    next unless /^\d+$/;
    push @num, $_ unless $seen{$_}++;
}
print "@num\n";
Used like:
Code:
./test.pl 1 2 3.4 5 6 1 3
Returns:
Code:
1 2 5 6 3
Cheers, Neil
 
Hi,

Thanks for the response.

I need this to be implemented through shell script.
I think it can be implemented easily in 'sed'. but i don't have much experience in using sed.

Thanks,
jaya
 
echo $x | nawk '
{ for (i=1; i <= NF ; i++) $i=(match($i, /\./) ? &quot;&quot; : $i); print}
'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top