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)
# 8  
Old 03-31-2008
I tried aigles' solution too, that was my first intuition, but it didn't seem too elegant after all. At least on my awk, you have to add a { print } at the end for it to do anything at all. Other than that, yes.

To continue in sed, the regular expressions would tend to get pretty dense, especially if in reality you have even more fields. I'm such a hammer-nail-regex person that I'd still probably try that before awk in real life (and then switch to perl if the sed got too wicked).

Code:
sed '/^bar:A:.*/bar:A:done/'

if there can be any number of columns before and after, something like

Code:
sed 's/\(\(^\|:\)bar:A:\)[^:]*/\1done/'

If there can be any number of fields in between, and they could be in either order, but the last field is always the one to change (don't look if you have a tendency for migraine):

Code:
sed -e 's/\(\(^\|:\)bar:\([^:]*:\)*A:\([^:]*:\)*\)[^:]*$/\1done/' -e t \
    -e 's/\(\(^\|:\)A:\([^:]*:\)*bar:\([^:]*:\)*\)[^:]*$/\1done/' file

Read up on regular expressions and back references if you would like to understand how this works.

Some sed variants might dislike the backslashes in the regex, or want more, or less, or different syntax. Consult the manual page for your local variant.

Last edited by era; 03-31-2008 at 05:17 PM.. Reason: Unsurprisingly, a bug in the complex example
# 9  
Old 03-31-2008
Sorry, I missed the space around the ':' delimiter in the input file, and the result is not written.
Try:
Code:
awk 'BEGIN { FS=OFS=":" } $1 == "bar " && $2 == " A " { $3="done" } 1' inputfile

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

Jean-Pierre.
# 10  
Old 03-31-2008
Thank you very much era and aigles, now i think i have something to play around with for the rest of the night. Smilie
knoxo
# 11  
Old 04-02-2008
Code:
while read line
do
echo $line | grep -w bar | grep A 1> /dev/null
if [ $? -le 0 ] 
then
	echo $line | grep -w bar | grep A | sed 's/new/done/'
else 
	echo $line
fi
done < filename

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