awk规格文本,并在上面打印2行
我有一个文件,我需要2行以上我的匹配结果使用awk。我已经整理了以下内容,目前为止效果良好,但我想使用match case进行搜索:awk规格文本,并在上面打印2行,awk,aix,tac,Awk,Aix,Tac,我有一个文件,我需要2行以上我的匹配结果使用awk。我已经整理了以下内容,目前为止效果良好,但我想使用match case进行搜索: #cat data.out { unload file name = dupli00913.unl number of rows = 251 } create table vodac.duplicall2 { unload file name = dupli01608.unl number of rows = 0 } create table vodac.du
#cat data.out
{ unload file name = dupli00913.unl number of rows = 251 }
create table vodac.duplicall2
{ unload file name = dupli01608.unl number of rows = 0 }
create table vodac.duplicall
我用来实现我想要的目标的命令如下:
cat data.out | awk "/create table vodac.duplicall/{for(i=1;i<=x;)print a[i++];print} {for(i=1;i<x;i++)a[i]=a[i+1];a[x]=\$0;}" x=2 | head -1 | awk '{print $6}'
output命令提供了我需要的内容,但必须用于“duplicall”表,而不是“duplicall2”。如何在我的命令中实现这一点以匹配特定于大小写的字符串?对于您显示的示例,请尝试以下内容。用GNU
awk
编写和测试
tac Input_file |
awk '
/create table vodac\.duplicall$/{
found=1
next
}
found && ++count==2{
print
exit
}'
说明:添加上述内容的详细说明
tac Input_file | ##Using tac Input_file to print lines in reverse order(from last line to first line).
awk ' ##Sending tac command output to awk program here.
/create table vodac\.duplicall$/{ ##Checking condition if line contains create table vodac\.duplicall here.
found=1 ##If above condition is TRUE then set found to 1 here.
next ##next will skip all further statements from here.
}
found && ++count==2{ ##Checking condition if found is SET and count is 2 then do following.
print ##Printing current line here.
exit ##exiting from program from here.
}'
如果你想用“awk”来表示这条线和上面的两条线,我不知道
如果要显示该行和上面的两行,可以执行以下操作:
grep -n 2 "text" <filename> | head -n 3
grep-n2“文本”| head-n3
在该行中,“grep-n2”显示该行及其周围的两行,“head-n3”显示后者的前三行。不适用于me@ChristopherKarsten,它给了我
{unload file name=dupli01608.unl number of rows=0}
期望的输出,请告诉我这是否是您现在期望的输出?谢谢,抱歉。它正在工作。我用猫代替了tac。我想可能是打字的问题error@ChristopherKarsten,欢迎光临,干杯,学习愉快。我想你应该看看:你的grep
命令是否支持-B
选项?那会比awk容易
grep -n 2 "text" <filename> | head -n 3