This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Use of SED in shell scripts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Use of SED in shell scripts" Watch "Use of SED in shell scripts" New topic
Author

Use of SED in shell scripts

Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

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.


Regards, Ricky
Oracle Weblogic 10g Certified Expert TechPaste.Com
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

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

Joined: Mar 27, 2003
Posts: 836
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

Joined: Aug 23, 2008
Posts: 254

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

Joined: Aug 22, 2006
Posts: 257

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


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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 ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Ramakanta Sahoo
Ranch Hand

Joined: Aug 23, 2008
Posts: 254

Thanks Andrew

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

Cheers!!
Ricky
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Use of SED in shell scripts
 
Similar Threads
Command to add lines to multiple files
Script to Find & Remove text in multiple files.
SED regular expressions for simple replace. ...
using sed to chnage data in file
Run search/replace on all files