【问题标题】:How to print everything between two patterns using awk如何使用 awk 打印两个模式之间的所有内容
【发布时间】:2022-01-26 08:17:52
【问题描述】:

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

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

我需要打印 PATTERN1 之间的所有内容以及 PATTERN2 之前的所有内容(始终以 # 开头的一行。

实际上,这些模式是诸如#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.

如果我能以其他颜色(如蓝色或红色)显示图案,那就太好了。

【问题讨论】:

标签: awk


【解决方案1】:

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

awk -v cc="#france" '/^#/{f=($1==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=($1==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,如This Post。这取决于您的终端设置是否支持 ANSI 代码。大多数都这样做。

例子:

awk -v cc="#germany" '/^#/{
    f=($1==cc)
    if (f) printf("\033[0;31m%s\033[0m\n", $0)
    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.

【讨论】:

  • 谢谢,效果很好。我怎样才能给图案上色?例如:#france
  • 使用 ANSI 代码。见编辑
【解决方案2】:

如果条目之间总是有空行,您可以通过将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 中测试)

【讨论】:

    【解决方案3】:

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

    awk -v RS="" '$0~/^#france\n/' Input_file
    

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 2013-06-03
      • 2011-05-11
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多