Results 1 to 5 of 5

Thread: sed problem

  1. #1
    Join Date
    Oct 2003
    Posts
    14

    Question Unanswered: sed problem

    hi, i am newbie in shell so plz dont flame me

    i am just experimenting with some tutorial
    one of them is replacing words in 'blah.txt' and replace it with a new word and save the changes to blah.txt

    like inside the txt has 'Tom and Jerry are Jerry mouse'
    say i want to replace Jerry to Bob

    i tried using ... sed 's/Jerry/Bob/g' blah.txt .... but that only display the changes and doesn't save the changes, i thought /g means append it .... i tried ... sed 's/Jerry/Bob/g' > blah.txt but not working

    and how do u make it case sensitive like if i want replace erry to Bob and the script wouldnt change anything cause it is erry and not 'Jerry'

    thanx in advance to whoever help me understand more bout sed

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Okay, the 'g' means perform the specified action 'globally', i.e. perform your substitution for every occurence on the input line. Without it you would only see the first instance of 'Jerry' changed to 'Bob'.

    Sed will not edit your file in situ. It will take your file as input, perfom the actions in the sed script and then output the modified input to standard output. You can redirect standard output to a new file and then move your new file to replace the input file to achieve what you require.

    e.g.

    sed 's/Jerry/Bob/g' blah.txt > newFile
    mv newFile blah.txt

    It is advisable (but not necessary) to prevent the 'mv' occurring unless the 'sed' command has executed successfully.

    sed 's/Jerry/Bob/g' blah.txt > newFile && mv newFile blah.txt

    I'm not sure what you mean when you refer to 'erry' but if you're meaning to incorporate a regular expression that incorporates word boundaries, you might have a problem as not all versions of sed support them.

    The word boundary characters are usually \< and \>, so you would represent the word 'erry' as '\<erry\>'.

    Failing that, you could use a regular expression more suited to your specific example. For your purposes, you could say that you are looking for a single space followed by 'erry' (s/ erry/ Bob/g).

    HTH

  3. #3
    Join Date
    Oct 2003
    Posts
    14
    thanx guru ... u make the sky look much more brighter cause u just kill -9 'cloud'

  4. #4
    Join Date
    Oct 2003
    Posts
    14
    guru after more understanding can u give some idea on this one ?
    just say i have a txt that has the below :

    Brad Brad Brad Brad
    John John John
    a aa
    aaa aa
    a
    aa
    aaa

    i want to replace Brad so i used sed 's/Brad/Yes/g
    i want to replace 'a' so i used sed 's/a/Yes/g <--- but this one replace all the character of a in the txt, it should only replace 1 character
    then i tried using sed/^a$/Yes/g <--- this work but when i used it on Brad which mean sed 's/^Brad$/Yes/g, it does nothing at all !!! :|

  5. #5
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    What happened was correct. Using something between ^ and $, you tell sed to pick up all lines which have only that word. So ^Brad$ dint display anything as there is no line which has only Brad in it. Change your input file to :
    Brad Brad Brad Brad
    Brad
    John John John
    a aa
    aaa aa
    a
    aa
    aaa

    and you will understand better.
    Thanks and Regards
    Karthik R

Posting Permissions

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