bash script to edit a file

Writing by on Sunday, 3 of August , 2008 at 10:48 pm

You can use sed to edit the contents of a file (say, replace ‘old_str’ with ‘new_str’ in the file default.properties and send the result to default.properties.bak).

sed ‘s/old_str/new_str/g’ ./default.properties > ./default.properties.bak

The script below uses the eval command to use variables with sed.

#!/bin/sh

FIND=’old_str’
REPLACE=’new_str’
FILE=./default.properties

eval “sed -e ‘s/$FIND”/”$REPLACE/g’ $FILE > $FILE.bak”
mv -f $FILE.bak $FILE

For more information on sed read sed – An Introduction and Tutorial.

Leave a comment

Category: Linux,Tips and Tricks

No Comments

No comments yet.

Leave a comment

You must be logged in to post a comment.

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.