Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ':@'` # find the line
input="connection.url=jdbc\racle\:thin\:@$ipnumber\:1521\:billz" # preparing new line
echo `sed "s/'${find}'/'${input}'/g"` # replace old with new line

The problem is: nothing happens.

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

share|improve this question
up vote 6 down vote accepted

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 << 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 for help with editing using ed, and 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\

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"

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

share|improve this answer
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
Thanks ,,,,that's work perfect .... – moata_u Mar 3 '11 at 8:57

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 ":@"`
sed -i "s/"$find"/"$test"/g"

THANKS geirha

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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