簡體   English   中英

合並兩行,當后續的行匹配模式時

[英]Combine 2 lines that when subsequent line match pattern

我正在嘗試將git日志輸出格式化為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腳本。

一種使用sed的方式假定您的輸入示例是infile的內容(由於\\s可能需要GNU sed 。如果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.

如果將整個文件加載到內存中,則很容易修復:

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'
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}
'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM