我怎样才能让 sed 删除一行然后忽略文件的测试?

我怎样才能让 sed 删除一行然后忽略文件的测试?

我正在使用以下命令从补丁中提取描述:

sed '
    s/Title: \(.*\)/### \1 ###\n\n**File:** FILE_NAME_HERE/
    /^diff\|^---/ {
        q
    }
' "$patch" | egrep -v '^(diff|---)'

我怎样才能摆脱该egrep -v '^(diff|---)'部分并仅使用 sed?我尝试这样做:

/^diff\|^---/ {
    d # <-- This is what I added
    q
}

但是一旦到达“d”,就会跳过“q”,并打印补丁主体中的其余行。以下是示例补丁:

Title: Font Array Support

Modifies st to support user-defined fallback fonts specified in an array
defined as `static const char *fonts[]`. This change also resolves an issue
where fallback fonts were used in place of default fonts in an inconsistent
manner which caused identical sets of text to sometimes use different fonts. In
the following example, DejaVu Sans Mono is the primary font with two others
specified as fallbacks:

    static const char *fonts[] = {
        "DejaVu Sans Mono",
        "VL Gothic",
        "WenQuanYi Micro Hei",
    };

diff --git a/st.c b/st.c
index 2594c65..f7973bd 100644
--- a/st.c
+++ b/st.c
@@ -353,10 +353,17 @@ typedef struct {
    FcPattern *pattern;
 } Font;

sed 脚本应该返回以“diff;”开头的行以上的所有内容,输出应该是这样的:

Title: Font Array Support

Modifies st to support user-defined fallback fonts specified in an array
defined as `static const char *fonts[]`. This change also resolves an issue
where fallback fonts were used in place of default fonts in an inconsistent
manner which caused identical sets of text to sometimes use different fonts. In
the following example, DejaVu Sans Mono is the primary font with two others
specified as fallbacks:

    static const char *fonts[] = {
        "DejaVu Sans Mono",
        "VL Gothic",
        "WenQuanYi Micro Hei",
    };

答案1

返回以“diff;”开头的行以上的所有内容

使用 sed

在这种情况下,请尝试:

sed '/^diff/,$d' a.patch

上述操作将删除从第一行与正则表达式匹配的行^diff到最后一行的所有行$

^diff当到达包含以下内容的行时,将退出稍微更高效的版本:

sed -n '/^diff/q; p' a.patch

使用您的示例文件:

$ sed -n '/^diff/q; p' a.patch
Title: Font Array Support

Modifies st to support user-defined fallback fonts specified in an array
defined as `static const char *fonts[]`. This change also resolves an issue
where fallback fonts were used in place of default fonts in an inconsistent
manner which caused identical sets of text to sometimes use different fonts. In
the following example, DejaVu Sans Mono is the primary font with two others
specified as fallbacks:

    static const char *fonts[] = {
        "DejaVu Sans Mono",
        "VL Gothic",
        "WenQuanYi Micro Hei",
    };

使用 awk

使用 awk:

awk '/^diff/{exit} 1' a.patch

1是 awk 中打印行的神秘简写。但是,当^diff到达与正则表达式匹配的第一行时,程序就会退出。

$ awk '/^diff/{exit} 1' a.patch
Title: Font Array Support

Modifies st to support user-defined fallback fonts specified in an array
defined as `static const char *fonts[]`. This change also resolves an issue
where fallback fonts were used in place of default fonts in an inconsistent
manner which caused identical sets of text to sometimes use different fonts. In
the following example, DejaVu Sans Mono is the primary font with two others
specified as fallbacks:

    static const char *fonts[] = {
        "DejaVu Sans Mono",
        "VL Gothic",
        "WenQuanYi Micro Hei",
    };

相关内容