Results 1 to 10 of 10
Problem: Suppose I have a string like
This is ~~~~~a ~~~~~test ~~~~~string
I want to remove the extra space but keep intact the single space between two words. Like this:
...
- 06-10-2008 #1
Removing extra space form a string
Problem: Suppose I have a string like
I want to remove the extra space but keep intact the single space between two words. Like this:This is ~~~~~a ~~~~~test ~~~~~string
I achieved this using java, but I smell that this is uncool because a linux guru will come with a single line of sed or awk or whatever that exactly do the same thing but in a much smarter way. Any help?This is a test string
EDIT: please read ~ as single space, I couldn't put extra spaces between words as the editor is formatting the text with intelligence which certainly not going to help me in this case.Imran
Linux User #467555 | Debian Squeeze | Intel(R) Core(TM)2 Duo CPU 4500 @ 2.20GHz | Gigabyte GA-G41MT-ES2L
| 2 GB RAM | 320 GB SATA | Kernel: 2.6.32-5-686
- 06-10-2008 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
It's not clear to me where your extra space is but if it's at the end of the line the command
will remove all trailing spaces. Unpicking it it's saying to replace with nothing (the // at the end) any pattern consisting of zero or more spaces (the " *") followed by end of line (the $).Code:sed 's/ *$//'
- 06-10-2008 #3
Oh, so quick a reply. Thanks scm.

As I said, in this editor its not possible to write a string with extra spaces, as it trims that automatically, so you kindly read the first string with ~ replaced with space. Hope this is clear.Imran
Linux User #467555 | Debian Squeeze | Intel(R) Core(TM)2 Duo CPU 4500 @ 2.20GHz | Gigabyte GA-G41MT-ES2L
| 2 GB RAM | 320 GB SATA | Kernel: 2.6.32-5-686
- 06-10-2008 #4
Um, imranka, did you come away from scm's response with a sufficient answer to your question? I couldn't tell one way or the other by reading your response.
But if you need to remove trailing spaces, but also need to replace multiple spaces inside a string with a single space, try this:
and if you don't want to remove trailing spaces, this will suffice:Code:sed -e 's/~~*/~/g' -e 's/~*/$/'
I've adopted your convention of using "~" to mean "space".Code:sed 's/~~*/~/g'
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 06-10-2008 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
- 06-10-2008 #6
Great! Thanks
This is great. Thanks scm and Bill. The first solution scm offered would not work as I was not concerned with trailing space, rather the extra spaces between two characters. So, modifying above sed as(replacing '~' with ' ') :
worked perfectly.sed -e 's/ */ /g' -e 's/ *$//'
Thanks once again.Imran
Linux User #467555 | Debian Squeeze | Intel(R) Core(TM)2 Duo CPU 4500 @ 2.20GHz | Gigabyte GA-G41MT-ES2L
| 2 GB RAM | 320 GB SATA | Kernel: 2.6.32-5-686
- 06-10-2008 #7Never happen. I'm too old to make mistakes.I think you've transposed a couple characters, Bill!--
Bill
Old age and treachery will overcome youth and skill.
- 06-11-2008 #8Linux User
- Join Date
- Aug 2006
- Posts
- 458
Code:# echo "This is a test string" |awk '{$1=$1}1' OFS=" " This is a test string
- 06-11-2008 #9With bash/ksh93 arrays:Code:
$ set -- This is a test string;echo "$@" This is a test string
With tr:Code:s="This is a test string" s=($s) $ echo "$@" This is a test string
With Awk this should be sufficient:Code:$ tr -s \ <<<'This is a test string' This is a test string
Code:$ awk '$1=$1' <<<'This is a test string' This is a test string
- 06-11-2008 #10
Thanks to all,
I must say I'm now overwhelmed with choice
Imran
Linux User #467555 | Debian Squeeze | Intel(R) Core(TM)2 Duo CPU 4500 @ 2.20GHz | Gigabyte GA-G41MT-ES2L
| 2 GB RAM | 320 GB SATA | Kernel: 2.6.32-5-686


Reply With Quote
