|
Home > Archive > Unix Shell > February 2006 > how to remove redunt word from space seperated word string
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to remove redunt word from space seperated word string
|
|
| linq936@hotmail.com 2006-02-22, 2:48 am |
| Hi,
The string I have is space seperated word string like " a a b b b c",
I want to trim it so that each word only shows up once, "a b c".
uniq command deals with lines, so I run sed to replace the space with
\n, and then run uniq. Here is my command,
echo $my_string | sed -e "s/ /\n/g" | uniq > tmp.file
The problem is how to restore the newline seperated string into space
seperated. sed does not recognize \n as search string.
Or any other way to do this?
| |
| Chris F.A. Johnson 2006-02-22, 2:48 am |
| On 2006-02-22, linq936@hotmail.com wrote:
> Hi,
> The string I have is space seperated word string like " a a b b b c",
> I want to trim it so that each word only shows up once, "a b c".
>
> uniq command deals with lines, so I run sed to replace the space with
> \n, and then run uniq. Here is my command,
> echo $my_string | sed -e "s/ /\n/g" | uniq > tmp.file
>
> The problem is how to restore the newline seperated string into space
> seperated. sed does not recognize \n as search string.
>
> Or any other way to do this?
printf "%s\n" $my_string | uniq | { tr '\012' ' '; echo; } > tmp.file
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
| |
| William James 2006-02-22, 2:48 am |
| linq936@hotmail.com wrote:
> Hi,
> The string I have is space seperated word string like " a a b b b c",
> I want to trim it so that each word only shows up once, "a b c".
>
> uniq command deals with lines, so I run sed to replace the space with
> \n, and then run uniq. Here is my command,
> echo $my_string | sed -e "s/ /\n/g" | uniq > tmp.file
>
> The problem is how to restore the newline seperated string into space
> seperated. sed does not recognize \n as search string.
>
> Or any other way to do this?
echo $my_string | ruby -ne 'puts split.uniq.join(" ")'
| |
| Stephane CHAZELAS 2006-02-22, 2:48 am |
| 2006-02-21, 20:38(-08), linq936@hotmail.com:
> Hi,
> The string I have is space seperated word string like " a a b b b c",
> I want to trim it so that each word only shows up once, "a b c".
>
> uniq command deals with lines, so I run sed to replace the space with
> \n, and then run uniq. Here is my command,
> echo $my_string | sed -e "s/ /\n/g" | uniq > tmp.file
>
> The problem is how to restore the newline seperated string into space
> seperated. sed does not recognize \n as search string.
[...]
printf '%s\n' "$my_string" |
tr -s '[:blank:]' '[\n*]' |
sort -u |
paste -sd ' ' -
--
Stéphane
| |
| Stephane CHAZELAS 2006-02-22, 2:48 am |
| 2006-02-21, 23:47(-05), Chris F.A. Johnson:
> On 2006-02-22, linq936@hotmail.com wrote:
>
> printf "%s\n" $my_string | uniq | { tr '\012' ' '; echo; } > tmp.file
set -f
unset IFS
printf "%s\n" $my_string | uniq | paste -sd ' ' -
No need to do filename generation on the expansion of $my_string
here. You need to have the default value of $IFS.
--
Stéphane
|
|
|
|
|