search and replace with restriction (awk, sed)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search and replace with restriction (awk, sed)
# 1  
Old 03-31-2008
search and replace with restriction (awk, sed)

Hello,
i have a file like that
Code:
foo A new
bar A new
bar B new

I need to replace 'new' with 'done',
but only in lines containing 'bar' AND 'A'.

output file should then become
Code:
foo A new
bar A done
bar B new

Sorry im not able to figure it out, not even shure if i should take sed.
Thanks a lot for suggestions.
knoxo
# 2  
Old 03-31-2008
Sounds like an awk problem actually. But sure, it can be done in sed, too.

Code:
sed 's/bar A new/bar A done/' filename

# 3  
Old 03-31-2008
Thank you for your reply, but i forgot to mention that i can not rely on the order of 'A' and 'bar'. i just can assume that they appear anywhere in that line.
So perhaps a modification of this sed-command would do the task.

For example,

Code:
sed '/bar A/ s/new/done/' filename

would search for the pattern "bar A" and then replace new with done in that particular line.
But what I need is an expression for the pattern like
"'bar' AND 'A' appear in that line".
Now its more like
"'bar A' appears in that line".
Anyone who can help?
# 4  
Old 03-31-2008
Code:
sed -e '/bar/!n' -e '/A/!n' -e 's/new/done/' file

This skips to the next line if there is no match on bar, and then if there is no match on A.

You might want to tighten up the expressions since a stray A in the middle of another word would match the second condition, but without more information, we can't help you with that. If you can be sure there will be a space on both sides then that's obviously a trivial improvement which would reduce the risk of false matches somewhat significantly.
# 5  
Old 03-31-2008
Great, that works!
'A' and 'B' were just examples, they actually correspond to longer strings...

Perhaps i should change the syntax to prevent such kind of matches if the search text appears anywhere in the line.
What if the file consists of colums instead (column delimiter is ':'):

Code:
foo : A : new
bar : A : new
bar : B : new

how could i restrict the search and replace condition to

"if column1 contains 'bar' AND column2 contains 'A', then replace any text in column3 with 'done'.
knoxo
# 6  
Old 03-31-2008
Try:

Code:
awk 'BEGIN { FS=OFS=":" } $1 == "bar" && $2 == "A" { $3="done" }' inputfile

Jean-Pierre.
# 7  
Old 03-31-2008
hello Jean-Pierre,

edit:

Now it works, i had additional blanks around the 'A' and 'bar', which is different from ' A ' etc so no math was found.
How could i make it insensitive to leading or trailing blanks?

However,
Code:
awk 'BEGIN { FS=OFS=":" } 1==1 { $3="done" }' inputfile

does not give any output either, while

Code:
awk 'BEGIN { FS=OFS=":" } 1==1' inputfile

prints all the contents of the file.
Help me figure out whats happening here?
knoxo

Last edited by knoxo; 03-31-2008 at 05:14 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another sed/awk search=>replace question

Hello, Need a little bit of help. Basically I need to replace lines in a file which were calculated wrong as it would 12 hours to regenerate the data. I need to calculate values based on other files which I've managed to figure out with grep/cut but now am stuck on how to shove these new... (21 Replies)
Discussion started by: f77coder
21 Replies

2. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

3. Shell Programming and Scripting

Search and replace is not working by sed or awk

Hi , I have one file and in this file i have one like TEST1 KEY0=AAC040R1;AAC041R1ISE;AAC041R2ISE;AAC370R1;ADR0500;ADR0600;AME245R1;AME245R2;BAP0135;BAP0300;PPINVDTD*;PPJERPTD*;PPJERPT*;PRBSUMM*;: i want to replace this line with the following line TEST1... (4 Replies)
Discussion started by: ashissau
4 Replies

4. Shell Programming and Scripting

Need help with search and replace using SED

Hi guys, thanks for accepting me in your forum .. I am trying to clean some hacked PHP files using SSH .. I am using this command: find . -type f -print0 | xargs -0 sed -i '/god_mod/d' <?php ... (3 Replies)
Discussion started by: wisam74us
3 Replies

5. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

6. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

7. Shell Programming and Scripting

awk/sed string search and replace

Need help with either sed or awk to acheive the following file1 ----- In the amazon forest The bats eat all the time... mon tue wed they would eat berries In the tropical forest The bats eat all the time... on wed bats eat nuts In the rain forest The bats eat all the time... on... (2 Replies)
Discussion started by: jville
2 Replies

8. Shell Programming and Scripting

sed search and replace

hi, im new for sed, anyone can help me to these in sed command my output file.txt "aaa",a1,bbb "ddd",a1,ccc "eee",a1,www need to change a1, to "a1"," output i need "aaa","a1","bbb "ddd","a1","ccc "eee","a1","www thanks in advance fsp (2 Replies)
Discussion started by: fspalero
2 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. UNIX for Dummies Questions & Answers

sed search and replace

Hello Folks, Anyone know how I can replace this line in file.xml <oacore_nprocs oa_var="s_oacore_nprocs">8</oacore_nprocs> with this line <oacore_nprocs oa_var="s_oacore_nprocs">1</oacore_nprocs> using sed or awk ? Thanks for your time. Cheers, Dave (7 Replies)
Discussion started by: d__browne
7 Replies
Login or Register to Ask a Question