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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to convert text file to html

Reply to Message Icon

Name: mm1609
Date: March 10, 2003 at 18:26:55 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: anukta_c
Date: March 11, 2003 at 08:32:26 Pacific
Reply:

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


0

Response Number 2
Name: anukta_c
Date: March 11, 2003 at 08:49:18 Pacific
Reply:


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


0

Response Number 3
Name: deeps
Date: March 11, 2003 at 09:56:13 Pacific
Reply:

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



0

Response Number 4
Name: deeps
Date: March 11, 2003 at 10:04:15 Pacific
Reply:

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


0

Response Number 5
Name: deeps
Date: March 11, 2003 at 10:05:49 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: deeps
Date: March 11, 2003 at 10:07:27 Pacific
Reply:

This was an ugly experience :-(



0

Response Number 7
Name: mm
Date: March 11, 2003 at 17:13:06 Pacific
Reply:

Thanks all for your solutions!:)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to convert text file to html

Convert a delimited text file to excel www.computing.net/answers/unix/convert-a-delimited-text-file-to-excel/2912.html

How to Read .txt file and edit www.computing.net/answers/unix/how-to-read-txt-file-and-edit/7947.html

How to convert .xls into txt file? www.computing.net/answers/unix/how-to-convert-xls-into-txt-file/7769.html