我想搜索2019年及以后的所有行,但仅在“ To:”字符串之后。 “发件人:”和“收件人:”之间的任何内容都不相关。
我曾尝试将grep与-A -B选项一起使用,但是AIX上的grep没有该选项。我也尝试过类似的方法,但是我不知道如何在比赛前打印行,以及如何在行中间搜索模式。
awk'$ 13> = 2019 {print $ 0}'file.txt
最后,我想在“ To:”字符串后面的每一行中搜索“ 2019”。例如,输出看起来像这样:
certificate4-从:CEST 2009年10月16日,星期五1:22:18 PM到:CEST 2019年10月16日,星期三1:32:16 PM
猜测OP的想法:
以及一些假设:
仅基于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
:如果上一行不为空,则打印它]print $0
:打印当前行(与“收件人:”和$ 15> = 2019匹配)prevline=$0
:将我们的'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
#############