If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > sed problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-03, 06:17
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
Question 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
Reply With Quote
  #2 (permalink)  
Old 10-07-03, 09:20
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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
Reply With Quote
  #3 (permalink)  
Old 10-07-03, 22:30
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
thanx guru ... u make the sky look much more brighter cause u just kill -9 'cloud'
Reply With Quote
  #4 (permalink)  
Old 10-13-03, 09:37
jinroh jinroh is offline
Registered User
 
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 !!! :|
Reply With Quote
  #5 (permalink)  
Old 10-17-03, 04:52
karthi_tvr karthi_tvr is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On