cat a.txt
12345
错误1
abcdef
aaaaaa
错误
“错误”是固定的字符串,我要想获取的结果是含有错误的行和它的上一行,如下:
12345
错误1
aaaaaa
12345
错误1
abcdef
aaaaaa
错误
“错误”是固定的字符串,我要想获取的结果是含有错误的行和它的上一行,如下:
12345
错误1
aaaaaa
错误
答案1:
[root@hostkiller ~]# cat 2.txt
12345
错误1
abcdef
后:
[root@hostkiller ~]# grep '错误1' -A 1 2.txt
abcdef
前:
[root@hostkiller ~]# grep '错误1' -B 1 2.txt
12345
答案2:
sed '$!N;/错误/P;D' urfile
答案3:
awk '/错误/{print a"\n"$0}{a=$0}' FILE
打印本行和下一行:
awk '/关键字/{getline v;print $0"\n"v}'