如何在HTML中使用awk格式化文本 [英] How to format text in html using awk
问题描述
cat table.txt
<$>
我想用awk将表格格式化为html格式。 p $ p> COL1 COL2 COL4 COL5 COL3
BRCC hete 15869 105A 1
BRAC he 1799967 956G 1
BCAS he 7334543 369AFVC 2
RCA he 9534262 7806 -14 4
RCA he 144848 1114A 5
RA he 206118 52A 5
BCAVV he 543304 3807TCD 5
BCA hoo 106091515 4308TDDSC 1
BCA hoo 206075 4563A 1
BCA呼799917 2612CDSA 1个
BCA呼206076 513G 2
BCA hoom 16941 3113A 3
$ p
awk'BEGIN {print< table>}}; {print< tr>< td width =80> $ 1< / td>< td width =80> $ 2< / td>< td width =150> $ 3< / td>< td width =150> $ 4< / td>< td> $ 5< / td>< tr>}; END {print< / table>}'table.txt
我有2个问题: / p>
1。问题: 我需要添加 ... 引用错误:
{print< table>}
字体样式:
awk'BEGIN {print< table style ='font-size:8.0pt; font-family:Verdana ,sans-serif>};
awk:cmd。行:1:BEGIN {打印 <表样式=字体大小:8.0pt
AWK:CMD线:1:^未终止的字符串
AWK:CMD线:1:BEGIN {打印。 < table style = font-size:8.0pt
awk:cmd。行:1:^语法错误
的zsh:没有这样的文件或目录:字体家庭:宋体,无衬线> 中}; {打印 < TR>< TD宽度= 80 >中$ 1< / td>< td width =80>$ 2< / td>< td width =150" $ 3" 150 >$ 4< / td>< td>$ 5< / td>< tr>}; END {print< / table>}
$ c $ 2,问题:
COL1 COL2 COL4 COL5 COL3
===========================
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3 col3
col1 col2 col4 col5 col3
========================== =
html
格式 awk'BEGIN {
split(80,80,150,150,,widths,,)
print< style> \
.my_table {font-size:8.0pt; FONT-FAMILY:\ Verdana\,\ SANS-serif\; border-bottom:3px双黑色;边界崩溃:崩溃; } \\\
\
.my_table tr.header {border-bottom:3px double black;} \\\
\
.my_table th {text-align:left;} \
< / style>
print< table class = \my_table\>
NR == 1 {
print< tr class = \header \>
tag =th
}
NR!= 1 {
print< tr>$ b $对于(i = 1; i <= NF; ++ i),打印<标签宽度=宽度b标签=td
} i]\>$ i< /tag>
print< / tr>
END {print< / table> ;}'table.txt> table.html
你得到 table.html
,
I would like to format my table in to html format using awk.
cat table.txt
COL1 COL2 COL4 COL5 COL3
BRCC hete 15869 105A 1
BRAC he 1799967 956G 1
BCAS he 7334543 369AFVC 2
RCA he 9534262 7806-14 4
RCA he 144848 1114A 5
RA he 206118 52A 5
BCAVV he 543304 3807TCD 5
BCA hoo 106091515 4308TDDSC 1
BCA hoo 206075 4563A 1
BCA hoo 799917 2612CDSA 1
BCA hoo 206076 513G 2
BCA hoom 16941 3113A 3
My awk solution:
awk 'BEGIN {print "<table>"} ; { print "<tr><td width="80">" $1 "</td><td width="80">" $2 "</td><td width="150">" $3 "</td><td width="150">" $4 "</td><td>" $5 "</td><tr>"} ; END { print "</table>"}' table.txt
I Have 2 Problems:
1. Problem:
I need to add {print "<table>"}
font style like:
awk 'BEGIN {print "<table style='font-size:8.0pt;font-family:"Verdana","sans-serif"'>"} ;
...
But there is a error with quoting:
awk: cmd. line:1: BEGIN {print "<table style=font-size:8.0pt
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: BEGIN {print "<table style=font-size:8.0pt
awk: cmd. line:1: ^ syntax error
zsh: no such file or directory: font-family:Verdana,sans-serif>"} ; { print "<tr><td width="80">" $1 "</td><td width="80">" $2 "</td><td width="150">" $3 "</td><td width="150">" $4 "</td><td>" $5 "</td><tr>"} ; END { print "</table>"}
2. Problem:
And I would like to add "borders" to my table with "======" mark. So output look like:
COL1 COL2 COL4 COL5 COL3
===========================
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
col1 col2 col4 col5 col3
===========================
A solution using awk
to generate table in html
format
awk 'BEGIN {
split("80,80,150,150,", widths, ",")
print "<style>\
.my_table {font-size:8.0pt; font-family:\"Verdana\",\"sans-serif\"; border-bottom:3px double black; border-collapse: collapse; }\n\
.my_table tr.header{border-bottom:3px double black;}\n\
.my_table th {text-align: left;}\
</style>"
print "<table class=\"my_table\">"
}
NR == 1{
print "<tr class=\"header\">"
tag = "th"
}
NR != 1{
print "<tr>"
tag = "td"
}
{
for(i=1; i<=NF; ++i) print "<" tag " width=\"" widths[i] "\">" $i "</" tag ">"
print "</tr>"
}
END { print "</table>"}' table.txt > table.html
you get in table.html
,
这篇关于如何在HTML中使用awk格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!