Read and copy xml line by line and preserve tab?

I'm trying to read an xml file and copy it line by line to another file and want to preserve the tabs.

What i'm trying to do is if I get to a certain line in the xml, I'm going to check to see if the next line is specifically what I want. If it's not, then I want to insert a single line of text right after it.

So I'm thinking I'll read the original xml line by line and copy it over to a temp file. While reading it line by line, if I come across my specific situation, i'll concat that new line to the temp file and continue with the rest of the xml.

Any ideas?

thanks in advance

An example to play around with:

awk '
/pattern/ {
  print $0
  getline
  # do your stuff and....
  print "Your new line..."
  next
}
{print}' file

hey thanks alot Franklin...major help...thanks, I'm almost there...

I modified the following from you...

awk '
/version/ {
  print $0
  getline
  print $0
  if ($0 != "<letters>")
    print "<letters>"
  next
}
{print}' $testfile 

what I can't figure out to do is put that value of print in bold to not show up in the results. I need it to do the if statement. But it's also showing up in my ending result.

Any ideas?

This is the current results:

<?xml version="1.0" encoding="UTF-8" ?>
        <GLOBAL>
<letters>
                <SYSTEM>ABC</SYSTEM>
                <SUBSYSTEM>LETTER</SUBSYSTEM>
        </GLOBAL>
        <letter templateName="TEMP">
                <patient patientID="123456789">
        </letter>
</letters>
<?xml version="1.0" encoding="UTF-8" ?>
        <GLOBAL>
<letters>
                <SYSTEM>ABC</SYSTEM>
                <SUBSYSTEM>LETTER</SUBSYSTEM>
        </GLOBAL>
        <letter templateName="TEMP">
                <patient patientID="987654321">
        </letter>
</letters>
<?xml version="1.0" encoding="UTF-8" ?>
<letters>
        <GLOBAL>
                <SYSTEM>ABC</SYSTEM>
                <SUBSYSTEM>LETTER</SUBSYSTEM>
        </GLOBAL>
        <letter templateName="TEMP">
                <patient patientID="546213789">
        </letter>
</letters>

The first two xml does NOT have <letters> after the <?xml tag. The third one does.

As you can see, the script adds <letters> to the first one and two xml's but it also printed out the <GLOBAL> tag since that was what was right after "<?xml"

thanks

awk '
/version/ {
  print $0
  getline
  !/<letters>/
    print "<letters>"
  next
}
{print}' $testfile

Hi rdcwayx,

Appreciate your code. Much simplier then mine.

I should have been more clear. What you gave me did exactly as the code i put in my post with the exception of the second "print $0".

I put the "print $0" in there on purpose because it prints the "<GLOBAL>" tag.

If I didn't have that "print $0" line, then "<letters>" will overlay "<GLOBAL>" tag. That's what you have in your code too.

What I really want is the "<letters>" tag to be inserted in between "<?xml" and "<GLOBAL>", not just come after "<?xml" and overlay "<GLOBAL>".

I'm going to keep playing with it but if anyone has a quick solution, i'd appreciate it too.

This is the result of your code:

<?xml version="1.0" encoding="UTF-8" ?>
<letters>
                <SYSTEM>ABC</SYSTEM>
                <SUBSYSTEM>LETTER</SUBSYSTEM>
        </GLOBAL>
        <letter templateName="TEMP">
                <patient patientID="123456789">
        </letter>
</letters>
<?xml version="1.0" encoding="UTF-8" ?>
<letters>
                <SYSTEM>ABC</SYSTEM>
                <SUBSYSTEM>LETTER</SUBSYSTEM>
        </GLOBAL>
        <letter templateName="TEMP">
                <patient patientID="987654321">
        </letter>
</letters>
<?xml version="1.0" encoding="UTF-8" ?>
<letters>
        <GLOBAL>
                <SYSTEM>ABC</SYSTEM>
                <SUBSYSTEM>LETTER</SUBSYSTEM>
        </GLOBAL>
        <letter templateName="TEMP">
                <patient patientID="546213789">
        </letter>
</letters>

thanks!

---------- Post updated at 04:24 PM ---------- Previous update was at 02:12 PM ----------

i got it figured out everyone...thanks for your help!!!

awk '
/version/ {
  print $0
  getline
  if ($0 != "<letters>") 
  {
    print "<letters>"
    print $0
  }
  else
  {
    print "<letters>"
  }
  next
}
{print}' $testfile