我正在尝试awk第一个column1,然后使用sort -u和使用ask和Ksh创建带有column1值的文件,
for i in `cat /u01/test/test.csv | awk -F , '{print $1}' | sort -u`
do
{
log=/u01/test
CLS="LEFT"
cat $log/test.csv|grep $i|awk -v v1=$log -v v3=$CLS -v v4=$i '{FS = ","}
{printf("\<TR class\=%s height\=22\>\n",v3) >> v1/"${v4}-test.html";}
{if ($2 == "ENG" || $2 == "HIN")
{printf("\<TD class\=yellow align\=left height\=\"17\" width\=\"10\%%\"\>%s \<\/TD\>\n",$1) >> v1/"${v4}-test.html";
printf("\<TD class\=yellow align\=left height\=\"17\" width\=\"10\%%\"\>%s \<\/TD\>\n",$2) >> v1/"${v4}-test.html";
printf("\<TD class\=yellow align\=left height\=\"17\" width\=\"10\%%\"\>%s \<\/TD\>\n",$3) >> v1/"${v4}-test.html"}
else
{
printf("\<TD class\=%s align\=left height\=\"17\" width\=\"10\%%\"\>%s\<\/TD\>\n",v3,$1) >> v1/"${v4}-test.html";
printf("\<TD class\=%s align\=left height\=\"17\" width\=\"10\%%\"\>%s\<\/TD\>\n",v3,$2) >> v1/"${v4}-test.html";
printf("\<TD class\=%s align\=left height\=\"17\" width\=\"10\%%\"\>%s\<\/TD\>\n",v3,$3) >> v1/"${v4}-test.html"}}
{if (v3~"RED"){v3 = "YELLOW"} else {v3 = "RED"}}
{printf("\<\/TR\>\n") >> v1/{v4}-test.html}'
}
done
但这不是使用column1唯一值创建文件
示例输入
LANG,ENG,HIN
REG,美国,得克萨斯州
LANG,ENG,SIB
REG,英国,欧洲
示例输出
两个输出文件名为LANG-test.html和REGI-test.html
LANG-test.html的内容
<TR Class=CLS height=22>
<TD class=yellow align=left height=17 width=10>LANG</TD>
<TD class=yellow align=left height=17 width=10>ENG</TD>
<TD class=yellow align=left height=17 width=10>HIN</TD>
<TD class=yellow align=left height=17 width=10>LANG</TD>
<TD class=yellow align=left height=17 width=10>ENG</TD>
<TD class=yellow align=left height=17 width=10>SIB</TD>
</TR>
和其他名称为REG-test.html的文件
<TR Class=CLS height=22>
<TD class=yellow align=left height=17 width=10>REG</TD>
<TD class=yellow align=left height=17 width=10>USA</TD>
<TD class=yellow align=left height=17 width=10>TEXAS</TD>
<TD class=yellow align=left height=17 width=10>LANG</TD>
<TD class=yellow align=left height=17 width=10>UK</TD>
<TD class=yellow align=left height=17 width=10>EUR</TD>
</TR>
试图找出printf语句中的错误beacuse awk无法解析printf中的行,其次基于唯一的第1列重定向输出具有挑战性
答案 0 :(得分:0)
这是一个仅使用awk运行的版本。关于细节的评论太多了,所以我就写了。
log=/u01/test
CLS="LEFT"
awk -F, -v v1=$log -v v3=$CLS '
{ printf("<TR class=%s height=22>\n",v3) >> v1"/"$1"-test.html";
if ($2 == "ENG" || $2 == "HIN") {
printf("<TD class=yellow align=left height=\"17\" width=\"10%\">%s </TD>\n",$1) >> v1"/"$1"-test.html";
printf("<TD class=yellow align=left height=\"17\" width=\"10%\">%s </TD>\n",$2) >> v1"/"$1"-test.html";
printf("<TD class=yellow align=left height=\"17\" width=\"10%\">%s </TD>\n",$3) >> v1"/"$1"-test.html" }
else {
printf("<TD class=%s align=left height=\"17\" width=\"10%\">%s</TD>\n",v3,$1) >> v1"/"$1"-test.html";
printf("<TD class=%s align=left height=\"17\" width=\"10%\">%s</TD>\n",v3,$2) >> v1"/"$1"-test.html";
printf("<TD class=%s align=left height=\"17\" width=\"10%\">%s</TD>\n",v3,$3) >> v1"/"$1"-test.html" }
if (v3~"RED") { v3 = "YELLOW" } else {v3 = "RED"}
printf("</TR>\n") >> v1"/"$1"-test.html"
}' $log/test.csv
请注意字符串连接,变量用法和转义字符的更改。 for循环已经消失,因为它没有任何意义。希望这会有所帮助。