0

Hey guys I dont know if this can be done with shell? There is a script with 200 lines and Im just interested in changing:

 ...
 161. subject = subject.force_encoding('binary')
 162. body = msg.force_encoding('binary')
 163. 
 164. smtp.send_mail(<<EOS, @from, @to.split(/,/))
 165. Date: #{Time::now.strftime("%a, %d %b %Y %X")}
 ...

For:

 161. subject = subject.force_encoding('binary')
 162. body = msg.force_encoding('binary')
 163. converted_time = Time.now.utc
 164. smtp.send_mail(<<EOS, @from, @to.split(/,/))
 165. Date: #{converted_time.strftime("%a, %d %b %Y %X")}
 ....

Is that possible using shell? I know how to add content at the end or change the file with new content using > and >> but I dont know if modifying the file in this way is possible. If not, I'll just use a perl script i reckon.

(the numbers at the beginning of the code are not code, just for reference, its the number of the line)

2
  • You cannot do much with plain shell. Bear in mind that in bash you cannot even copy a file (well maybe it is possible with some magic, but no-one does it normally). You need to use cp which is part of GNU tool-set. It would probably be helpful if you specified system. Windows also has a shell. Commented Jul 17, 2013 at 9:45
  • I'm on Linux (CentOS) Commented Jul 17, 2013 at 9:53

1 Answer 1

2

You can use this syntax to replace the whole content of line 165 with <new content>:

sed "165s/.*/<new content>/g" file

In your case, and if I see it properly, you want to add content to line 163 and replace the content in line 165. So this will make the trick:

$ line163='converted_time = Time.now.utc'
$ line165='Date: #{converted_time.strftime("%a, %d %b %Y %X")}'
$
$ sed -e "3s/.*/$line163/g" -e "5s/.*/$line165/g" file
subject = subject.force_encoding('binary')
body = msg.force_encoding('binary')
converted_time = Time.now.utc
smtp.send_mail(<<EOS, @from, @to.split(/,/))
Date: #{converted_time.strftime("%a, %d %b %Y %X")}

Note I used line 3 and 5 in my case. I store the text to be used in variables and then use the sed "s/content/$variable/g" expression. The -e is used to do more than one different sed actions at the same time.

To make the changes permanent, add the -i flag:

sed -i -e ... file

will update the file with the new content. It is good to create a backup before that, that can be easily done with:

sed -i.bak -e ... file

It will update file with new content and the backup file will be created with name file.bak (or whatever extension you give to i).

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! when I run this program the corrected script appears as a "cat" in my terminal screen but when I enter the script once more, the changes are gone...
Good point @adriancdperu, see my updated answer referring the -i option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.