I'm having a problem using the sed command . I'm trying to write a bash script that does the following :

  1. search for the line that contain :@
  2. then save the line that contained :@ and replace it with new line

as in the following:

#! /bin/bash

echo "Please enter the ip address of you file"
read ipnumber
find=`grep ':@' application.properties` # find the line
input="connection.url=jdbc\racle\:thin\:@$ipnumber\:1521\:billz" # preparing new line
echo `sed "s/'${find}'/'${input}'/g" application.properties` # replace old with new line

The problem is: nothing happens.

I've already tried to use "${find}" instead of '${find}'


There are several problems with that snippet. I recommend you learn bash's syntax a little better before writing scripts in it.

Anyway, if you want to edit a file, you want a file editor. sed is not a file editor. I believe this will do what you want.


read -p "Enter the ip address: " ipnumber

ed -s application.properties << EOF

What that ed script says is: /:@/c - on the first line containing :@, change the line with everything following until a line containing only a .. Then w to write the changes to the file.

See http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed for help with editing using ed, and http://mywiki.wooledge.org/BashGuide to learn bash.

sed can be used to achieve the same, but it can't edit files, it can only be used to create a new file.

sed "/:@/c\
connection.url=jdbc:oracle:thin:@$ipnumber:1521:billz" application.properties

Here sed will output the file with the changes applied. You can redirect to a new file, then move that file over the old one. GNU sed also has a non-standard -i option that does the last part transparently. GNU sed also doesn't require you to have that newline in the sed script.

# GNU sed
sed -i "/:@/c connection.url=jdbc:oracle:thin:@$ipnumber:1521:billz" application.properties

Note that the ed will only replace the first line that matches the regex while sed will replace all lines matching that regex.

  • you are genius.....Thanks man Thanks , is there way to use sed ?? – moata_u Mar 3 '11 at 8:20
  • @moata_u yes, I've updated the answer to also explain how to do it with sed. – geirha Mar 3 '11 at 8:35

my problem was : i was using an variable name (value) ,, which is reserved :))
Solution is :


read -p "Please Enter The ip address : " ipnum 
find=`grep ":@" application.properties`
sed -i "s/"$find"/"$test"/g" application.properties

THANKS geirha

  • 1
    You were not using any reserved variable names in your initial code, you simply had the shell and sed syntax wrong. Again I recommend reading the bash guide I linked to in my answer. You are still using quotes wrongly, and it may fail to work if the find variable contains whitespace, glob syntax, regex syntax or slashes. – geirha Mar 3 '11 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.