【问题标题】:Text processing: find a match in the middle of the line and then print that one and line before文本处理:在该行的中间找到一个匹配项,然后打印该匹配项和之前的行
【发布时间】:2020-02-01 21:17:04
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 也许我不明白,但根据你的例子,只要你测试的是 15 美元而不是 13 美元,awk 代码就可以工作。
  • 如果你想使用grep,在模式前面加上To:.*,例如To:.*2019
  • How to produce a Minimal, Reproducible example;特别是,提供一些示例数据、预期输出以及您迄今为止尝试过的内容(以及结果输出)

标签: ksh aix


【解决方案1】:

猜测 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:/ && $15 >= 2019 { printf "\n#############\n"
                              if (length(prevline) > 0) { print prevline }
                              print $0
                              printf   "#############\n"
                            }
                            { prevline=$0 }
' print15.dat

解释:

  • /To:/ && $15 >= 2019 :匹配模式为“To:”且字段 #15 >= 2019 的任何行(当然,这并不强制“To:”出现在字段 #15 之前)
  • print/######## : 简单的页眉/拖尾,以明显区分匹配行集
  • if/length/print : 如果 prevline 不为空,则打印它
  • print $0 :打印当前行(匹配 'To:' 和 $15>=2019)
  • prevline=$0 :将我们的“prevline”变量设置为当前行(用作我们处理的下一行的“上一行”)

还有输出:

#############
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
#############

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 2015-01-04
相关资源
最近更新 更多