我有以下awk
代码。它首先确定标题行中感兴趣的字段号,然后打印这些字段的输出。问题是我使用关联数组来保留感兴趣的字段编号,因此我在循环中打印出来,而不是for
使用.它的问题是它还在行尾添加了分隔符。print
printf
有没有什么简单的方法可以解决这个问题(没有例如再次运行输出并删除额外的分隔符)?或者是否有更好的方法来打印出其编号存储在数组中的字段?
awk '
BEGIN {
FS = ","; OFS = ","
no_headers = 2; headers[1] = "header1"; headers[2] = "header3"
k = 0
}
NR==1 {
for (i=1; i<=no_headers; i++) {
for (j=1; j<=NF; j++) {
s = gensub(/"/, "", "g", $j)
if (s==headers[i]) { col_no[++k] = j }
}
}
}
NR>1 {
for (i=1; i<=k; i++) { printf "%s,", $col_no[i] }
print ""
}' test_awk.txt
测试文件如下所示
"header1","header2","header3","header4"
"a","b","c",4
1,"b",,"d"
"a","2","c","d"
我目前的输出看起来像这样
"a","c",
1,,
"a","c",
但我希望它看起来像这样(IE,没有尾随分隔符)
"a","c"
1,
"a","c"
答案1
修改您的语句以从格式说明符中printf()
删除并将其添加到该部分内。使用三元运算符附加if not 到最后一个字段,
{..}
,
printf "%s", (i==k) ? $col_no[i] : $col_no[i]","