How to grep particular string occurring multiple times [duplicate]

佐手、 提交于 2020-01-06 10:09:23

问题


I want to capture a string "1 row affected..". But problem is there are n no of such string present in the same file.

My concern to capture "1 row affected.." only which is present after the string " UPDATE kplustp..Service SET Service_Name = "PositionService", ServiceType = \'Z\', Hostname = " " " in log file. "1 row affected.." will be present after 3 4 sentences from " UPDATE kplustp..Service SET Service_Name = "PositionService", ServiceType = \'Z\', Hostname = " " "

I am working on solaris.

How to do?


回答1:


Use ggrep with the --byte-offset option to find the byte position of the last string that will occur before the string you want to match. Then feed that into /usr/xpg4/bin/tail with the -c option to produce a truncated version of the file containing only the part after that byte location. You can then do your search.

For instance, to search file.txt to find lines containing "foo", but only after the last of any lines containing "bar":

/usr/xpg4/bin/tail -c +`ggrep --byte-offset bar file.txt | cut -d: -f1 | tail -1` file.txt | grep foo

This is somewhat more difficult on Solaris than on Linux/BSD because the default tail and grep are ancient and lack the necessary options. Knowing how to find the GNU version of grep (ggrep) and that the XPG4 (X/Open CAE Portability Guide 4) versions of some commands have useful additional options is half the trick. Both should be installed on most Solaris 10 systems.



来源:https://stackoverflow.com/questions/21259447/how-to-grep-particular-string-occurring-multiple-times

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