date;
tocompdate=$(date --date="-90 days ago" +%d%m%Y)
echo $tocompdate;
awk -v pattern="$testdate" '{print "Some matter "nr[NR--]" matter "$testdate""};' tocompare.txt >> mail2sent.txt`
在这里,我获取当前日期并添加 90 天并将其存储在 tocompdate
中。并将 tocompdate
与文件进行比较,其中一行中存在相同的日期,而该行之前的其他行中存在其他一些内容。
当搜索模式匹配时,我需要打印一些文本,并打印“匹配行之前的行”,并在几个单词后再次匹配行。
例如:
文件包含以下内容
a1
28112019
b2
04032018
c3
04032018
d4
27072015
e5
27072015
f6
06012030
如果我与27072015
进行比较,我想要如下输出
The d4 is on 27072015
The e5 is on 27072015
最佳答案
$ awk -v tgt="27072015" '$0==tgt{printf "The %s is on %s\n", p, $0} {p=$0}' file
The d4 is on 27072015
The e5 is on 27072015
关于bash - 在 awk 中在文件中搜索时使用变量作为搜索模式时面临的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32993095/