无循环地提取多个列

时间:2009-09-28 11:30:29

标签: awk

我正在编写一个awk脚本,它将获取grep的输出并将其格式化为HTML表格。分隔符是“:”字符;我遇到的问题是该字符也可以出现在文本中。因此,如果我只分别使用$ 1,$ 2和$ 3作为文件名,行号和注释,我会在第一个之后丢失任何内容:在评论中

有没有办法说$ 1,$ 2,然后$ 3..NR而不显式循环列并将它们连接在一起?

到目前为止,这是脚本:

`

#!/usr/bin/awk

BEGIN {
    FS=":"

    print "<html><body>"
    print "<table>"
    print "<tr><td>File name</td><td>Line number</td><td>Comment</td></tr>"
}

{
    print "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td></tr>"
}
END {
    print "</table>"
    print "</body></html>"

}`

一些示例输入:

./mysql-connector-java-5.0.8/src/com/mysql/jdbc/BlobFromLocator.java:177:       // TODO: Make fetch size configurable
./mysql-connector-java-5.0.8/src/com/mysql/jdbc/CallableStatement.java:243:     // TODO Auto-generated method stub
./mysql-connector-java-5.0.8/src/com/mysql/jdbc/CallableStatement.java:836:     // TODO: Do this with less memory allocation

2 个答案:

答案 0 :(得分:1)

{ print gensub(/^[^:]*:[^:]*:/,"","g") }

答案 1 :(得分:1)

BEGIN {FS =“:”; OFS =“:”} {name = $ 1;数= $ 2; $ 1 = “”; $ 2 = “”;注释= SUBSTR($ 0.3); }