我的文件input.txt如下:
filename:test1.v
BUG: bug 102 is fixed by some user
IO_CHANGE: there is no io_change for this version
FEATURE: no feature added
filename:test2.v
BUG: bug 103 is fixed by some user
also bug 105 is fixed
IO_CHANGE: there is no io_change for this version
FEATURE: yes feature number 3 also feature 23
and feature 34 is added
filename:test3.v
BUG: bug 104 is fixed by some user
FEATURE: yes feature number 2
IO_CHANGE:
我的问题: - 有时会对BUG / FEATURE / IO_CHANGE有一个很长的描述,它有两行或有时在IO_CHANGE中没有任何内容,所以它是空白的。输出文件应包含所有错误的列表,然后是功能和io_changes。这三种类型可以在输入文件中以任何顺序排列,我需要从文件中找到所有错误/特征/ io_changes并按列列出它们。
答案 0 :(得分:1)
这个怎么样?我们将值存储在每个文件的数组中。在这里,我连接出现在多行上的条目。
awk 'function dump() {if (vc>0)
print fn, vals["BUG"], vals["FEATURE"], vals["IO_CHANGE"]
}
BEGIN {FS=":";OFS="\t";vc=0}
FNR==1 {dump();val=""; delete vals; fn=FILENAME; vc=0}
NF>1 {val=$1; vals[val]=vals[val] $2; vc++}
NF==1 {vals[val] = vals[val] " " $1}
END{dump()}' test*v
答案 1 :(得分:0)
如果找到了短语,则设置变量,如果发现其中一个短语未设置,则根据文件名将行保存到数组。
删除之前的所有内容:在每一行上
然后在列中打印行
#!/bin/bash
awk 'BEGIN{printf("%-8s%-60s%-60s%-20s\n\n","FILE","|BUG","|IO","|FEATURE")}
/BUG/{a=1}/IO_CHANGE:/ || /FEATURE/{a=0} {if (a){Bug[FILENAME]=Bug[FILENAME]""$0" "}}
/IO_CHANGE:/{b=1}/BUG/ || /FEATURE/{b=0} {if (b){IO[FILENAME]=IO[FILENAME]$0" "}}
/FEATURE/{c=1}/IO_CHANGE:/ || /BUG/{c=0} {if (c){Feat[FILENAME]=Feat[FILENAME]$0" "}}
END{
for (k in Bug){
Bug[k] = substr(Bug[k],index(Bug[k],":"))
IO[k] = substr(IO[k],index(IO[k],":"))
Feat[k] = substr(Feat[k],index(Feat[k],":"))
printf("%-8s%-60s%-60s%-20s\n\n","|"k,"|"Bug[k],"|"IO[k],"|"Feat[k])}}
' test*v
不幸的是,这不会为每个文件打印多行