1

I'm using the following command to extract descriptions from patches:

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

How can I get rid of the egrep -v '^(diff|---)' part and only use sed? I tried doing this:

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

But once that "d" is reached, the "q" gets skipped and the rest of the lines in the body of the patch get printed. Here is a sample 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",
    };

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;

The sed script should return everything above the line that starts with "diff;" this is what the output should be:

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",
    };
  • @John1024, I've added a sample patch. – Eric Pruitt Aug 16 '16 at 2:42
0

return everything above the line that starts with "diff;"

Using sed

In that case, try:

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

The above deletes all lines from the first that matches the regex ^diff to the last line, $.

A slightly more efficient version exits when the line containing ^diff is reached:

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

Using your sample file:

$ 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",
    };

Using awk

Using awk:

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

The 1 is awk's cryptic shorthand for print-the-line. However, when the first line that matches the regex ^diff is reached, the program exits.

$ 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",
    };
  • 1
    Thanks, that works. I'm actually comfortable with awk, but I want to get better with sed, and I also find it easier to write portable sed scripts than portable awk scripts likely because I've been spoiled by gawks numerous, useful extensions whereas I rarely find myself yearning for new features when I use sed. In this case, the awk script you posted is perfect portable, but the sed script I posted is a shortened version of what I'm actually using. – Eric Pruitt Aug 16 '16 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.