Read and copy xml line by line and preserve tab?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read and copy xml line by line and preserve tab?
# 1  
Old 01-06-2010
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
# 2  
Old 01-06-2010
An example to play around with:
Code:
awk '
/pattern/ {
  print $0
  getline
  # do your stuff and....
  print "Your new line..."
  next
}
{print}' file

# 3  
Old 01-06-2010
hey thanks alot Franklin...major help...thanks, I'm almost there...

I modified the following from you...

Code:
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:

Code:
<?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
# 4  
Old 01-06-2010
Code:
awk '
/version/ {
  print $0
  getline
  !/<letters>/
    print "<letters>"
  next
}
{print}' $testfile

# 5  
Old 01-07-2010
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:

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!!!

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do you preserve the max length of a line after replacing a specific value?

I'm new with scripting and I can't seem to figure out what I should do to get the output that I want. My file content would be below. ID2|ID3 |ID4|ID5 | I'm trying to replace the field of ID3 which has a fixed length of 10 characters, for each entry I have placed on a .txt file... (5 Replies)
Discussion started by: asdfghjkl
5 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

6. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

7. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

Preserve first line and search in others

Hi gurus Is possible to preserve first line of text and search in another lines ? the only way that I think is: ps aux | awk '{if (NR==1) {print $0} else if ($11 ~ /sshd/) {print $0}}' but is there any more elegant way ? thanks (4 Replies)
Discussion started by: wakatana
4 Replies

10. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies
Login or Register to Ask a Question