在后续行匹配模式时组合2行

时间:2012-02-24 19:14:18

标签: python perl sed awk

我正在尝试将git log输出格式化为asciidoc格式的更改日志。我已经使用git log --format完成了这项工作。接下来,我需要将提交消息中的错误号添加到主题中。

以下输入是使用

生成的
      git log --reverse --no-merges $1..$2 --format='* %s%n+%n%b' | \
      sed -e '/^Change-Id:.*$/d' | sed -e '/^Signed-off-by:.*$/d'

输入示例:

      * This is subject without issue number
      +
      There will be multiple lines of text and multiple paragraphs.

      2nd paragraph of the commit message.


      * This is commit with issue number
      +
      There can be multiple lines of comment message. 

      2nd paragraph of the commit message. A line with Bug: issue ### 
      will be the last line. I need to combine the issue ### with 
      the subject line.

      Bug: issue 1234

      * This is commit with issue number in Issue: 1235 format
      +
      There can be multiple lines of comment message. 

      2nd paragraph of the commit message. A line with Issue: ### 
      will be the last line. I need to combine the issue ### with 
      the subject line.

      Issue: 1235

预期产出

      * This is subject without issue number
      +
      There will be multiple lines of text and multiple paragraphs.

      2nd paragraph of the commit message.


      * issue 1234 This is commit with issue number
      +
      There can be multiple lines of comment message. 

      2nd paragraph of the commit message. A line with Bug: issue ### 
      will be the last line. I need to combine the issue ### with 
      the subject line.

      * issue 1235 This is commit with issue number in Issue: 1235 format
      +
      There can be multiple lines of comment message. 

      2nd paragraph of the commit message. A line with Issue: ### 
      will be the last line. I need to combine the issue ### with 
      the subject line.

我想知道是否可以使用Awk完成此操作。你能提供上面可以完成的Awk代码吗?如果没有,还有什么其他选择?我想创建一个生成所需输出的shell脚本。

3 个答案:

答案 0 :(得分:0)

使用sed的一种方法是假设您的输入示例是infile的内容(由于GNU sed可能需要\s。如果sed抱怨,请使用文字空格进行更改):

sed -n '/^\s*bug:/I ! { H ; b }; s/^[^:]*:// ; G ; s/\([^\n]*\)\n\(.*\*\)/\2\1/ ; s/^\n// ; p' infile

输出:

* This is subject without issue number
+
There will be multiple lines of text and multiple paragraphs.

2nd paragraph of the commit message.


* issue 1234 This is commit with issue number
+
There can be multiple lines of comment message. 

2nd paragraph of the commit message. A line with Bug: issue ### 
will be the last line. I need to combine the issue ### with 
the subject line.

说明:

-n                           # Disable printing.
/^\s*bug:/I ! {              # If line doesn't begin with 'bug' ignoring case.
  H                          # Append line to 'hold space'.
  b                          # Read next line.
}
s/^[^:]*://                  # Line beginning with 'bug': Remove part of line until a colon.
G                            # Get data of 'hold space'.
s/\([^\n]*\)\n\(.*\*\)/\2\1/ # Put bug line just before the commit message.
s/^\n//                      # Remove leading newline.
p                            # Print.

答案 1 :(得分:0)

如果将整个文件加载到内存中,则很容易修复:

s/^\*((?:(?!^\*).)*)\n^Bug: (issue \d+)\n/* $2$1/msg;

      ^^^^^^^^^^^^^
      Matches anything but "*" at the start of a line.

这正是如此:

perl -0777pe1's/^\*((?:(?!^\*).)*)\n^Bug: (issue \d+)\n/* $2$1/msg'

答案 2 :(得分:0)

awk '
    $1 == "*" {
        if (entry) {
            print subject
            print entry
            entry = ""
        }
        subject = $0
        next
    }
    $1 == "Bug:" {
        sub(/\*/, "* issue " $NF, subject)
        next
    }
    {entry = entry "\n" $0}
    END {print subject; print entry}
'