Find the answer to your Linux question:
Results 1 to 3 of 3
I have following line hello world world hello world I am able to delete one space using following sed 's/[ \t]//g' and more than one space beginning of the line ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    19

    How to delete one/more space using sed

    I have following line

    hello world world hello world

    I am able to delete one space using following

    sed 's/[ \t]//g'

    and more than one space beginning of the line

    sed 's/[ \t]*//g'

    but when I doing this for replacing one or more space ":" in line

    sed 's/[ \t]*//g'

    getting following output

    h:e:l:l:o:w:o:r:l:d:w:o:r:l:d:h:e:l:l:o

    instead of
    hello:world:world:hello:world:

    please suggest

    thanks

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    I'm pretty sure you posted the second code snippet twice.

    * means match "none or more" so your pattern will happily match nothing and insert a colon.

    You'd want to use a + to indicate "one or more"

    Code:
    echo "hello world world hello world" | sed 's/[ \t]\+/:/g'
    hello:world:world:hello:world
    If this doesn't work for you, remove the \ before the +. For some reason I had to escape it when I'm pretty sure I shouldn't need to.

    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    working with fields is much easier
    Code:
    # echo "hello world world hello world" | awk '{$1=$1}1' OFS=":"
    hello:world:world:hello:world

Posting Permissions

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