Detecting the range of the matches - shell script

故事扮演 提交于 2019-12-13 04:49:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!