Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk help on multiple files as input and output to multiple files

Status
Not open for further replies.

pavankiran56

Programmer
Mar 11, 2013
1
HI all,
below is awk script im using to convert a csv file to HTML i want to passs multiple files as input to awk and get output in to multiple html files as output in one go is it possible ...

i know that awk can accept multiple inputs but out put is redirected to only one file but how can we get multiple outputfiles
Code:
file=$1
awk -F, ' BEGIN {
                print "<html><body>"
                print "<table border=1>"
} NR == 1 {
print "<tr>"
for(i=1;i<=NF;i++){
print "<th><b><font face=\"verdana\"><font size=\"2\"><font color=\"Brown\">" $i"</font></b></th>"
}
print "</tr>"
} NR > 1 {
print "<tr>"
for(i=1;i<=NF;i++) {
print "<td><b><font face=\"verdana\"><font size=\"2\"><font color=\"Black\">" $i"</font></b></td>"
}
print "</tr>"
} END {
                print "</table>"
                print "</body></html>"
} ' $file >> $file.html
 
The problem is that you write the begin and end of your HTML dokument in the bEGIN- and END-section. But, by processing multiple files these both sections will be executed only once and not on begin and end of every file.
To process multiple files you can embed your awk script in a shell script.
 
Hi

Which Awk implementation and version are you using ? [tt]gawk[/tt] 4.0.0 and newer have [tt]BEGINFILE[/tt] and [tt]ENDFILE[/tt] special patterns which would be helpful in this case.

Feherke.
[link feherke.github.com/][/url]
 
I would simply create from your code a function and call it for every file in file list, like this:
pavankiran56.sh
Code:
[COLOR=#0000ff]# ---------------- processing functions ----------------[/color]
[COLOR=#008080]create_html() {[/color]
 [COLOR=#008080]file[/color]=[COLOR=#a020f0]$1[/color]
 [COLOR=#804040][b]echo[/b][/color][COLOR=#ff00ff] [/color][COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff]** Now generating HTML for CSV-file '[/color][COLOR=#a020f0]$file[/color][COLOR=#ff00ff]'[/color][COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff] [/color]
 awk [COLOR=#6a5acd]-F[/color], [COLOR=#804040][b]'[/b][/color][COLOR=#ff00ff] BEGIN {[/color]
[COLOR=#ff00ff]                print "<html><body>"[/color]
[COLOR=#ff00ff]                print "<table border=1>"[/color]
[COLOR=#ff00ff] } [/color]
[COLOR=#ff00ff] NR == 1 {[/color]
[COLOR=#ff00ff]   print "<tr>"[/color]
[COLOR=#ff00ff]   for(i=1;i<=NF;i++){[/color]
[COLOR=#ff00ff]     print "<th><b><font face=\"verdana\"><font size=\"2\"><font color=\"Brown\">" $i"</font></b></th>"[/color]
[COLOR=#ff00ff]   }[/color]
[COLOR=#ff00ff]   print "</tr>"[/color]
[COLOR=#ff00ff] } [/color]
[COLOR=#ff00ff] [/color]
[COLOR=#ff00ff] NR > 1 {[/color]
[COLOR=#ff00ff]   print "<tr>"[/color]
[COLOR=#ff00ff]   for(i=1;i<=NF;i++) {[/color]
[COLOR=#ff00ff]     print "<td><b><font face=\"verdana\"><font size=\"2\"><font color=\"Black\">" $i"</font></b></td>"[/color]
[COLOR=#ff00ff]   }[/color]
[COLOR=#ff00ff]   print "</tr>"[/color]
[COLOR=#ff00ff] } [/color]

[COLOR=#ff00ff] END {[/color]
[COLOR=#ff00ff]   print "</table>"[/color]
[COLOR=#ff00ff]   print "</body></html>"[/color]
[COLOR=#ff00ff] } [/color][COLOR=#804040][b]'[/b][/color] [COLOR=#a020f0]$file[/color] [COLOR=#804040][b]>[/b][/color] [COLOR=#a020f0]$file[/color].html
[COLOR=#008080]}[/color]

[COLOR=#008080]process_file() {[/color]
  [COLOR=#008080]file[/color]=[COLOR=#a020f0]$1[/color]
  [COLOR=#804040][b]echo[/b][/color][COLOR=#ff00ff] [/color][COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff]*  Processing file: '[/color][COLOR=#a020f0]$file[/color][COLOR=#ff00ff]' ...[/color][COLOR=#804040][b]"[/b][/color]
  create_html [COLOR=#a020f0]$file[/color]
[COLOR=#008080]}[/color]

[COLOR=#0000ff]# ---------------- main --------------------------------[/color]
[COLOR=#008080]files[/color]=[COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff]CSV_file*.csv[/color][COLOR=#804040][b]"[/b][/color]
[COLOR=#804040][b]for [/b][/color]file [COLOR=#804040][b]in[/b][/color] [COLOR=#a020f0]$files[/color]; [COLOR=#804040][b]do[/b][/color]
  process_file [COLOR=#a020f0]$file[/color] 
[COLOR=#804040][b]done[/b][/color]
[COLOR=#804040][b]echo[/b][/color][COLOR=#ff00ff] [/color][COLOR=#804040][b]"[/b][/color][COLOR=#ff00ff]*  Done.[/color][COLOR=#804040][b]"[/b][/color]

Now when we have for example 2 csv files
CSV_file_01.csv
Code:
1 2 3
4 5 6
CSV_file_02.csv
Code:
a b c
x y z
then executing the shell script above
Code:
$ pavankiran56.sh
*  Processing file: 'CSV_file_01.csv' ...
** Now generating HTML for CSV-file 'CSV_file_01.csv'
*  Processing file: 'CSV_file_02.csv' ...
** Now generating HTML for CSV-file 'CSV_file_02.csv'
*  Done.
generates 2 HTML files:
CSV_file_01.csv.html
Code:
<html><body>
<table border=1>
<tr>
<th><b><font face="verdana"><font size="2"><font color="Brown">1 2 3</font></b></th>
</tr>
<tr>
<td><b><font face="verdana"><font size="2"><font color="Black">4 5 6</font></b></td>
</tr>
</table>
</body></html>
CSV_file_02.csv.html
Code:
<html><body>
<table border=1>
<tr>
<th><b><font face="verdana"><font size="2"><font color="Brown">a b c</font></b></th>
</tr>
<tr>
<td><b><font face="verdana"><font size="2"><font color="Black">x y z</font></b></td>
</tr>
</table>
</body></html>
 
I would put my awk script in a file named mytool.awk (with adding #!/usr/bin/awk -f and giving execution permission)

Then, in a directory containing my csv files, i would just write:
Bash:
for i in *;do mytool.awk $i>$i'.html';done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top