Converting text file to html page


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Converting text file to html page
# 1  
Old 07-31-2017
Converting text file to html page

Hello Everyone,

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

Text file

Code:
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
Code:
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 .

Code:
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

Code:
#!/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.

Code:
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


Last edited by nextStep; 07-31-2017 at 12:35 PM.. Reason: Formatting the output
# 2  
Old 07-31-2017
Code:
#!/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.

Code:
<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
Code:
exec 1>outputfile

at the top of the script.
# 3  
Old 07-31-2017
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".

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


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

# 4  
Old 07-31-2017
My code works.

Image

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.
# 5  
Old 07-31-2017
Nothing i have changed.The code is same as given

Code:
#!/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"

Code:
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.
# 6  
Old 07-31-2017
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:
Code:
#!/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"

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 07-31-2017
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.
This User Gave Thanks to nextStep For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting data for text file to csv

Gents Using the script attached (raw2csv). i use to create the file .csv.. The input file is called 201.raw. Kindly can you check if there is easy way to do it. The script works fine but takes a lot time to process Thanks for your help (8 Replies)
Discussion started by: jiam912
8 Replies

2. Shell Programming and Scripting

Format problem while converting text file to csv

Hi , I need a help in following scenario.I tried searching in google but couldn't able to find the exact answer. Sorry if i am re-posting already answered query. While i am trying to convert into log file into csv i couldn't able to get the format which i am looking for. I converted file... (4 Replies)
Discussion started by: varmas424
4 Replies

3. Shell Programming and Scripting

Converting text file in a matrix

Hi All, I do have a file with many lines (rows) and it is space delimited. For example: I have a file named SR345_pl.txt. If I open it in an editor, it looks like this: adfr A2 0.9345 dtgr/2 A2 0.876 fgh/3 A2 023.76 fghe/4 A2 2345 bnhy/1 A3 3456 bhy A3 0.9876 phy A5 0.987 kdrt A5... (9 Replies)
Discussion started by: Lucky Ali
9 Replies

4. UNIX for Dummies Questions & Answers

Converting a text file with irregular spacing into a space delimited text file?

I have a text file with irregular spacing between values which makes it really difficult to manipulate. Is there an easy way to convert it into a space delimited text file so that all the spaces, double spaces, triple spaces, tabs between numbers are converted into spaces. The file looks like this:... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

Converting columns of a text file.

Not sure the most effient way to do this. I have figiured out how to extract columns with shell script, but not sure how to convert This is what I have... NEWDNS 04-Jun-2011 06:00:59.762 10.220.136.217 crl.verisign.com This is what I need.... Change date, remove mil seconds,... (1 Reply)
Discussion started by: mrlayance
1 Replies

6. UNIX for Dummies Questions & Answers

Possible to download web page's text to a file?

Hi, Say there is a web page that contains just text only - that is, even the source code is just the text itself, nothing more. An example would be "http://mynasadata.larc.nasa.gov/docs/ocean_percent.txt" Is there a UNIX command that would allow me to download this text and store it in a... (1 Reply)
Discussion started by: Breanne
1 Replies

7. Shell Programming and Scripting

Converting a text file to HTML

Hi, I need to convert a text file formatted like this ("tshark -z conv,ip" output) to HTML: ===================================================================================================== IPv4 Conversations Filter:<No Filter> | <- ... (4 Replies)
Discussion started by: ph0enix
4 Replies

8. UNIX for Dummies Questions & Answers

How do I extract text only from html file without HTML tag

I have a html file called myfile. If I simply put "cat myfile.html" in UNIX, it shows all the html tags like <a href=r/26><img src="http://www>. But I want to extract only text part. Same problem happens in "type" command in MS-DOS. I know you can do it by opening it in Internet Explorer,... (4 Replies)
Discussion started by: los111
4 Replies

9. UNIX for Dummies Questions & Answers

Converting HTML to CSV

Hi, I need to convert a relatively large html file (1.5megs) into CSV under Unix. How would I be able to do this? Much thanks. (3 Replies)
Discussion started by: Jexel
3 Replies

10. Shell Programming and Scripting

linking unix generated text file to html page

i am trying to link the text files that i generated from my shell script to an html page, to that i can view them using a browser, like internet explorer. i want to open the text files in html page when i enter a command to view the text file from the shell command. please could anyone help... (1 Reply)
Discussion started by: alexd
1 Replies
Login or Register to Ask a Question