Converting text file to html page

Hello Everyone,

I have the following text file with the each field separated by newline

Text file

P
file1-en-us_US-20170718T150157Z.json
Wed 19 Jul 2017 06:10:13 AM EDT
P
file2-en-us_US-20170718T160150Z.json
Wed 19 Jul 2017 06:10:13 AM EDT
P
file3-en-us_US-20170718T163218Z.json
Wed 19 Jul 2017 06:10:13 AM EDT
P
file4-en-us_US-20170718T170151Z.json
Wed 19 Jul 2017 06:10:13 AM EDT

Now i want to convert the above text file into html.I have researched and written the below code but the output is not in the desired format.

Desired output

Header  Filename                                                          Timestamp
P           file1-en-us_US-20170718T150157Z.json    Wed 19 Jul 2017 06:10:13 AM EDT

P          file2-en-us_US-20170718T160150Z.json       Wed 19 Jul 2017 06:10:13 AM EDT

The output now is broken .

Header	Filename	TimeStamp
P
file1-e	-us_US-20170718T150157Z.jso
Wed 19 Jul 2017 06:10:13 AM EDT
P
file2-e	-us_US-20170718T160150Z.jso
Wed 19 Jul 2017 06:10:13 AM EDT
P
file3-e	-us_US-20170718T163218Z.jso
Wed 19 Jul 2017 06:10:13 AM EDT
#!/bin/sh
FILES="/home/user/timestamp.txt"
for file in $FILES   ; do
       echo "files are "  $file
       html=$(echo $file | sed 's/\.txt$/\.html/i')

       echo "<html>" >> $html
       echo "<style type="text/css">
            table, th, td {
            border: 1px solid black;
            }
            </style>" >> $html
       echo "   <body>" >> $html
       echo '<table>' >> $html
       echo '<th>Header</th>' >> $html
       echo '<th>Filename</th>' >> $html
       echo '<th>TimeStamp</th>' >> $html

       while IFS='\n' read -ra line ; do
        echo "here"
        echo "<tr>"  >> $html
        for i in "${line[@]}"; do
           echo "<td>$i</td>" >> $html

          done
         echo "</tr>"  >> $html
       done < $file
        echo '</table>'
        echo "   </body>" >> $html
        echo "</html>" >> $html
    done


Could you please help on getting the results .

Adding to that i formatted the out put in the below fashion, but still the output is displayed in the above fashion.

P	file1.json	Wed 19 Jul 2017 06:10:13 AM EDT	
P	file2.json	Wed 19 Jul 2017 06:10:13 AM EDT	
P	file3.json	Wed 19 Jul 2017 06:10:13 AM EDT	
#!/bin/sh

FILES="data"

echo "<html>
<style type='text/css'>
            table, th, td {
            border: 1px solid black;
            }
</style>
<body>
<table><tr><th>Header</th><th>Filename</th><th>TimeStamp</th></tr>"

for FILE in $FILES
do
        exec 0<$FILE

        while read H && read F && read D
        do
                printf "<tr>"
                printf "<td>%s</td>" "$H" "$F" "$D"
                printf "</tr>\n"
        done
done

printf "</table></body></html>\n"

Note the giant text block enclosed in one set of double quotes with no double quotes inside it.

<html>
<style type='text/css'>
            table, th, td {
            border: 1px solid black;
            }
</style>
<body>
<table><tr><th>Header</th><th>Filename</th><th>TimeStamp</th></tr>
<tr><td>P</td><td>file1-en-us_US-20170718T150157Z.json</td><td>Wed 19 Jul 2017 06:10:13 AM EDT</td></tr>
<tr><td>P</td><td>file2-en-us_US-20170718T160150Z.json</td><td>Wed 19 Jul 2017 06:10:13 AM EDT</td></tr>
<tr><td>P</td><td>file3-en-us_US-20170718T163218Z.json</td><td>Wed 19 Jul 2017 06:10:13 AM EDT</td></tr>
<tr><td>P</td><td>file4-en-us_US-20170718T170151Z.json</td><td>Wed 19 Jul 2017 06:10:13 AM EDT</td></tr>
</table></body></html>

No need to redirect into $html 500 times, just redirect the output of the script, or do

exec 1>outputfile

at the top of the script.

Thanks , but i got everything in one cell. i need the information into three cell ie header should have value "P", filename should have "file1.en-us_US-20170718T150157Z.json" and timestamp should have "Wed 19 Jul 2017 06:10:13 AM EDT".

Header	Filename	TimeStamp
P	file1-en-us_US-20170718T150157Z.json	Wed 19 Jul 2017 06:10:13 AM EDT
Header   Filename                                                 Timestamp
P            file1-en-us_US-20170718T150157Z.json   Wed 19 Jul 2017 06:10:13 AM EDT

My code works.

Either you changed it, or your input data is different from what you show, or there's something odd about the way you viewed it.

Show your code and exactly how you use it and exactly the output you get, word for word, letter for letter, keystroke for keystroke.

Nothing i have changed.The code is same as given

#!/bin/sh

exec 1>email.html

FILES="timestamp.txt"

echo "<html>
<style type='text/css'>
            table, th, td {
            border: 1px solid black;
            }
</style>
<body>
<table><tr><th>Header</th><th>Filename</th><th>TimeStamp</th></tr>"

for FILE in $FILES
do
        exec 0<$FILE

        while read H && read F && read D
        do
                printf "<tr>"
                printf "<td>%s</td>" "$H" "$F" "$D"
                printf "</tr>\n"
        done
done

printf "</table></body></html>\n"
cat timestamp.txt
P       file1-en-us_US-20170718T150157Z.json    Wed 19 Jul 2017 06:10:13 AM EDT
P       file2-en-us_US-20170718T160150Z.json    Wed 19 Jul 2017 06:10:13 AM EDT
P       file3-en-us_US-20170718T163218Z.json    Wed 19 Jul 2017 06:10:13 AM EDT
P       file4-en-us_US-20170718T170151Z.json    Wed 19 Jul 2017 06:10:13 AM EDT

Where i went wrong.

You originally posted one field per line, not one record per line. You specifically said each field was separated by a newline, also.

Obviously, they are not. Modify my code to:

#!/bin/sh

exec 1>email.html

FILES="timestamp.txt"

echo "<html>
<style type='text/css'>
            table, th, td {
            border: 1px solid black;
            }
</style>
<body>
<table><tr><th>Header</th><th>Filename</th><th>TimeStamp</th></tr>"

for FILE in $FILES
do
        while read H F D
        do
                printf "<tr>"
                printf "<td>%s</td>" "$H" "$F" "$D"
                printf "</tr>\n"
        done <$FILE
done

printf "</table></body></html>\n"
1 Like

Thanks a lot for the wonderful script,it worked.Apologies for the early mistake.
BTW are the variables H F D system variables or is it just user defined variables, and what was the difference earlier
#exec 0<$FILE
while read H && read F && read D

and now

while read H F D
done <$FILE

Much appreciated for the timely help.

1 Like

H F D are just variables, nothing special about them. I could have used almost any other alphanumeric name.

I wasn't quite sure if done <$FILE would work with three separate reads (one per line), so I redirected stdin itself to avoid needing any redirection later. With just one read, I know done <$FILE will work fine.

On thinking, it probably would have worked in the first version too. So, in the end, no real reason.

Hi Corona688,

The code you gave to me was extremely useful .Now when i incorporated in the main script all the results are coming under the "Header" cell

Header                                                                      Filename  Timestamp
P	file1-en-us_US-20170718T150157Z.json Wed 19 Jul 2017 06:10:13 AM EDT
P	file2-en-us_US-20170718T150157Z.json Wed 19 Jul 2017 06:10:13 AM EDT
P	file3-en-us_US-20170718T150157Z.json Wed 19 Jul 2017 06:10:13 AM EDT

There is no change in the source file

P	file1-en-us_US-20170718T150157Z.json	Wed 19 Jul 2017 06:10:13 AM EDT
P	file2-en-us_US-20170718T150157Z.json	Wed 19 Jul 2017 06:10:13 AM EDT
P	file3-en-us_US-20170718T150157Z.json	Wed 19 Jul 2017 06:10:13 AM EDT

I have few functions above the given function as below.Could you please advise what needs to be modified to get the records in each Cell like earlier.

fn_parsepccanfrenchJson()
{
#echo "inside parse3"
arr=( $(find /home/aunnikrishnan/pccanfrench/json/ -type f -name "*.json" | cut -d '/' -f6-))
len=${#arr[@]}
IFS=! timearr=( $(find /home/aunnikrishnan/pccanfrench/json/ -type f -name "*.json" -printf '%Tc!' ) )
timeArray=(`echo ${timearr}`)
for ((i=0; i<$len; i++))
do
        appVersion=$(cat "/home/aunnikrishnan/pccanfrench/json/${arr}"|jq '.' | grep "groupType" | awk -F ': ' '{print $2}' | sed 's/\"//g' | sed 's/ //g' | sed 's/,//')
        appArr=(`echo ${appVersion}`)

        UnixShell=("$appArr" "${arr}" "$timeArray" )
        printf '%s\t%s\t%s\t\n' "${UnixShell[@]}"  >> pccanfrench.txt
done
}
#fn_parsepccanfrenchJson

#ILES="pcusaenglish.txt"
#FILES="timestamp.txt"

fn_html()
{
FILES="timestamp.txt"
echo "<html>
<style type='text/css'>
            table, th, td {
            border: 1px solid black;
            }
</style>
<body>
<table><tr><th>Header:</th><th>Filename:</th><th>TimeStamp:</th></tr>"

for FILE in $FILES
do
        while read H F D
        do
                printf "<tr>"
                printf "<td>%s</td>" "$H" "$F" "$D"
                printf "</tr>"
        done <$FILE
done

printf "</table></body></html>\n"
}
fn_html

[/quote]

[/quote]

The script with two functions given in post#9 works exactly as expected when fed with the data files given. Corona688's comments in post#4 apply...