在输出字段周围添加双引号

时间:2018-02-05 11:20:40

标签: linux awk

我正试图用双​​引号括住awk输出字段但是仍然遇到错误。此代码在Ubuntu系统上的bash脚本中运行

错误

awk: cmd. line:1: BEGIN {print "<table id="\"alert\"",">"} ; {  print "<tr><td class="\"site\"",">" $1 "</td><td class="\"fdate\"",">" $2 "</td><td class="\"prcp\"",">" $3 "</td><td class="\"snow\"",">" $4 "</td><td class="\"tmp2m\"",">" $5 "</td><td class="\"dpt2m\"",">" $6 "</td><td class="\"wchill\"",">" $7 "</td><td class="\"wind10m\"",">" $8 "</td></tr>"} ; END { print "</table>"}
awk: cmd. line:1:                          ^ backslash not last character on line
awk: cmd. line:1: BEGIN {print "<table id="\"alert\"",">"} ; {  print "<tr><td class="\"site\"",">" $1 "</td><td class="\"fdate\"",">" $2 "</td><td class="\"prcp\"",">" $3 "</td><td class="\"snow\"",">" $4 "</td><td class="\"tmp2m\"",">" $5 "</td><td class="\"dpt2m\"",">" $6 "</td><td class="\"wchill\"",">" $7 "</td><td class="\"wind10m\"",">" $8 "</td></tr>"} ; END { print "</table>"}
awk: cmd. line:1:                          ^ syntax error

尝试过的代码

awk -F, 'BEGIN {print "<table id="\"alert\"",">"} ; {  print "<tr><td class="\"site\"",">" $1 "</td><td class="\"fdate\"",">" $2 "</td><td class="\"prcp\"",">" $3 "</td><td class="\"snow\"",">" $4 "</td><td class="\"tmp2m\"",">" $5 "</td><td class="\"dpt2m\"",">" $6 "</td><td class="\"wchill\"",">" $7 "</td><td class="\"wind10m\"",">" $8 "</td></tr>"} ; END { print "</table>"}' /home/weather/csv > /home/weather/csv.html

2 个答案:

答案 0 :(得分:2)

你的报价太多了,你可能不想用逗号分隔要打印的单个项目:

$ awk -F, 'BEGIN {print "<table id=\"alert\"" ">"}'
<table id="alert">

有时将双引号设置为变量有助于提高可读性:

$ awk -F, -v q='"' 'BEGIN {print "<table id=" q "alert" q ">"}'
<table id="alert">

答案 1 :(得分:1)

以下是你应该如何接近这个(未经测试):

drag