gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:01 25 4
gpt4 key购买 nike

我试图用双引号将 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

最佳答案

你有太多引述,你可能不想用逗号分隔要打印的各个项目:

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

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

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

关于linux - 在输出字段周围添加双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com