Grep跨越多行,但返回所有匹配

时间:2015-12-17 05:43:35

标签: linux windows awk grep cygwin

我试图对cygwin或linux下的windows文件进行grep,以查找mysql日志中表锁和表解锁之间的所有MySQL语句。

我可以使用awk,但我需要原始文件中的行号

我不知道为什么-Pzo不在cygwin工作

有效地,我正在寻找一种方法来查找被阻止获取锁定的语句(主查询被中断的地方)

如果我至少可以从grep获得合理的结果,那么我可以搜索结果以查找某些内容中断查询的条目。

    2302221 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
    2302221 Query   SELECT id,startAtom,finishAtom FROM browse_being_allocated WHERE poolID = 31543 AND rushID = '32ca680dd0d84f9b9b2945e2186c09ff' AND format = 516 AND startAtom <= 1182716 AND finishAtom > 1182716
    2302221 Query   INSERT INTO browse (poolId,atom,skew,format,rushID,start,finish,databytes,srcPoolID,srcAtom,srcSkew,arrived) VALUES (31543,1182716,0,516,'32ca680dd0d84f9b9b2945e2186c09ff',274545,274588,315392,0,0,0,1)
    2302221 Query   UPDATE browse_being_allocated SET startAtom = 1182717 WHERE id = 26471948
    2302221 Query   UNLOCK TABLES
    2522564 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
    2522564 Query   SELECT id,startAtom,finishAtom FROM browse_being_allocated WHERE poolID = 31543 AND rushID = '32ca680dd0d84f9b9b2945e2186c09ff' AND format = 516 AND startAtom <= 1182717 AND finishAtom > 1182717
    2522564 Query   INSERT INTO browse (poolId,atom,skew,format,rushID,start,finish,databytes,srcPoolID,srcAtom,srcSkew,arrived) VALUES (31543,1182717,0,516,'32ca680dd0d84f9b9b2945e2186c09ff',274588,274633,331776,0,0,0,1)
    2522564 Query   UPDATE browse_being_allocated SET startAtom = 1182718 WHERE id = 26471948
    2522564 Query   UNLOCK TABLES

上面的例子是明智的,下面将代表故障情况的第一部分。

            2334151 Change user user@dbsrv1 on db
    2334151 Query   SET NAMES utf8
    2334151 Query   SET character_set_results = NULL
    2334151 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
    2302201 Change user user@dbsrv1 on db
    2302201 Query   SET NAMES utf8
    2302201 Query   SET character_set_results = NULL
    2302201 Query   SELECT DISTINCT rushID FROM tags WHERE rushID NOT IN (SELECT DISTINCT rushID FROM essencefragments) GROUP BY rushID
 151216 19:00:39    2566722 Quit    

我已经尝试了很多不同的方法来获得多线比赛,但我根本无法得到任何有效的方法。理想情况下,我可以在cygwin下使用它,我真的需要保留原始行号,这样我就可以跳回到原始日志并将其绑定。

有什么想法? 干杯

1 个答案:

答案 0 :(得分:1)

awk应该能够做你想做的事。我认为perl和sed可能也是如此。

使用-E(扩展正则表达式)我相信让你的正则表达式过于贪婪。 至于你的grep -P无法正常工作的原因,你必须使用grep --version和grep - help并做一些研究。我在Ubuntu 16.04上使用GNU grep 2.22正常工作。

如果我正确理解你的问题(我很可能不会这样),你就想要LOCK和UNLOCK字符串之间的所有内容。这是一个可行的解决方案吗?

awk 'BEGIN {ln=1; lck="n"; print "---"};
     lck=="y" {print ln")",$0};
     $3=="UNLOCK" {lck="n"; ln++; print "---"; next};
     $3=="LOCK" && lck=="n" {print ln")",$0; lck="y";ln++; next};
     {ln++};
' NEWSJBHQDB12A.log > NEWSJBHQDB12A_filtered.txt;

它应该跟踪并显示行号(var ln),以便您轻松引用原始日志文件。使用&#34; lck&#34;变量作为开/关开关,它会在找到&#34; LOCK&#34;并在找到&#34; UNLOCK时停止打印。&#34;

$ cat file
2302221 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
2302221 Query   SELECT id,startAtom,finishAtom FROM browse_being_allocated WHERE poolID = 31543 AND rushID = '32ca680dd0d84f9b9b2945e2186c09ff' AND format = 516 AND startAtom <= 1182716 AND finishAtom > 1182716
2302221 Query   INSERT INTO browse (poolId,atom,skew,format,rushID,start,finish,databytes,srcPoolID,srcAtom,srcSkew,arrived) VALUES (31543,1182716,0,516,'32ca680dd0d84f9b9b2945e2186c09ff',274545,274588,315392,0,0,0,1)
2302221 Query   UPDATE browse_being_allocated SET startAtom = 1182717 WHERE id = 26471948
2302221 Query   UNLOCK TABLES
2334151 Change user user@dbsrv1 on db
2334151 Query   SET NAMES utf8
2334151 Query   SET character_set_results = NULL
2334151 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
2302201 Change user user@dbsrv1 on db
2302201 Query   SET NAMES utf8
2302201 Query   SET character_set_results = NULL
2302201 Query   SELECT DISTINCT rushID FROM tags WHERE rushID NOT IN (SELECT DISTINCT rushID FROM essencefragments) GROUP BY rushID 151216 19:00:39
2566722 Quit    
2522564 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
2522564 Query   SELECT id,startAtom,finishAtom FROM browse_being_allocated WHERE poolID = 31543 AND rushID = '32ca680dd0d84f9b9b2945e2186c09ff' AND format = 516 AND startAtom <= 1182717 AND finishAtom > 1182717
2522564 Query   INSERT INTO browse (poolId,atom,skew,format,rushID,start,finish,databytes,srcPoolID,srcAtom,srcSkew,arrived) VALUES (31543,1182717,0,516,'32ca680dd0d84f9b9b2945e2186c09ff',274588,274633,331776,0,0,0,1)
2522564 Query   UPDATE browse_being_allocated SET startAtom = 1182718 WHERE id = 26471948
2522564 Query   UNLOCK TABLES

测试:

$ awk 'BEGIN {ln=1; lck="n"; print "---"};
     lck=="y" {print ln")",$0};
     $3=="UNLOCK" {lck="n"; ln++; print "---"; next};
     $3=="LOCK" && lck=="n" {print ln")",$0; lck="y";ln++; next};
     {ln++};
' file
---
1)     2302221 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
2)     2302221 Query   SELECT id,startAtom,finishAtom FROM browse_being_allocated WHERE poolID = 31543 AND rushID = '32ca680dd0d84f9b9b2945e2186c09ff' AND format = 516 AND startAtom <= 1182716 AND finishAtom > 1182716
3)     2302221 Query   INSERT INTO browse (poolId,atom,skew,format,rushID,start,finish,databytes,srcPoolID,srcAtom,srcSkew,arrived) VALUES (31543,1182716,0,516,'32ca680dd0d84f9b9b2945e2186c09ff',274545,274588,315392,0,0,0,1)
4)     2302221 Query   UPDATE browse_being_allocated SET startAtom = 1182717 WHERE id = 26471948
5)     2302221 Query   UNLOCK TABLES
---
9)     2334151 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
10)     2302201 Change user user@dbsrv1 on db
11)     2302201 Query   SET NAMES utf8
12)     2302201 Query   SET character_set_results = NULL
13)     2302201 Query   SELECT DISTINCT rushID FROM tags WHERE rushID NOT IN (SELECT DISTINCT rushID FROM essencefragments) GROUP BY rushID 151216 19:00:39
14)     2566722 Quit    
15)     2522564 Query   LOCK TABLES browse WRITE, browse_being_allocated WRITE
16)     2522564 Query   SELECT id,startAtom,finishAtom FROM browse_being_allocated WHERE poolID = 31543 AND rushID = '32ca680dd0d84f9b9b2945e2186c09ff' AND format = 516 AND startAtom <= 1182717 AND finishAtom > 1182717
17)     2522564 Query   INSERT INTO browse (poolId,atom,skew,format,rushID,start,finish,databytes,srcPoolID,srcAtom,srcSkew,arrived) VALUES (31543,1182717,0,516,'32ca680dd0d84f9b9b2945e2186c09ff',274588,274633,331776,0,0,0,1)
18)     2522564 Query   UPDATE browse_being_allocated SET startAtom = 1182718 WHERE id = 26471948
19)     2522564 Query   UNLOCK TABLES
---

简单示例:

$ cat file 
NO PRINT
NO PRINT
1 Query LOCK
STUFF
STUFF
STUFF
1 Query UNLOCK
NO PRINT
2 Query LOCK
STUFF
2 Query UNLOCK
NO PRINT
NO PRINT
NO PRINT
NO PRINT

$ awk 'BEGIN {ln=1; lck="n"; print "---"};
     lck=="y" {print ln")",$0};
     $3=="UNLOCK" {lck="n"; ln++; print "---"; next};
     $3=="LOCK" && lck=="n" {print ln")",$0; lck="y";ln++; next};
     {ln++};
' file
---
3) 1 Query LOCK
4) STUFF
5) STUFF
6) STUFF
7) 1 Query UNLOCK
---
9) 2 Query LOCK
10) STUFF
11) 2 Query UNLOCK
---