我需要在匹配的图案之前打印一行。 例子:我有一个这样的文件,
文件为
test file: file1
labalanalma
kaknalknala
user: xyxyxyxyxyxyxyxyyxy
djkdhjkh:whwejewj
jwjkewhjkwe: jkdwhjdweh
etc
etc:etc
Jndwejklhewljk
pattern1: Found
wkjhwekwj
wkjhwekj
我要打印“ pattern1:找到”和“用户:xyxyxyxyxyxyxyxyyxy”(如果首先搜索“ pattern1:找到”,则仅打印从用户开始的行。这是AIX,不是Linux,所以grep -B
在这里无法使用。
答案 0 :(得分:2)
只需将您的user
匹配项放入变量并打印即可:
$ awk '/user:/{u=$0}/pattern/{print $0; print u}' file
pattern1: Found
user: xyxyxyxyxyxyxyxyyxy
答案 1 :(得分:0)
此Perl单行代码将起作用。.
$ perl -ne ' $x=$_ if /user:/; print "$_$x" if /pattern1:/ ' match.txt
pattern1: Found
user: xyxyxyxyxyxyxyxyyxy
$