Visit Our UNIX and Linux User Community


grep a pattern and replace a value in it and write to the same file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a pattern and replace a value in it and write to the same file.
# 1  
Old 09-11-2009
grep a pattern and replace a value in it and write to the same file.

I have some complication with this, I have a file like below[db.sh] for DEV_1 till DEV_10. and the db values are set accordinly which are not unique. For example DEV1,DEV4,DEV6 can have the same target DB name.
I waned to identify[grep] for DEV_2 and then replace the TARGET_DATABASE value with the new DB name, can you suggest.

DEV_1_SOURCE_DATABASE=server1
DEV_1_SOURCE_AUDIT_DATABASE= server1
DEV_1_TARGET_DATABASE=server4
DEV_1_TARGET_AUDIT_DATABASE=server4

DEV_2_SOURCE_DATABASE=server2
DEV_2_SOURCE_AUDIT_DATABASE= server2
DEV_2_TARGET_DATABASE=server5
DEV_2_TARGET_AUDIT_DATABASE=server5
.
.
.
.
.
.


i used sed like sed 's/`cat db.sh | grep -i DEV_2 | grep -i database | egrep -v "audit|source"`/newdbname' < db.sh > db.sh_new
i got a error;
tried awk but dint work as i am trying to pass command on:
can someone help in this :
my ultimate need is i need to grep a pattern and replace the value in that location only,


this might be too long !!!!
# 2  
Old 09-11-2009
can you post the required output?
# 3  
Old 09-11-2009
In case new db name is "UNIX",
Are you expecting the output as follows?

DEV_1_SOURCE_DATABASE=server1
DEV_1_SOURCE_AUDIT_DATABASE= server1
DEV_1_TARGET_DATABASE=server4
DEV_1_TARGET_AUDIT_DATABASE=server4

DEV_2_SOURCE_DATABASE=server2
DEV_2_SOURCE_AUDIT_DATABASE= server2
DEV_2_TARGET_DATABASE=UNIX
DEV_2_TARGET_AUDIT_DATABASE=server5

Is this what your expecting?
# 4  
Old 09-11-2009
yes, u are right,, i expect the outut as above,,

but i would like to rremind that the original file contains "server4" as a value for more than one DB target,, i mean not only DEV_2 target is server4 but also say DEV_3 will be targetted to server4.
# 5  
Old 09-11-2009
This may help you.

Code:
sed 's/\(DEV_2_TARGET_DATABASE=\)\(.*\)/\1UNIX/g' t1

# 6  
Old 09-11-2009
ope the file in vi editor

1.hit esc .
2. Type %s/"pattern to be matched"/"pattern to be replaced"/g

This will replace all the instances of pattern to be matched
# 7  
Old 09-11-2009
since i could not get your expectation clearly,
Here am giving some solution

$cat file

DEV_1_SOURCE_DATABASE=server1
DEV_1_SOURCE_AUDIT_DATABASE= server1
DEV_1_TARGET_DATABASE=server4
DEV_1_TARGET_AUDIT_DATABASE=server4

DEV_2_SOURCE_DATABASE=server2
DEV_2_SOURCE_AUDIT_DATABASE= server2
DEV_2_TARGET_DATABASE=server5
DEV_2_TARGET_AUDIT_DATABASE=server5

Solution 1:

script1.sh
a=` cat file | grep -w "DEV_2_TARGET_DATABASE" | cut -d '=' -f 2`
sed "s/\($a\)/newdb/g" file > file1
mv file1 file

The above script will take the value assigned for DEV_2_TARGET_DATABASE, and substitute that value i.e "server5" to newdb in the whole file content. ( even for DEV 4 or DEV 3)

Solution 2:

If you want to substitute only DEV_2_TARGET_DATABASE=server5 to DEV_2_TARGET_DATABASE=newdb

sed "s/\(DEV_2_TARGET_DATABASE=\)\(.*\)/\1newdb/g" file

Hope this helps..

Previous Thread | Next Thread
Test Your Knowledge in Computers #133
Difficulty: Easy
The first prototypes of Linux were publicly released in 1991.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

2. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

3. Shell Programming and Scripting

Search pattern and write line into another file

Hi, I have a file which contains the below details.. My requirement is to fetch all the lines which are starting with "ABC_XY_" into 1 file and rest of the lines (not starting with "ABC_XY_") into another file. Could you please help with what command needs to be used? file1.txt ----------... (12 Replies)
Discussion started by: satyaatcgi
12 Replies

4. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. UNIX for Dummies Questions & Answers

Write to file using tail -f through a pipe to grep

Hi -- I'm looking to write to a file after piping output from tail -f through to grep: #write to a file for all lines with "searchtext" within in error_log: Expand|Select|Wrap|Line Numbers tail -f /var/error_log | grep searchtext > output.txt The above command... (2 Replies)
Discussion started by: ndedhia1
2 Replies

7. Shell Programming and Scripting

Replace a particular pattern in a file

Hi , I have a requirement where i have this file abcd $$xyz=123 $$zef=789 $$mno=123 $$pqr=456 I need to replace $$pqr from 456 to a new value which will be present in a variable to me. I am not aware of sed and awk functionality. Can somebody please show me how to replace this... (6 Replies)
Discussion started by: lifzgud
6 Replies

8. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies

9. Shell Programming and Scripting

How to replace some content of a file and write with a new name

Hi I have a control file which looks like this LOAD DATA INFILE '/home/scott/XXX.dat' PRESERVE BLANKS ............. ............. how can i change the content of this file and replace the file in the second line with anothe file name and write it back with another name to the disk? ... (5 Replies)
Discussion started by: mwrg
5 Replies

10. Shell Programming and Scripting

help needed .. Unable to write the data to new file after matching the pattern

Hi, i am pretty new to Unix environment ..... Can i get some help from any of you guyz on writing Unix script. my requirement is like reading a csv file, finding a specific pattern in the lines and repalce the string with new string and write it to another file. My file is file ABC123.dat... (3 Replies)
Discussion started by: prashant_jsw
3 Replies

Featured Tech Videos