convert a text file output to a html format using bash - bash

HI am trying to convert a text file to html with table so that i could mail the output in a table format and i used awk 'BEGIN{print "Content-Type: text/html; charset="us-ascii""\n "<html>"\n "<Body>"\n "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print \n</Body>"\n "</html>"\n"</table>"}' a.txt >> email.html but am having problems could some one help me on this

You need to clean your line. The \n need to be in double quote like this:
awk '
BEGIN{
print "Content-Type: text/html; charset=us-ascii\n <html>\n <Body>\n<table>"
}
{print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i"</td>"
print "</tr>"
}
END{
print "\n</Body>\n</html>\n</table>"
}' a.txt >> email.html

-Edited- It works with this:
awk '
BEGIN{
print "Content-Type: text/html; charset="us-ascii"\n<html>\n<head>\n<style>\ntable , th,td\n{\n border:1px solid black;
border-collapse:collapse;\n}\n</style>\n</head>\n<Body>\n<table>"
}
{print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i"</td>"
print "</tr>"
}
END{
print "\n</table>\n</Body>\n</html>\n"
}' a.txt >> email.html

Related

Read Multiple files in awk and print each file separately one below another

I have a requirment to read multiple files as input and print each of them in a table format one below another.
My code is like below to send mail,
echo "$(<file1)" | awk -F',' 'BEGIN{print "<table border=1 cellpadding=1 cellspacing=0 bordercolor=BLACK >"}{ print "<tr><td>"$1"</td><td>"$2"</td></tr>"} END { print "</table>" } ' | /usr/sbin/sendmail -t
file1:
101|ABC|XXX
102|CAF|YYY
file2:
104|RRR|gfd
106|ytr|rte
Output should be a table format like read above files and do some formatting then send mail with tables one below another.
Output in tabular format:
Table
Table
Could you please try following, based on your shown samples input and expected output written in GNU awk. I have written logic how to generate a proper html, properties are taken from OP's tried code itself, if any further editing needed for table etc then one could edit this accordingly too. You could take this output into a output file and then could use sendmail to send this by email too.
awk '
BEGIN{
FS="|"
print "<html>" ORS "<table border=1 cellpadding=1 cellspacing=0 bordercolor=BLACK >"
}
{
print "<tr>"
for(i=1;i<=NF;i++){
print "<td>"$i"</td>"
}
print "</tr>"
}
END{
print "</table>" ORS "</html>"
}' Input_file1 Input_file2

merge two csv files/Extract data using two csv file in shell(using awk)

I have two CSV files
csv 1:
a0,|a1|a2|a3|
b0,|b1|b2|b3|
c0,|c1|c2|c3|
csv 2:
a2,5
a3,7
c3,10
d6,3
b2,6
output(can be unsorted):
a0,a2(5);a3(7)
b0,b2(6)
c0,c3(10)
others,d6(3)
I tried using grep and do it. But I think it will be lengthy and time taking.
#My tried code
while IFS= read -r line
do
f=$(echo "$line" | cut -d, -f1)
f1=$(echo "$line" | cut -d, -f2)
echo "$(cat csv1.csv | grep "|$f|" | cut -d, -f1),$f($f1)" >> output
done < csv2.csv
Output something I am able to produce
a0,a2(5)
a0,a3(6)
c0,c3(6)
Can anyone help me to achieve the output expected in shell script??
EDIT: Since OP has changed delimiters so adding this solution now(written and tested with shown samples).
awk '
FNR==NR{
gsub(/^ +| +$/,"",$3)
a[$1]=$2
next
}
{
num=split($2,array,"|")
}
{
for(i=1;i<=num;i++){
if(array[i] in a){
val=(val?val ";":"")array[i] "(" a[array[i]] ")"
}
}
$2=val
val=""
}
1
' FS="," OFS="," file2 file1
Could you please try following.
awk '
FNR==NR{
gsub(/^ +| +$/,"",$3)
a[$2]=$3
next
}
FNR==1{
print
next
}
{
num=split($3,array,",")
}
{
for(i=1;i<=num;i++){
if(array[i] in a){
val=(val?val ",":"")array[i] "(" a[array[i]] ")"
}
}
$3=val
val=""
}
1
' FS="|" OFS="|" Input_file2 Input_file1

Print in Table based on condition

Below is my code for creating the table
awk -v server=${svr} 'BEGIN{
FS=","
print "=============================="
printf "<h3>ServerName:%s</h3>", server
print "=============================="
print "<HTML>""<TABLE border="1">"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
{
printf "%s", "<td"
if ($i+0==3) printf " bgcolor=#FF3333"
print ">" $i "
elif print ">" ok " bgcolor=#99FF33"</td>"
}
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
Here what i am trying is if $i+0 is 3 then only print the values in table else if non of the value is 3 then it should print $i+0 value as "OK" with a bgcolor.
As you see, i was trying something using elif but doesn't seems to be working.
Please let me know how can it be done
By seeing your code more carefully I have fixed following problems in it now.
Changed line print ">" $i " TO print > "$i", else it may give syntax error not sure you have tested this code or only written it.
Added > to line printf "%s", "<td".
Added </td>in if condition after bg value to complete cell of that specific row of table.
Changed this(print ">" ok " bgcolor=#99FF33"</td>") TO print "> ok bgcolor=#99FF33</td>" first I mis-understood that you want to user variable in printing but seems you only want to print statement to console.
Changed elif TO else to print correct statement if(if statement condition is NOT TRUE).
Added ' after END BLOCK of awk code.
Mentioned Input_file name to code too.
Fixed formatting of your code for better understanding and making it look nicer :)
awk -v server=${svr} '
BEGIN{
FS=","
print "=============================="
printf "<h3>ServerName:%s</h3>", server
print "=============================="
print "<HTML>""<TABLE border="1">"
}
{
printf "<TR>"
for(i=1;i<=NF;i++){
printf "%s", "<td>"
if($i+0==3){
printf "bgcolor=#FF3333</td>"
print > "$i"
}
else{
print "> ok bgcolor=#99FF33</td>"
}
}
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' Input_file

awk not creating table . Only the tags are getting printed

Here awk is used with shell script. I am trying to print the fields in e.txt in a table format. This is the code used. But i am not getting a table as my output. The output is included below. How to get this code to print the table. should something else be included.
e.txt
5 a 678
4 b 7899
Shell script using awk
awk 'BEGIN {print "<table>"}
{print "<tr><td>" $1 "</td><td>" $2 "</td></tr>"}
END {print "</table>"}' /home/scripts/e.txt
Output:
<table>
<tr><td>5</td><td>a</td></tr>
<tr><td>4</td><td>b</td></tr>
</table>
I need to print it in the form of a table with some styles .
Could you please try following code and let us know.
awk 'BEGIN{print "<html>" ORS "<table>"} {print "<tr><td>" $1 "</td><td>" $2 "</td></tr>"} END{print "</table>" ORS "</html>"}' Input_file > test.html

awk: using escape characters in for statement

I have the following awk command which works perfectly:
awk 'BEGIN { ORS = " " } {print 22, $1, NR; for(i=2;i<=NF;++i) print $i}{print "\n"}' file
What it does is insert two columns: the value 22 as the first, and the nr of the row as the third (see this question).
I tried running this command in a for loop as follows:
for p in 20 21 22
do
awk 'BEGIN { ORS = " " } {print $p, \$1, NR; for(i=2;i<=NF;++i) print \$i}{print "\n"}' file > file$p
done
So instead of the 22 in the first column, 3 files are made where each file has 20, 21 or 22 as the first column. Unfortunately, this doesn't work and I get the message: ^ backslash not last character on line.
It probably has something to do with how I escaped the $ characters, but I don't know how else to do it... any ideas?
You can assign a shell-parameter to awk using
-v parameter=value
i.e. use
awk -v p=$p ...
Like this:
for p in 20 21 22
do
awk -v p=$p 'BEGIN { ORS = " " } [TOO LONG LINE]' file > file$p
done
Complete command:
for p in 20 21 22; do awk -v p=$p 'BEGIN { ORS = " " } {print p, $1, NR; for(i=2;i<=NF;++i) print $i}{print "\n"}' file > file$p; done
You are in the right direction. Because your awk script is within ' (single quotes), bash doesn't replace $p with its value in the script (See Advanced Bash-Scripting Guide - Quoting).
You need to do one of the following:
1 (Recommened):
for p in 20 21 22
do
awk -v p=$p 'BEGIN { ORS = " " } {print p, \$1, NR; for(i=2;i<=NF;++i) print \$i}{print "\n"}' file > file$p
done
2: Use " instead of ' and within the awk escape the " by \"

Resources