Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
a small question - how can we remove spaces within a file without making use another file?...
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    16

    remove spaces

    a small question - how can we remove spaces within a file without making use another file?

  2. #2
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    [dir@host] $ cat av
    giga g hhh
    hira moti giga gi gaa giga fi fa

    [dir@host] $ sed "s/[ \t][ \t]*//g" av|tee av
    gigaghhh
    hiramotigigagigaagigafifa

    [dir@host] $ cat av
    gigaghhh
    hiramotigigagigaagigafifa

    [dir@host] $
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  3. #3
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    To just remove multiple spaces...not single space... just replace/substitute [ \t][ \t] with a single space ..i.e.

    sed "s/[ \t][ \t]*/ /g" av|tee av

    a space added...^ (within / and /)
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    If you really want to remove all space characters from a file in situ
    Code:
    sed -i "s/ *//g" filename
    will do it.

  5. #5
    Just Joined!
    Join Date
    Jun 2006
    Posts
    16
    the tee is printing out the file at the output. Do I need to put >/dev/null or am i doing wrong

    sed "s/[ \t][ \t]*//g" av|tee av
    -i option is not available to me

    sed -i "s/ *//g" filename

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    sometimes you just have to be practical.
    Code:
    sed "s/[ \t][ \t]*//g" av > temp
    mv temp original_file_name
    what's wrong with that?
    sed with the -i option will do the same thing at the background for you.

  7. #7
    Just Joined!
    Join Date
    Jun 2006
    Posts
    16
    sometimes you just have to be practical.

    Code:
    sed "s/[ \t][ \t]*//g" av > temp
    mv temp original_file_name
    Didnt wanted to use an additional file

    what's wrong with that?
    sed with the -i option will do the same thing at the background for you
    -i option for sed is not supported . May be old version and needs to be upgraded

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you do not have -i option, then no go for your version of sed. Even if you have the latest sed with -i option, it is also going to create an "invisible" temp file for you. so what's the big problem ? just use whatever you have to get the job done.

  9. #9
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You could use tr:

    Code:
    echo $(cat temp | tr -d "\ ") > temp
    Where temp is the name of the file
    Linux User #453176

  10. #10
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Kieren View Post
    You could use tr:

    Code:
    echo $() > temp
    Where temp is the name of the file
    you don't need extra echo and cat(UUOC) and the escape
    Code:
    tr -d " "< temp > outfile
    also, usually, people don't cat a file and redirect to the same file.

Page 1 of 2 1 2 LastLast

Posting Permissions

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