search and replace
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I need to do the following search and replace in a text file.
I'm looking for a pattern
AAA*BBB where * is any string of characters not containing BBB (i.e BBB is the first BBB after AAA). I need to replace all such occurrences with AAACCCBBB
I suspect this can be done with awk reasonably easily.
14 replies
Sign in or join Beachball Help to reply.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
sed might be a better choice. I don't find "man sed" particularly easy reading, but http://www.grymoire.com/Unix/Sed.html may shed a little more light on it. I have only used it for basic substitutions, like sed -e 's;originalstring;replacementtext;g' filename
The delimiter is whatever the character following the 's is, so in this example, ; is the delimiter. Usually, the tutorials and such use a / as the delimiter. The trailing g means do a global replacement. It's supposed to be able to deal with regular expressions so you might be able to cobble together an expression using things like [0-9] which would mean any numeric 0 thru 9.
Sorry I can't be of more help to you but I'm just not very skilled with sed. But I think it might be a better choice of tool for what you are trying to do.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
yes, i know how to do basic search and replace using both awk and sed but I don't know how to do the kind of replace I'm asking about.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Using something like you had for an example, suppose you had the string AAAPPPBBB
I use the vi editor to do things like this
.,$s/PPP/CCC/g
this will search the file from where the cursor is, usually at the top, to the bottom and will change all occurrences of PPP to CCC.
However, I suspect you don't have anything that simple to change.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
However, I suspect you don't have anything that simple to change.
you are right, I don't have anything so simple. the string that has to be changed is variable in content and in length. I think the longest one is 15 characters and the shortest is 5.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
This is a fairly simple sed:
sed -i -e 's/AAA.*BBB/AAACCCBBB/g' file
This tells sed to look for 'AAA.*BBB' (where .* is the standard regular expression for 'any number of any character).
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I should have mentioned that I've tried this very command before posting the question but it doesn't appear to do what I need. It looks for the first occurrence of AAA and the last occurrence of BBB after that and kills everything in between. that's not what I want. I want the first occurrence of BBB after AAA and replace only the stuff in between.
to clarify, given
AAAblahblahBBBblah blah AAAblah BBB
the command as given turns it into AAACCCBBB while i want this
AAACCCBBBblah blah AAACCCBBB
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
sed 's/(AAA).*(BBB)/ CCC /' - 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
perl -pi -e 's/(AAA).*(BBB)/$1CCC$2/'
man perlrun - 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
nope, this still replaces everything between the first AAA and the last BBB, just like Camelot's command.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
BobHarris wrote:
perl -pi -e 's/(AAA).*(BBB)/$1CCC$2/'
man perlrunsame problem with this one.
to clarify again. both your commands turn
AAAblahblahBBBblah blah AAAblah BBBinto AAACCCBBB while what I want as the result is
AAACCCBBBblah blah AAACCCBBB
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
ok, someone on another forum clued me in how to make perl matching non-greedy by adding ? at the end and the following variation of your command seems to do what i want
perl -pi -e 's/(AAA).*?(BBB)/$1CCC$2/g'
Thanks!
Message was edited by: V.K.
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You want a non-greedy search, with a global switch.
OK, try this:
perl -pi -e 's/(AAA).*?(BBB)/$1CCC$2/g'there are 2 changes.
Notice the ? after the .* which say do not be greedy.
and notice the g at the end of the command which says apply this change to all occurances.
Does this work for you?
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I was just 4 minutes too late
- 0
Apple Support Community member ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
yep, see my post just above yours!