文本处理:在行的中间找到一个匹配项,然后打印那个和之前的行

Text processing: find a match in the middle of the line and then print that one and line before

我想搜索 2019 年及以后的所有行,但仅在 "To:" 字符串之后。 "From:" 和 "To:" 之间的任何内容都不相关。

我试过将 grep 与 -A -B 选项一起使用,但 AIX 上的 grep 没有该选项。 我也尝试过类似的方法,但我不知道如何在匹配前打印行,以及如何在行中间搜索模式。

awk '$13 >= 2019 {打印 $0}' file.txt

最后我想在 "To:" 字符串后面的每一行上搜索“2019”。 例如输出看起来像这样:

certificate4 - 从:2009 年 10 月 16 日星期五 1:22:18 CEST 下午到:2019 年 10 月 16 日星期三 1:32:16 CEST 下午

猜测 OP 的想法:

  • 如果一行包含字符串 'To:' 和
  • 该行在字段 #15 中也有一个数字 >= 2019 然后
  • 打印上一行和当前行

以及一些假设:

  • 文件中的第一行可以匹配
  • 连续行可以匹配 'To:/>=2019' 搜索:
  • 对于所有感兴趣的行,'To' 出现在字段 #15
  • 之前

示例数据仅基于 OP 提供的一行:

$ cat -n print15.dat
 1  certificate1 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2019 1:32:16 PM CEST
 2  this is line two
 3  this is line three
 4  certificate4 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2019 1:32:16 PM CEST
 5  this is line five
 6  this is line six
 7  certificate7 - From: Friday, October 16, 2020 1:22:18 PM CEST To: Wednesday, October 16, 2017 1:32:16 PM CEST
 8  this is line eight
 9  this is line nine
10  certificate10 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2020 1:32:16 PM CEST
11  this is line eleven
12  certificate12 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2023 1:32:16 PM CEST
13  certificate13 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2024 1:32:16 PM CEST
14  this is line fourteen

应用所描述的逻辑,我们看到第 1、4、10、12 和 13 行匹配 'To:./>=2019'。

一个awk解法:

$ awk '/To:/ &&  >= 2019 { printf "\n#############\n"
                              if (length(prevline) > 0) { print prevline }
                              print [=11=]
                              printf   "#############\n"
                            }
                            { prevline=[=11=] }
' print15.dat

解释:

  • /To:/ && >= 2019 :匹配任何具有模式 'To:' 和字段 #15 >= 2019 的行(当然,这并不强制 'To:' 在字段 #15 之前)
  • print/######## :简单 header/trailer 以明显区分匹配行集
  • if/length/print : 如果 prevline 不为空则打印它
  • print [=17=] :打印当前行(匹配 'To:' 和 $15>=2019)
  • prevline=[=18=] :将我们的 'prevline' 变量设置为当前行(用作我们处理的下一行的 'previous line')

并且输出:

#############
certificate1 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2019 1:32:16 PM CEST
#############

#############
this is line three
certificate4 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2019 1:32:16 PM CEST
#############

#############
this is line nine
certificate10 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2020 1:32:16 PM CEST
#############

#############
this is line eleven
certificate12 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2023 1:32:16 PM CEST
#############

#############
certificate12 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2023 1:32:16 PM CEST
certificate13 - From: Friday, October 16, 2009 1:22:18 PM CEST To: Wednesday, October 16, 2024 1:32:16 PM CEST
#############