getut
March 6th, 2020, 10:08 PM
I'm trying to use awk to edit an xml file. I'm using awk instead of sed because ultimately this is purposed for a non-ubuntu, non-debian OS and there are limitations to the freebsd sed where this ultimately needed.
In my environment awk is more portable from my testing platform to the final usage than sed.
It is a rather large xml file but the gist of it is that I have a unique line that is my search line, directly followed by the line that I actually need to change. The source for the change will be coming from another file that contains ONLY the entire line that needs to be changed.
So the 2 line block in the file that needs to be changed would be similar to:
<unique_xml_tag>data1,data2</unique_xml_tag>
<custom_tag>encrypted data that needs to be changed</custom_tag>
The extra file contains ONLY the line
<custom_tag>different encrypted data</custom_tag>
Just playing with this and trying to break this down into logical learning steps. I am past the step of simply identifying the line I need to change. But I'm getting something way wrong trying expand on it and work this into some version of the sub command.
awk '/unique_xml_tag/ { getline; print }' file_to_be_edited.xml
is spitting out the line that I need to change. But the rest of my needs are beyond me so far.
I have tried reading my changes into a variable with awk -v changes=`cat file_containing_changes.txt` but that doesn't seem to work. Also for the actual substitution itself I've been playing with awk sub and can get it to work replacing the first line that I match to, but getting sub to work with getline so that it takes place on the second line is also too much for me at this early stage.
I just can't pull it together into a working command and I can't find any examples of quite this type of scenario. I found getline into a variable from a file, I found replacing from secondary files, but nothing quite like this to use as an example.
In my environment awk is more portable from my testing platform to the final usage than sed.
It is a rather large xml file but the gist of it is that I have a unique line that is my search line, directly followed by the line that I actually need to change. The source for the change will be coming from another file that contains ONLY the entire line that needs to be changed.
So the 2 line block in the file that needs to be changed would be similar to:
<unique_xml_tag>data1,data2</unique_xml_tag>
<custom_tag>encrypted data that needs to be changed</custom_tag>
The extra file contains ONLY the line
<custom_tag>different encrypted data</custom_tag>
Just playing with this and trying to break this down into logical learning steps. I am past the step of simply identifying the line I need to change. But I'm getting something way wrong trying expand on it and work this into some version of the sub command.
awk '/unique_xml_tag/ { getline; print }' file_to_be_edited.xml
is spitting out the line that I need to change. But the rest of my needs are beyond me so far.
I have tried reading my changes into a variable with awk -v changes=`cat file_containing_changes.txt` but that doesn't seem to work. Also for the actual substitution itself I've been playing with awk sub and can get it to work replacing the first line that I match to, but getting sub to work with getline so that it takes place on the second line is also too much for me at this early stage.
I just can't pull it together into a working command and I can't find any examples of quite this type of scenario. I found getline into a variable from a file, I found replacing from secondary files, but nothing quite like this to use as an example.