Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Hi All, i wants to removing a space from my string by script.Like i have a=inder, if there is space like a= inder . before and after inder. Then i ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    20

    removing a space

    Hi All,

    i wants to removing a space from my string by script.Like i have a=inder, if there is space like a= inder . before and after inder. Then i wants to remove that space.Please tell me the script how to do this. Thanks ...

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Could you give us a very short but complete example of a script in which you anticipate finding a string with such an unwanted space? This gives us a context in which to help you.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    20
    Let i have a file aa.txt

    $cat aa.txt
    main= in (SPACE AT END AND START OF IN)
    end= cc (SPACE AT END AND START OF CC)

    How can i remove the space after = sign.

    If you wants more specification, please let me know.

    Thanks ...

  4. #4
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Code:
    cat aa.txt | sed "s/= /=/g" > aa_replaced.txt
    EOF

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    cat aa.txt | sed "s/= /=/g" > aa_replaced.txt
    For more information, follow this link for the Useless Use of Cat Award.

    Better:
    Code:
    sed "s/= /=/g" aa.txt > aa_replaced.txt
    You can eliminate multiple spaces thus:
    Code:
    sed "s/= */=/g" aa.txt > aa_replaced.txt
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    Jan 2008
    Posts
    20
    Thanks

  7. #7
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    using bash for not too large input file
    Code:
    while read line
    do
     echo ${line/= /=}
    done < "file"

  8. #8
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by wje_lf View Post
    Code:
    cat aa.txt | sed "s/= /=/g" > aa_replaced.txt
    For more information, follow this link for the Useless Use of Cat Award.
    for this case, i think it doesn't really matter, as performance wise, they are both quite the same. sed takes in standard input also.
    Code:
    # wc -l file
    15830725 file
    # time cat file | sed "s/= /=/g"  > cattest
    
    real    0m47.755s
    user    0m46.723s
    sys     0m1.004s
    # time sed "s/= /=/g" file > sedtest
    
    real    0m48.812s
    user    0m46.343s
    sys     0m0.756s

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Correct. It doesn't matter performancewise. But in general, the simpler the code, the better. The fewer pieces, the better. It makes code easier to maintain. It's this sort of practical esthetics that led Randal Schwartz to hand out awards for useless uses of cat. That's a bit of an ad hominem argument, sure, but he has a very good point, one with which I quite agree.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by wje_lf View Post
    ... uses of cat. That's a bit of an ad hominem argument ...
    I'd call it ad Felis -- couldn't resist ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

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
  •  
...