Find the answer to your Linux question:
Results 1 to 8 of 8
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Simply question about 'sed'


    surely i'm wrong something
    i need to change some strings in a text file
    trying this on a sedtest.txt file (containing '123')
    Code:
    sed 's/123/abc/g' sedtest.txt
    the sedtest.txt file doesn't change! (i see abc on the screen, but the file still contains 123)
    i know that this is really a simply question, but looking around in the web documentation the command SEEMS ok

    any help is really appreciated

  2. #2
    Linux Newbie
    Join Date
    Jul 2005
    Location
    ~/home
    Posts
    105
    you have to write the changes into a new file:
    Code:
    sed 's/123/abc/g' sedtest.txt > newfile.txt
    Help me getting a Opera licence
    Beginning with debian? -> read THIS!

  3. #3
    sed reads either a file or standard input
    sed writes to standard output

    try this:

    sed 's/123/abc/g' sedtest.tst >sedout.txt

  4. $spacer_open
    $spacer_close
  5. #4
    thx for the really fast reply
    hmm so i cannot modify the source file?
    i'm writing a really simply script.. so for modify the original file i must
    Code:
    sed 's/123/abc/g' sedtest.txt >sedout.txt
    rm sedtest.txt && mv sedout.txt sedtest.txt
    that's true? or there's a better solution?

    another little question
    if i must change a "really complex" string like
    Code:
    ; do rm -R $(flashprefix)/root/
    how can i do it with sed? [/quote]

  6. #5
    Linux Enthusiast scientica's Avatar
    Join Date
    Sep 2003
    Location
    South- or "Mid-" Sweden
    Posts
    742
    Quote Originally Posted by Newbie_69
    thx for the really fast reply
    hmm so i cannot modify the source file?
    i'm writing a really simply script.. so for modify the original file i must
    Code:
    sed 's/123/abc/g' sedtest.txt >sedout.txt
    rm sedtest.txt && mv sedout.txt sedtest.txt
    that's true? or there's a better solution?
    "man 1 sed" should give you the answer, under the "-i[suffix], --in-place[=suffix]
    " part of the description. It should allow you to do:
    Code:
    sed 's/123/abc/g' -i'~' sedtest.txt
    (should make a backup named 'sedtest.txt~' if I read the man page correct - N.B. never tried it though (and can't right now as I don't have access to any linux machine right now ). Also, use "-i" instead of "-i'~'" if you don't want a backup of the original file - just remember to make sure the regexp is "MurphyFree" then.
    And if that doesn't do the trick, then you can simply make those 3 commands 2:
    Code:
    sed 's/123/abc/g' sedtest.txt > sedout.txt
    mv -f sedout.txt sedtest.txt # the -f is probably redundant
    Quote Originally Posted by Newbie_69
    another little question
    if i must change a "really complex" string like
    Code:
    ; do rm -R $(flashprefix)/root/
    how can i do it with sed?
    not sure what you mean by that I must admit...
    Regards Scienitca (registered user #335819 - http://counter.li.org )
    --
    A master is nothing more than a student who knows something of which he can teach to other students.

  7. #6
    thx again
    Code:
    sed 's/abc/def/g' -i sedtest.txt
    is simply perfect for me, i don't need a backup

    for the other question i wanna change some strings in a configure.ac file that contains / and/or " (eg: ; do rm -R $(flashprefix)/root/) i dunno like insert they in a sed command

  8. #7
    Linux Enthusiast scientica's Avatar
    Join Date
    Sep 2003
    Location
    South- or "Mid-" Sweden
    Posts
    742
    Quote Originally Posted by Newbie_69
    thx again
    Code:
    sed 's/abc/def/g' -i sedtest.txt
    is simply perfect for me, i don't need a backup

    for the other question i wanna change some strings in a configure.ac file that contains / and/or " (eg: ; do rm -R $(flashprefix)/root/) i dunno like insert they in a sed command
    aha, well you can use more or less any character for delimiter (the '/' chars sorrounding the regexp and replacement) - well iirc only '\' is dissallowed as delimiter, so all these should do the same:
    Code:
    echo '123' | sed 's/123/abc/'
    echo '123' | sed 's:123:abc:'
    echo '123' | sed 's%123%abc%'
    it's quite common to use '%' or ':' instead of '/' when the pattern and/or replacement contains '/', you can use s/patt/repl/ but the you must escape the '/' using '\/'.
    eg, 's:root/:foo/bar/path:g' should replace 'root/' with 'foo/bar/path'.

    you can also have sed lines like this:
    Code:
    sed s/'A "B" C'/"foo 'bar' $enviroment_var"/g
    #which could be written/escaped as:
    sed s/A\ \"B\"\ C/foo\ \'bar\'\ $enviroment_var/g
    # or
    sed "s/A \"B\" C/foo 'bar' $enviroment_var/g"
    # or
    sed 's/A "B" C/foo \'bar\' '$enviroment_var'/g' # afaik this should work
    # or... (I think you get the picture of how one can escape and mix things to look more or less like the IOCCC ("The International Obfuscated C Code Contest"))
    uhm... that might seem a bit messy, and that's because it is--- if you got an example line in the .ac file I might have time to give an example on how to do it (or if you feel brave, try to formulate the regexp and try it and ask why it doesn't if it fails (actually that would be the best, as you'd probably be learning more from trying rather than me shoving a solution, which makes no sence but somehow works for that single line, down your throat ))

    if you're not a shell-addicted-mainiac (like me) the following paragraph can safely be ignored as it's more or less a "escaing strings is cool and fun "-speach for madmen like me...
    I'd recoment reading up on how strings works in bash (or you favourite shell (read: bash :P)), try search for "strings" in the docs/man pages for your shell and play with 'echo' and trying all sorts of wild escaping-guesses is a good starting point ('man echo' or 'info echo' should give some examples/explanations of some escapes, like \a \b, \e (I'd just like to point out that combinations with \e can render "strange" results - like the terminal going black, text with all sorts of colors apearing here and there, text not showing up at all - that's because \e is a little special, '\e[...m' escape codeas are ANSI codes which allows for some text transformation, like changing color and cursor position - if you're new to that I'd suggest searching for "ANSI escape codes" first as that should shed some light on the magic behind \e) - other than that most escape codes should be completly harmless (to my knowing), you might have to close the terminal window and start a new one if you for instance find the escape code to make typed characters 'disappear', some times pressing ^C a few times and then 'reset' and striking enter will 'restore' the terminal window)
    Regards Scienitca (registered user #335819 - http://counter.li.org )
    --
    A master is nothing more than a student who knows something of which he can teach to other students.

  9. #8
    i'm sorry for these apparently simple questions
    until yesterday evening i didn't have big problem looking in the various documentation, not so with this sed command
    thx for all yours tips: all seems ok now
    have a nice day

Posting Permissions

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