如何使用 awk 打印两个模式之间的所有内容

How to print everything between two patterns using awk

我可以使用这个 awk 命令打印两个模式之间的所有内容:

awk '/''/{a=1} a; /PATTERN2/{a=0}' ~/the/path/to/file.txt

我需要的是打印 PATTERN1 之间的所有内容,包括 PATTERN1PATTERN2 之前的所有内容(它总是以 #.

实际上,这些模式是标签,例如 #france#germany

例如文件包含:

#france
France is a European country.
It's capital city is Paris.
One of its biggest cities is Lyon.

#germany
Germany is a European country.
It's capital city is Berlin.
One of its biggest cities is Munich.

调用后我要打印的内容#france:

#france
France is a European country.
It's capital city is Paris.
One of its biggest cities is Lyon.

另外,如果我能用蓝色或红色等其他颜色显示图案,那就太好了。

如果条目之间总是有空行,您可以通过将 RS 设置为空字符串来利用 GNU AWK 的段落模式,让 file.txt 内容为

#france
France is a European country.
It's capital city is Paris.
One of its biggest cities is Lyon.

#germany
Germany is a European country.
It's capital city is Berlin.
One of its biggest cities is Munich.

然后

awk 'BEGIN{RS=""}/^#france/' file.txt

输出

#france
France is a European country.
It's capital city is Paris.
One of its biggest cities is Lyon.

解释:RS="" 导致 GNU AWK 将段落视为行。我只是过滤以 (^) #france.

开头的行

(在 GNU Awk 5.0.1 中测试)

您可以使用基于匹配您要打印的部分设置标志的模式:

awk -v cc="#france" '/^#/{f=(==cc)} f' file
#france
France is a European country.
It's capital city is Paris.
One of its biggest cities is Lyon.

或者:

awk -v cc="#germany" '/^#/{f=(==cc)} f' file
#germany
Germany is a European country.
It's capital city is Berlin.
One of its biggest cities is Munich.

如果不匹配,它将永远不会开始打印,只有在遇到另一个 #hashtag 时才会停止打印。


如何给图案上色?

您将使用 ANSI Escape Codes as in This Post。这取决于您的终端设置是否支持 ANSI 代码。大部分都是。

示例:

awk -v cc="#germany" '/^#/{
    f=(==cc)
    if (f) printf("3[0;31m%s3[0m\n", [=12=])
    next 
    } 
    f' file

打印(在我的终端上):

#germany                             # red on black
Germany is a European country.       # green on black...
It's capital city is Berlin.         # green is the default color
One of its biggest cities is Munich.

仅使用您显示的示例,您可以尝试以下 awk 代码。我在哪里使用 RS(记录分隔符)作为段落模式并检查行是否从特定字符串开始然后打印整个段落。

awk -v RS="" '[=10=]~/^#france\n/' Input_file