Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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}'

share|improve this question

2 Answers 2

up vote 5 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.

#!/bin/bash

read -p "Enter the ip address: " ipnumber

ed -s application.properties << EOF
/:@/c
connection.url=jdbc:oracle:thin:@$ipnumber:1521:billz
.
w
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.

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.

# POSIX
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.

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 :

#!/bin/bash

read -p "Please Enter The ip address : " ipnum 
find=`grep ":@" application.properties`
test="connection.url=jdbc:oracle:thin:@$ipnum:1521:e"
sed -i "s/"$find"/"$test"/g" application.properties

THANKS geirha


share|improve this answer
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

 
discard

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.