To Replace a line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To Replace a line in a file
# 1  
Old 04-19-2014
To Replace a line in a file

Hi Folks,

I am trying to replace large string(jdbc url) in a property file. I have many such property files in subfolders where i need to replace jdbc url. Below is the code that i have tried but it does not work.
Need your suggestions for the same.

.property file will have value like below.
Code:
JDBC_URL=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=stora.com)(PORT=1521))(LOAD_BALANCE=YES)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=STYU)(FAILOVER_MODE=
(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))))"
 
INSTALL_ID=KTJUM


change.sh is the file where i am writing below code:


Code:
NEW="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=stem.com)(PORT=1521))(LOAD_BALANCE=YES)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=uyet)(FAILOVER_MODE=
(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))))"
 
 
for file in `find . -name *.properties`
do 
cat $file |grep -iw "JDBC_URL"|cut -d '@' -f2|read OLD
echo "perl -i -p -e 's/$OLD/$NEW/g' $file" |sh
done

OLD variable contains below string from file:
Code:
(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=stora.com)(PORT=1521))(LOAD_BALANCE=YES)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=STYU)(FAILOVER_MODE=
(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=5))))"

When i do this, the OLD string is not getting replaced with new String. But when i do the same thing for other line in file like install_id, the perl command works.

Please suggest what i am missing in the code and how better i can work.


PS: there is no underline for delimiter @ but i dont why i am getting here in the above i tried undoing the underline but it isnot working. Please ignore the underline.

Last edited by Scrutinizer; 04-19-2014 at 06:08 AM.. Reason: code tags
# 2  
Old 04-19-2014
Does your find command give any output (-name *.properties while the file extension seems to be property)?
# 3  
Old 04-19-2014
The all file that i want to change through above code have extension as .properties .
# 4  
Old 04-21-2014
Code:
perl -i -pe '$new="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=stem.com)(PORT=1521))(LOAD_BALANCE=YES)(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=uyet)(FAILOVER_MODE=(TYPE=SELECT)(METHOD=BASIC)(RETRIES=180)(DELAY=15))))";s/(JDBC_URL=.+?)\@(.*)/$1\@$new\"/;' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace line for another line in file

Hey, i have been struggeling to find the answer myself so this is the best forum has alwyas help me :) i have an API that get data from a provider via curl. the data comes like this : AccountCode1234 CompneyNameA Credit: 200 AccountCode5678 CompneyNameB Credit: 200 AccountCode999... (3 Replies)
Discussion started by: batchenr
3 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

Replace Each Line Of A File

Hi All, I want to repalce each line of a file by a constant value using awk. I do not need to match the pattern and replace and I also do not need to perform replace for selected lines of file. Every line in the file will be replace by the constant value. I searched every where and did not... (2 Replies)
Discussion started by: angshuman
2 Replies

6. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

7. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

8. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

9. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

10. Shell Programming and Scripting

how to replace a line in file with blank line

Hi I nned cmd to which will help me to replace a line in file with blank line e.g. file1 a b c d e after running cmd I shud get file1 b c d e (5 Replies)
Discussion started by: tarunn.dubeyy
5 Replies
Login or Register to Ask a Question