Como posso fazer o sed excluir uma linha e ignorar o teste do arquivo?

1

Estou usando o seguinte comando para extrair descrições de correções:

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

Como posso me livrar da parte egrep -v '^(diff|---)' e usar somente sed? Eu tentei fazer isso:

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

Mas quando esse "d" é atingido, o "q" é ignorado e o resto das linhas no corpo do patch é impresso. Aqui está um patch de amostra:

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;

O script sed deve retornar tudo acima da linha que começa com "diff;" isso é o que a saída deve ser:

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",
    };
    
por Eric Pruitt 16.08.2016 / 04:04

1 resposta

0

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

Usando sed

Nesse caso, tente:

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

O texto acima exclui todas as linhas do primeiro que corresponde ao regex ^diff até a última linha, $ .

Uma versão um pouco mais eficiente sai quando a linha contendo ^diff é atingida:

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

Usando seu arquivo de amostra:

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

Usando o awk

Usando o awk:

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

O 1 é a abreviação abreviada de awk para impressão na linha. No entanto, quando a primeira linha que corresponde ao regex ^diff é atingida, o programa sai.

$ 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",
    };
    
por 16.08.2016 / 05:21

Tags