Computing.Net > Forums > Unix > How to convert text file to html

How to convert text file to html

Reply to Message Icon

Original Message
Name: mm1609
Date: March 10, 2003 at 18:26:55 Pacific
Subject: How to convert text file to html
OS: UNIX
CPU/Ram: 255
Comment:

Hi,

I would like to know how to convert a text file in UNIX to HTML using awk?

For example,
my file has the following contents :

Name Age
A 23
B 16
C 60
D 29

How to output as html as a table?

Thanks!
mm


Report Offensive Message For Removal


Response Number 1
Name: anukta_c
Date: March 11, 2003 at 08:32:26 Pacific
Subject: How to convert text file to html
Reply: (edit)

Hi,
If I understand correctly, you want to convert a columnar textfile to an HTML table.
The code below will do that:

echo "Type name of file to be converted"
read infile
outfile=$infile.html
echo "<table border=1>" > $outfile
cat $infile|while read line
do
echo "<tr>" >> $infile.html
echo $line|awk -v newfile=$outfile '{
for(i=1;i<=NF;i++)
{
print("<td>" $i "</td>") >> newfile
}
}'
echo "</tr>" >> $outfile
done
echo "</table>" >> $outfile

I am not sure if this site will let me post the html tags literally....I am giving it a try...
Anukta


Report Offensive Follow Up For Removal

Response Number 2
Name: anukta_c
Date: March 11, 2003 at 08:49:18 Pacific
Subject: How to convert text file to html
Reply: (edit)


Line no 7 should have been

echo "<tr>" >> $outfile

This is very basic HTML table, u might want to modify this to suit your requirement.
-Anukta


Report Offensive Follow Up For Removal

Response Number 3
Name: deeps
Date: March 11, 2003 at 09:56:13 Pacific
Subject: How to convert text file to html
Reply: (edit)

Hi,
I am basically making use of Anukta's code only but with a few changes, in way that i am avoiding the read of the file using cat command, rather i am using awk directly to interpret lines.

Here it is:

#!/bin/ksh
read infile?'Type name of file to be converted: '
outfile=$infile.html
nawk -v newfile=$outfile 'BEGIN{\
print "" > newfile
}
{
print "" >> newfile
for(i=1;i" $i "") >> newfile
}
print "" >> newfile
}
END{print "" >> newfile }' $infile

The difference between the code will be noticeable if you input file is large, else remains the same.

One more thing i am using nawk instead of awk as some machines dont understand the -v option, due to the older version of awk.

~deeps



Report Offensive Follow Up For Removal

Response Number 4
Name: deeps
Date: March 11, 2003 at 10:04:15 Pacific
Subject: How to convert text file to html
Reply: (edit)

well, i should have known that .... :-)

Let's try again....


#!/bin/ksh
read infile?'Type name of file to be converted: '
outfile=$infile.html
nawk -v newfile=$outfile 'BEGIN{\
print "

" > newfile
}
{
print "
" >> newfile
for(i=1;i<=NF;i++)
{
print("
") >> newfile
}
print "
" >> newfile
}
END{print "
" $i "
" >> newfile }' $infile

hope this works....

Someone should take Anukta's idea and change the code on this web page, as it gets too slow to give an answer with those signs :)

~deeps


Report Offensive Follow Up For Removal

Response Number 5
Name: deeps
Date: March 11, 2003 at 10:05:49 Pacific
Subject: How to convert text file to html
Reply: (edit)

well, i should have known that .... :-)

Let's try again....


#!/bin/ksh
read infile?'Type name of file to be converted: '
outfile=$infile.html
nawk -v newfile=$outfile 'BEGIN{\
print "<table border=1>" > newfile
}
{
print "<tr>" >> newfile
for(i=1;i<=NF;i++)
{
print("<td>" $i "</td>") >> newfile
}
print "</tr>" >> newfile
}
END{print "</table>" >> newfile }' $infile

hope this works....

Someone should take Anukta's idea and change the code on this web page, as it gets too slow to give an answer with those signs :)

~deeps


Report Offensive Follow Up For Removal


Response Number 6
Name: deeps
Date: March 11, 2003 at 10:07:27 Pacific
Subject: How to convert text file to html
Reply: (edit)

This was an ugly experience :-(



Report Offensive Follow Up For Removal

Response Number 7
Name: mm
Date: March 11, 2003 at 17:13:06 Pacific
Subject: How to convert text file to html
Reply: (edit)

Thanks all for your solutions!:)


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to convert text file to html

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge