Help fixing awk code to print values from 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help fixing awk code to print values from 2 files
# 1  
Old 10-15-2011
Help fixing awk code to print values from 2 files

Hi everyone,

Please help on this:

I have file1:
Code:
<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:
Code:
<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:
Code:
       <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.
Code:
       <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:
Code:
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
# 2  
Old 10-15-2011
Hi Ophiuchus,

I fixed your code. Test it:
Code:
$ 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
# 3  
Old 10-15-2011
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)
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

Last edited by Ophiuchus; 10-15-2011 at 05:32 PM..
# 4  
Old 10-15-2011
New version:
Code:
$ 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[i], 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
# 5  
Old 10-15-2011
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?
# 6  
Old 10-16-2011
Try:
Code:
$ 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[i], 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[i] 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
# 7  
Old 10-16-2011
Hi birei,

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

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print median values of matrix -awk?

I use the following script to print the sum and how could I extend this to print medians instead? thanks name s1 s2 s3 s4 g1 2 8 6 5 g1 5 7 9 9 g1 6 7 8 9 g2 8 8 8 8 g2 7 7 7 7 g2 10 10 10 10 g3 3 12 1 24 g3 5 5 24 48 g3 12 3 12 12 g3 2 3 3 3 output name s1 s2 s3 s4 g1 5 7 8 9... (5 Replies)
Discussion started by: quincyjones
5 Replies

2. Shell Programming and Scripting

Help fixing awk code

can someone please help me spot and fix the issue with the following code: awk -F, -v SEARCHPATT="(Wed|Tue)" -v ADDISTR="Mon|Tue|Wed|Thu|Fri|Sat|Sun" -vVF="$VALFOUND" "BEGIN{ {D = D = 1 D = D = 2 } $0 ~ "," VF "," {L = 1 ... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. Shell Programming and Scripting

Print values within groups of lines with awk

Hello to all, I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX". My issue with current code is that should print values for 3 groups and only is... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

4. Shell Programming and Scripting

awk print odd values

value=$(some command) for all in `echo $value` do awk checks each value (all) to see if it is a odd number. if so, prints the value done sounds easy enough but i've been unable to find anything on google. (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

6. Shell Programming and Scripting

Print minimum and maximum values using awk

Can I print the minimum and maximum values of values in first 4 columns ? input 3038669 3038743 3037800 3038400 m101c 3218627 3218709 3217600 3219800 m290 ............. output 3037800 3038743 m101c 3217600 3219800 m290 (2 Replies)
Discussion started by: quincyjones
2 Replies

7. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

8. UNIX for Advanced & Expert Users

Awk to print values of second file

Hello, I have a data file with 300,000 records in it, and another file which contains only the line numbers of roughly 13,000 records in the data file which have data integrity issues. I'm trying to find a way to print the original data by line number identified in the second file. How can I do... (2 Replies)
Discussion started by: peteroc
2 Replies

9. Shell Programming and Scripting

Print a key with its all values using awk/others

input COL1 a1 b1 c1 d1 e1 f1 C1 10 10 10 100 100 1000 C2 20 20 200 200 200 2000 output C1 a1 10 1 C1 b1 10 1 C1 c1 10 1 C1 d1 100 2 C1 e1 100 2 C1 f1 1000 3 C2 ... (12 Replies)
Discussion started by: ruby_sgp
12 Replies

10. Shell Programming and Scripting

Awk to print distinct col values

Hi Guys... I am newbie to awk and would like a solution to probably one of the simple practical questions. I have a test file that goes as: 1,2,3,4,5,6 7,2,3,8,7,6 9,3,5,6,7,3 8,3,1,1,1,1 4,4,2,2,2,2 I would like to know how AWK can get me the distinct values say for eg: on col2... (22 Replies)
Discussion started by: anduzzi
22 Replies
Login or Register to Ask a Question