Find the answer to your Linux question:
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: ...
  1. #1
    Linux Newbie imranka's Avatar
    Join Date
    Dec 2007
    Location
    Kolkata
    Posts
    177

    Removing extra space form a string

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

    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

  2. #2
    scm
    scm is offline
    Linux 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
    Code:
    sed 's/ *$//'
    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 $).

  3. #3
    Linux Newbie imranka's Avatar
    Join Date
    Dec 2007
    Location
    Kolkata
    Posts
    177
    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

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    Code:
    sed -e 's/~~*/~/g' -e 's/~*/$/'
    and if you don't want to remove trailing spaces, this will suffice:
    Code:
    sed 's/~~*/~/g'
    I've adopted your convention of using "~" to mean "space".

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by wje_lf View Post
    But if you need to remove trailing spaces, but also need to replace multiple spaces inside a string with a single space, try this:
    Code:
    sed -e 's/~~*/~/g' -e 's/~*/$/'
    I think you've transposed a couple characters, Bill!
    Code:
    sed -e 's/~~*/~/g' -e 's/~*$//'

  6. #6
    Linux Newbie imranka's Avatar
    Join Date
    Dec 2007
    Location
    Kolkata
    Posts
    177

    Thumbs up Great! Thanks

    Quote Originally Posted by scm View Post
    I think you've transposed a couple characters, Bill!
    Code:
    sed -e 's/~~*/~/g' -e 's/~*$//'
    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 ' ') :
    sed -e 's/ */ /g' -e 's/ *$//'
    worked perfectly.
    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

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I think you've transposed a couple characters, Bill!
    Never happen. I'm too old to make mistakes.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    # echo "This is      a     test  string" |awk '{$1=$1}1' OFS=" "
    This is a test string

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    $ set --  This is      a     test  string;echo "$@"
    This is a test string
    With bash/ksh93 arrays:

    Code:
    s="This is      a     test  string"
    s=($s)
    $ echo "$@"
    This is a test string
    With tr:

    Code:
    $ tr -s \  <<<'This is      a     test  string'
    This is a test string
    With Awk this should be sufficient:

    Code:
    $ awk '$1=$1' <<<'This is      a     test  string'
    This is a test string

  10. #10
    Linux Newbie imranka's Avatar
    Join Date
    Dec 2007
    Location
    Kolkata
    Posts
    177
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...