Help fixing awk code to print values from 2 files

Hi everyone,

Please help on this:

I have file1:

<file title="Title 1 and 2">
    <report>
      <title>Title 1</title>
      <number>No. 1234</number>
      <address>Address 1</address>
      <date>October 07, 2009</date>
      <description>Some text</description>
    </report>
    <report>
      <title>Title 2</title>
      <number>No. 457</number>
      <address>Address 2</address>
      <date>October 15, 2009</date>
      <description>Some text</description>
    </report>
  </file>

and file2:

<file title="Title 1 and 2">
    <report id="No. 1234">
      <f v="1">Report x</f>
      <f v="1">Report t</f>
      <f v="1">Report y</f>
      <f v="1">Report u</f>
      <f v="1">Report o</f>
      <j v="0">Report m</f>
    </report>
    <report id="No. 457">
      <f v="1">Report x</f>
      <f v="1">Report t</f>
      <f v="1">Report y</f>
      <f v="1">Report u</f>
      <f v="1">Report o</f>
      <j v="0">Report m</f>
    </report>
  </file>

and I want to get this output:

       <si><t>Report</t></si>
       <si><t>Tittle</t></si>
       <si><t>Number</t></si>
       <si><t>Address</t></si>
       <si><t>Date</t></si>
       <si><t>Description</t></si>
       <si><t>Title 1 and 2</t></si>
       <si><t>Title 1</t></si>
       <si><t>No. 1234</t></si>
       <si><t>Address 1</t></si>
       <si><t>October 07, 2009</t></si>
       <si><t>Some text</t></si>
       <si><t>Title 2</t></si>
       <si><t>No. 457</t></si>
       <si><t>Address 2</t></si>
       <si><t>October 15, 2009</t></si>
       <si><t>Some text</t></si>
       <si><t>Report o</t></si>
       <si><t>Report t</t></si>
       <si><t>Report u</t></si>
       <si><t>Report x</t></si>
       <si><t>Report y</t></si>

The things is I'm trying since a couple of hours ago changing my code (below code) to get the output desired,
but the central part (values from file1 in red) that supposed to be as the values in blue above are not being shown correctly, the output I'm getting is.

       <si><t>Report</t></si>
       <si><t>Tittle</t></si>
       <si><t>Number</t></si>
       <si><t>Address</t></si>
       <si><t>Date</t></si>
       <si><t>Description</t></si>
       <si><t>Title 1 and 2</t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t>Title 1</t></si>
       <si><t></t></si>
       <si><t>No. 1234</t></si>
       <si><t></t></si>
       <si><t>Address 1</t></si>
       <si><t></t></si>
       <si><t>October 07, 2009</t></si>
       <si><t></t></si>
       <si><t>Some text</t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t>Title 2</t></si>
       <si><t></t></si>
       <si><t>No. 457</t></si>
       <si><t></t></si>
       <si><t>Address 2</t></si>
       <si><t></t></si>
       <si><t>October 15, 2009</t></si>
       <si><t></t></si>
       <si><t>Some text</t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t></t></si>
       <si><t>Report o</t></si>
       <si><t>Report t</t></si>
       <si><t>Report u</t></si>
       <si><t>Report x</t></si>
       <si><t>Report y</t></si>

My code so far is:

awk -F'[<>]' 'BEGIN{F="\t<si><t>";B="</t></si>"
  print F"Report"B"\n",F"Tittle"B"\n",F"Number"B"\n",F"Address"B"\n",F"Date"B"\n",F"Description"B}
    NR==FNR{
    {$2~"file title"; split ($0,N,"\""); print F N[2] B}
    {$2=="title" || $2=="number" || $2=="address" || $2=="date" || $2=="description"; print F $3 B}
    next} 
    $2 ~ /f v=\"/{u[$3]}
    END{for (i in u) print F i B | "sort"}' file1 file2

May somebody help to show me where is the problem with my code or fix it please?

Thanks in advance

Hi Ophiuchus,

I fixed your code. Test it:

$ awk -F'[<>]' 'BEGIN{F="\t<si><t>";B="</t></si>"
  print F"Report"B"\n",F"Tittle"B"\n",F"Number"B"\n",F"Address"B"\n",F"Date"B"\n",F"Description"B}
    NR==FNR{
    if ($2~"file title") { split ($0,N,"\""); print F N[2] B } else if (\
    $2=="title" || $2=="number" || $2=="address" || $2=="date" || $2=="description" ) {print F $3 B}
    next} 
    $2 ~ /f v=\"/{u[$3]}
    END{for (i in u) print F i B | "sort"}' file1 file2
        <si><t>Report</t></si>
        <si><t>Tittle</t></si>
        <si><t>Number</t></si>
        <si><t>Address</t></si>
        <si><t>Date</t></si>
        <si><t>Description</t></si>
        <si><t>Title 1 and 2</t></si>
        <si><t>Title 1</t></si>
        <si><t>No. 1234</t></si>
        <si><t>Address 1</t></si>
        <si><t>October 07, 2009</t></si>
        <si><t>Some text</t></si>
        <si><t>Title 2</t></si>
        <si><t>No. 457</t></si>
        <si><t>Address 2</t></si>
        <si><t>October 15, 2009</t></si>
        <si><t>Some text</t></si>
        <si><t>Report o</t></si>
        <si><t>Report t</t></si>
        <si><t>Report u</t></si>
        <si><t>Report x</t></si>
        <si><t>Report y</t></si>

Regards,
Birei

Hi birei,

Many thatnks for your reply, it works just perfect!! thank you, thank you.

Only one more thing to finish, how to write in a shorter way the "print part" using "printf"?
I need to print what I already have and 2 more lines at the beginning and one at the end as follow.
(the blue lines are what I need to add, the lines in red are those I want to write in a better way using Printf.
I want to learn how to do it within the same awk code)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <si><t>Report</t></si>
    <si><t>Tittle</t></si>
    <si><t>Number</t></si>
    <si><t>Address</t></si>
    <si><t>Date</t></si>
    <si><t>Description</t></si>
    <si><t>Title 1 and 2</t></si>
    <si><t>Title 1</t></si>
    <si><t>No. 1234</t></si>
    <si><t>Address 1</t></si>
    <si><t>October 07, 2009</t></si>
    <si><t>Some text</t></si>
    <si><t>Title 2</t></si>
    <si><t>No. 457</t></si>
    <si><t>Address 2</t></si>
    <si><t>October 15, 2009</t></si>
    <si><t>Some text</t></si>
    <si><t>Report o</t></si>
    <si><t>Report t</t></si>
    <si><t>Report u</t></si>
    <si><t>Report x</t></si>
    <si><t>Report y</t></si>
</sst>

Many thanks again for your help.

Grettings

New version:

$ awk -F'[<>]' 'BEGIN{F="\t<si><t>";B="</t></si>";H="Report,Tittle,Number,Address,Date,Description"; split(H, arrH, /,/);
  for (i=1;i<=length(arrH);i++) { printf "%s%s%s\n", F, arrH, B} }
    NR==FNR{
    if ($2~"file title") { split ($0,N,"\""); print F N[2] B } else if (\
    $2=="title" || $2=="number" || $2=="address" || $2=="date" || $2=="description" ) {print F $3 B}
    next} 
    $2 ~ /f v=\"/{u[$3]}
    END{for (i in u) print F i B | "sort"}' file1 file2
        <si><t>Report</t></si>
        <si><t>Tittle</t></si>
        <si><t>Number</t></si>
        <si><t>Address</t></si>
        <si><t>Date</t></si>
        <si><t>Description</t></si>
        <si><t>Title 1 and 2</t></si>
        <si><t>Title 1</t></si>
        <si><t>No. 1234</t></si>
        <si><t>Address 1</t></si>
        <si><t>October 07, 2009</t></si>
        <si><t>Some text</t></si>
        <si><t>Title 2</t></si>
        <si><t>No. 457</t></si>
        <si><t>Address 2</t></si>
        <si><t>October 15, 2009</t></si>
        <si><t>Some text</t></si>
        <si><t>Report o</t></si>
        <si><t>Report t</t></si>
        <si><t>Report u</t></si>
        <si><t>Report x</t></si>
        <si><t>Report y</t></si>

Regards,
Birei

Hi birei,

Many thanks, it works, learning from you.

The last question, I think I need to add another print command in BEGIN statement to print fist 2 lines in blue, but how to print the last line in blue within END statement?

Try:

$ awk -F'[<>]' 'BEGIN{F="\t<si><t>";B="</t></si>";H="Report,Tittle,Number,Address,Date,Description"; split(H, arrH, /,/);
  for (i=1;i<=length(arrH);i++) { printf "%s%s%s\n", F, arrH, B} }
    NR==FNR{
    if ($2~"file title") { split ($0,N,"\""); print F N[2] B } else if (\
    $2=="title" || $2=="number" || $2=="address" || $2=="date" || $2=="description" ) {print F $3 B}
    next} 
    $2 ~ /f v=\"/{u[$3]=FNR}
    END{asorti(u);for (i=1;i<=length(u);i++) print F u B; print "</sst>"}' file1 file2
        <si><t>Report</t></si>
        <si><t>Tittle</t></si>
        <si><t>Number</t></si>
        <si><t>Address</t></si>
        <si><t>Date</t></si>
        <si><t>Description</t></si>
        <si><t>Title 1 and 2</t></si>
        <si><t>Title 1</t></si>
        <si><t>No. 1234</t></si>
        <si><t>Address 1</t></si>
        <si><t>October 07, 2009</t></si>
        <si><t>Some text</t></si>
        <si><t>Title 2</t></si>
        <si><t>No. 457</t></si>
        <si><t>Address 2</t></si>
        <si><t>October 15, 2009</t></si>
        <si><t>Some text</t></si>
        <si><t>Report o</t></si>
        <si><t>Report t</t></si>
        <si><t>Report u</t></si>
        <si><t>Report x</t></si>
        <si><t>Report y</t></si>
</sst>

Regards,
Birei

Hi birei,

Perfect! Many thanks, you have solved my problem, many thanks for your help.

Regards.