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