awk 获取开始和结束键中的所有文本

Awk get all text within start and end keys

我从 man df

得到以下输出
DF(1)                             User Commands                            DF(1)

NAME
       df - report file system space usage

SYNOPSIS
       df [OPTION]... [FILE]...

DESCRIPTION
       This manual page documents the GNU version of df.  df displays the amount
       of space available on the file system containing each file name argument.
       If  no  file  name is given, the space available on all currently mounted
       file systems is shown.  Space is shown in 1K blocks  by  default,  unless
       the  environment  variable POSIXLY_CORRECT is set, in which case 512-byte
       blocks are used.

       (continues)

我想要 awk 这个输出,这样我只得到以 NAME 开始并在 DESCRIPTION 之前结束的行,就像这样:

NAME
       df - report file system space usage

SYNOPSIS
       df [OPTION]... [FILE]...

为此,模式表达式会是什么样子?

这会显示以“NAME”开头的行和以“DESCRIPTION”开头的行之间的所有内容。

man df | awk '/^NAME/{f=1}/^DESCRIPTION/{f=0}f'

我会按照以下方式使用 GNU AWK 完成此任务

man df | awk '/^DESCRIPTION/{exit}/^NAME/,0{print}'

说明:如果行以 DESCRIPTION exit 开始(结束处理)从以 NAME 开始的行开始,直到 0 print 行。请注意,0 永远不会为真,因此它会打印以 DESCRIPTION.

开头的所有内容

(在 gawk 4.2.1 中测试)

你可以这样做:

man df | awk -v RS= -v ORS='\n\n' 'NR==2 || NR == 3'
NAME
       df - report file system space usage

SYNOPSIS
       df [OPTION]... [FILE]...

When RS is set to the empty string, each record always ends at the first blank line encountered 参见 awk 手册:https://www.gnu.org/software/gawk/manual/html_node/Multiple-Line.html