Hi I'm trying to replace text in a file based upon a pattern.
The pattern I'm looking for is:
<styleURL>#style0002</styleURL>
<name>#######6105#######</name>
The # are seven alphanumeric characters before and after 6105.
I need it to replace that with this recursively:
<styleURL>#style0003</styleURL>
<name>#######6105#######</name>
Any ideas would be appreciated.
Thanks
If the two lines are consecutive in your file you can try something like this:
awk '
/\<styleURL\>#style0002\<\/styleURL\>/ {
print "<styleURL>#style0003</styleURL>"
print "<name>#######6109#######</name>"
getline;next
}1' file
That will replace
<styleURL>#style0002</styleURL>
<name>*******6105*******</name>
with
<styleURL>#style0003</styleURL>
<name>*******6105*******</name>
?
Just double checking.
I was wrong when I used # to represent alphanumeric characters as the # before style0002 isnt an alphanumeric it is always there.
And yes they are always consecutive.
Let me know if you need to know anything else.
Thanks!
It's the right idea - at least, it's how I would go about it - however, I think Franklin52 made a few typos, though, and one mistake.
First, s/6109/6105/; then, un-escape '<' and '>' ('\<' and '\>' have a meaning of their own; they match the empty string at the begging, end of a word). I would lose the '1' after the closing '}' as well; I think that's a typo (if I'm wrong and it does serve a purpose, someone please correct me...).
Keep in mind that the code as presented below doesn't need an explicit 'next' statement - you can include one, but you don't need to in this instance (it's implicit). If you ever have a need to skip to the next record immediately after doing something, though, use next; it's analogous to continue in a for loop.
Fix those minor issues and I think that code will do what you want it to do; that is, assuming that I'm understand correctly what you're trying to do. Restated, that code is:
awk '/<styleURL>#style0002<\/styleURL>/ {
print "<styleURL>#style0003</styleURL>";
print "<name>#######6105#######</name>";
getline;
}'
Personally, I love awk for tasks of this nature.
You just want to replace "style0002" with "style0003" within the styleURL tag and the 4 numbers after the 7th position of the name tag, right?
Try something like this, I've used awk variables for the values, adjust them for your own fit:
awk -v old="style0002" -v new="style0003" -v num="1234" '
/\<styleURL\>/ && $0 ~ old {
sub(old, new)
print
getline
print substr($0, 1,13) num substr($0, 18)
next
}1' file