I have file where I need 2 lines above my match result using awk. I have put together the below and so far so good, however I want to use match case for search:
#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
The command I am using to achieve what I want is below:
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}'
The above gives me the below:
dupli00913.unl
The output command gives me what I need, but must be for "duplicall" table not "duplicall2". How can I achieve this in my command to match case specific string?
With your shown samples, could you please try following. Written and tested in GNU awk
.
tac Input_file |
awk '
/create table vodac\.duplicall$/{
found=1
next
}
found && ++count==2{
print
exit
}'
Explanation: Adding detailed explanation for above.
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.
}'