INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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 

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

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.

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

Hi

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

Feherke.
http://feherke.github.com/

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

I would simply create from your code a function and call it for every file in file list, like this:
pavankiran56.sh

CODE

# ---------------- processing functions ----------------
create_html() {
 file=$1
 echo "** Now generating HTML for CSV-file '$file'" 
 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
}

process_file() {
  file=$1
  echo "*  Processing file: '$file' ..."
  create_html $file
}

# ---------------- main --------------------------------
files="CSV_file*.csv"
for file in $files; do
  process_file $file 
done
echo "*  Done." 

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> 

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

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:

CODE --> bash

for i in *;do mytool.awk $i>$i'.html';done 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close