问题
I have a file A and B .
- A contains log entries
- B contains some line numbers which in turn are some specific line numbers
referring the log file
I have an awk program like this:
echo "($(awk 'NR==1{r1=$1;next}
NR>2{printf"||"}
{printf"(NR>%d&&NR<%d)",r1,$1;r1=$1}' B))&&/mypattern/"|
awk -f- A
---> (courtesy jeff y from stack flow )
in this link you can find the requirement for which the above script is born
the part before the "|" generates the ranges to be search in file A.
eg : (NR>385&&NR<537)||(NR>537&&NR<539)||(NR>539&&NR<547)|
these are some of the ranges which can be generated by the part before "|"
I need to store the ranges for which "mypattern" has a match in a sep file ora variable ate last
say eg NR>385&&NR<537
--> this range on file A may not have a match according to my pattern where as (NR>539&&NR<547
this may have
so any idea to check whether a match had happened , if so to store the NR corresponding NR values in side a file .
回答1:
Given your clarification in comments, I would propose a solution that only takes one pass through FILE_A with awk, rather than trying to extract and use line numbers and ranges numerically (FILE_B) in multiple passes:
awk '/Exception/ && !/ExceptionUnparseable date/ {
haveEx="yes"; ex=$0; exDate=last; haveMatch=""}
haveEx && /tms/ {
haveMatch="yes"; print exDate; print ex; haveEx=""}
haveMatch && /tms/ {print}
{last = $0} ' FILE_A
Or, if you don't need to see the lines that do match the pattern:
awk '/Exception/ && !/ExceptionUnparseable date/ {
haveEx="yes"; ex=$0; exDate=last}
haveEx && /tms/ {
print exDate; print ex; haveEx=""}
{last = $0} ' FILE_A
来源:https://stackoverflow.com/questions/33805839/detecting-the-range-of-the-matches-shell-script