使用awk以svn log的模式输出

时间:2013-08-23 19:17:17

标签: bash shell svn sed awk

如何从svn log verbose中仅修改和添加带有修订版,作者和注释的文件:

猫测试:

r7351 | user01 | 2013-07-02 17:53:28 -0400 (Tue, 02 Jul 2013) | 2 lines
Changed paths:
    D /trunk/demo/proj1/.project

jira-125723
  Removing unwanted files

 ------------------------------------------------------------------------
 ------------------------------------------------------------------------
r7352 | user02 | 2013-07-02 17:54:24 -0400 (Tue, 02 Jul 2013) | 2 lines
Changed paths:
   D /trunk/demo/proj2/320-test.ert

jira-125723
Removing unwanted files
------------------------------------------------------------------------
------------------------------------------------------------------------
r7504 | user04 | 2013-07-08 14:26:36 -0400 (Mon, 08 Jul 2013) | 4 lines
Changed paths:
   M /trunk/demo/maven/Sum.jsp
   M /trunk/demo/Code/Results.jsp

jira-121639

WP-IQISU- lot of changes to fix issue
------------------------------------------------------------------------
------------------------------------------------------------------------
r7523 | user08 | 2013-07-08 16:17:52 -0400 (Mon, 08 Jul 2013) | 1 line
Changed paths:
   A /trunk/demo/ant/properties

jira-2185[/CODE]

My code and output:

cat test | awk '/^r[0-9]* \|/ { REVISION=$1; } /  [AM]/ { print REVISION": "$0; }'
r7504:    M /trunk/demo/maven/Sum.jsp
r7504:    M /trunk/demo/Code/Results.jsp
r7523:    A /trunk/demo/ant/properties[/CODE]

但我也需要作者和评论信息。 Desired output:

r7504:  user04 jira-121639  Modified /trunk/demo/maven/Sum.jsp     WP-IQISU- lot of changes to fix issue
r7504:  user04 jira-121639  Modified /trunk/demo/Code/Results.jsp  WP-IQISU- lot of changes to fix issue
r7523:  user08 jira-2185    Added    /trunk/demo/ant/properties    

2 个答案:

答案 0 :(得分:1)

你可以使用这个awk:

awk -F '[ |]+' '$1~/^r[0-9]+$/{R=$1;A=$2;next} !C{C++;M=$0} C && $2~/[AM]/{print R,A,$3,M}' OFS=': ' file
r7504: user04: /trunk/demo/maven/Sum.jsp: Changed paths:
r7504: user04: /trunk/demo/Code/Results.jsp: Changed paths:
r7523: user08: /trunk/demo/ant/properties: Changed paths:

答案 1 :(得分:1)

$ cat tst.awk
BEGIN{ RS="\n-+\n"; FS="\n"; OFS="\t" }
/\n   [MA]/ {
    sub(/^-+\n/,"")
    sub(/[[:space:]]+$/,"")

    split($1,hdr,/ \| /)

    numFiles=0
    for (i=3;$i;i++) {
        sub(/^ +M +/,"Modified" OFS,$i)
        sub(/^ +A +/,"Added" OFS,$i)
        file[++numFiles] = $i
    }

    user = $(i + 1)
    sub (/\[.*/,"",user)

    cmts = $(i + 3)

    for (fileNr=1;fileNr<=numFiles;fileNr++) {
        print hdr[1], hdr[2], user, file[fileNr], cmts
    }
}
$
$ awk -f tst.awk file
r7504   user04  jira-121639     Modified        /trunk/demo/maven/Sum.jsp       WP-IQISU- lot of changes to fix issue
r7504   user04  jira-121639     Modified        /trunk/demo/Code/Results.jsp    WP-IQISU- lot of changes to fix issue
r7523   user08  jira-2185       Added   /trunk/demo/ant/properties

以上使用GNU awk,因此我们可以将RS设置为多个字符。

为了帮助您理解它,为变量添加临时中间“打印”。要对输出的格式进行特定处理,而不仅仅是在提取的值man awk之间添加标签,并查看printf