I'm having a problem using the sed
command . I'm trying to write a bash script that does the following :
- search for the line that contain
:@
- 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.
What that ed script says is:
/:@/c
- on the first line containing:@
, change the line with everything following until a line containing only a.
. Thenw
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.
Update:
sed can be used to achieve the same, but it can't edit files, it can only be used to create a new file.
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.Note that the ed will only replace the first line that matches the regex while sed will replace all lines matching that regex.
my problem was : i was using an variable name (value) ,, which is reserved :))
Solution is :
THANKS geirha