• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of SED in shell scripts

 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to search a perticular text in a text file and if found then replace the whole line with given replacement text..But i need the replacement should go and paste at exactly same line which got deleted.
and if not seached text not found then append it like >> file.

sed -e 's/Listen/Listen 9060/g' httpd.conf > hi.conf

I know sed command can do it but dont know how to properly implement it.
I have a script if i get a proper command i will post it to show the full purpose.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite clear what you want. Can we have an example line and the output you expect? Either one of the following probably do the job:where the first replaces the entire line containing Listen with Listen 9060, and the second replaces all instances of Listen in a single line with Listen 9060 (which is what you already had). It might not be quite right since I don't know what you actually need. Does that help?
[ October 10, 2008: Message edited by: Charles Lyons ]
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ,Its good.
I did a small change..

sed -re 's/^Listen.*$/Listen 9060/' httpd.conf > httpd.txt

But now the problem is there are 7 to 10 changes to do like above and i want to get all replacement texts, which needs to get replaced when some search text found from a different file let say it variable.properties.

and any way i need to send the output to a file but with same name.
So here i cant do the same.
sed -re 's/^Listen.*$/Listen 9060/' httpd.conf > httpd.conf

So what am thinking is in script i will do a backup file say httpd.conf.bkp first and then i will run the sed command like below with multiple changes.

sed -re 's/^Listen.*$/Listen 9060/' -re 's/^DocumentRoot.*$/DocumentRoot "/opt/Myapp/' httpd.bkp > httpd.conf

Is this gonna work ??

And dont have much idea how i will be able to source replace text using a different file.

Ricky


[ October 10, 2008: Message edited by: Ricky Boxon ]
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand, but I think you'll be able to do what you want using sed and some Bash scripting to bridge between the two files. You're correct that you can't pipe out to the same file as the input. So a copy is good to start with (a simple "cp" will do in your script).

If it requires more scripting than sed can provide, awk can be good. Otherwise you'll get much better performance writing a C/C++ program to do the work (sed gets very slow very quickly on large files due to its buffering, and awk is interpreted so you get that performance penalty).
Is this gonna work ??
We can help you but can't provide all the answers; if you try it, you'll find out...
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i did nt mean it.
I was talking about my idea(taking bkup)...gonna work or not. Not the command.


Thanks.
[ October 10, 2008: Message edited by: Ricky Boxon ]
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are sed variants that allow in-place edits. But like Charles suggested, you should write sed's output to a temporary file, review if you got your sed RE & command line right and the changes it did are okay, and only then overwrite your original file.

What exactly is in this 'variable.properties' file? Will the replacement text change for every substitution, or does this 'variable.properties' file drive the replacement text for all the replacements?

- Anand
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11941
211
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ricky Boxon:
So what am thinking is in script i will do a backup file say httpd.conf.bkp first ...

Excellent idea. If this is being scripted, you could consider going a step further and adding a date/time stamp into the backup filename so you can go back to a particular version at any time.

Originally posted by Ricky Boxon:
...and then i will run the sed command like below with multiple changes.

sed -re 's/^Listen.*$/Listen 9060/' -re 's/^DocumentRoot.*$/DocumentRoot "/opt/Myapp/' httpd.bkp > httpd.conf

I don't know if you can have multiple expressions in one line like this - I would suspect that even if it is a GNU extension to sed, it might not be portable to Solaris. You may have to experiment with it and see where you end up.

An alternative (more useful if you are scripting it) is to have the commands on multiple lines:


Note that this coding style of using indents etc., is a personal coding preference - I get to see which lines are sub commands of sed, and what they are acting on.

Regards, Andrew
[ October 11, 2008: Message edited by: Andrew Monkhouse ]
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew

Once I finish my script i'll get back to you.

Cheers!!
Ricky
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic