The following script seems to do what you want:
#! /usr/bin/awk -f
BEGIN {
inTag = 0
}
{
if (inTag == 0) {
position = match ($0,"<tag id=\"name\">")
if (position == 0)
print $0
else {
print substr($0,1,(position-1))
inTag = 1
}
} else {
if ( substr($0,1,6) == "</tag>") {
inTag = 0
print substr($0,7)
}
}
}
Notes, warnings etc...
The first line will probably have to be changed/removed depending on your exact environment and how you invoke the script (e.g. remove the first line and then do awk -f <script_file> <xml file> should work)
There is a built in assumption that there are no white spaces at the start of the lines.
The closing tag must be at the start of a line.
On the check for the closing tag you need to make sure the length (currently 6) is consistent with the tag length you are looking for and that the print starts at that value plus 1.
All of these limitations are removable but assuming they are not going to cause issues why make the script more complex than it needs to be. |