用grep读取日志文件中的型式1和打印包含型式1只行。停止搜索时PATTERN2在文件中被发现 [英] Using grep to read a log for pattern1 in the file and print only lines containing pattern1. Stop searching when pattern2 is found in the file
问题描述
使用的grep / SED / AWK
我要搜索样式1
直到 PATTERN2
在文件中被发现,打印出含有结果仅样式1
。
Using grep/sed/awk
I want to search for pattern1
until pattern2
is found in a file and print the results containing only pattern1
.
我不想对此有在这个网站很多解决方案样式1的范围和PATTERN2之间的界限。请帮助。
I don't want the lines between the range of pattern1 and pattern2 for which there are many solutions in this site. Please help.
我试过没有结果如下:
亚行logcat | grep的-i样式1| grep的-m 1'PATTERN2
adb logcat | grep -i 'pattern1' | grep -m 1 'pattern2'
看起来|
使它成为键,条件
和我要找的字符串,不在同一行上。他们在不同的线路。
Looks like '|'
makes it an AND condition
and the strings that I am looking for, are not in the same line. They are in different lines.
更新:的解决方案,在终端,但不是在一个脚本工作。有人能找出为什么没有在脚本的工作帮助吗?
Updated: The solutions work in the terminal but not in a script. Can someone help with finding out why it's not working in a script?
推荐答案
使用sed将停止打印PATTERN2被发现后:
Use sed to stop printing after pattern2 is found:
adb logcat | sed '/pattern2/q' | grep -i 'pattern1'
或者使用SED既打印样式1和PATTERN2后退出:
Or use sed to both print pattern1 and quit after pattern2:
adb logcat | sed -n '/pattern1/p;/pattern2/q'
这篇关于用grep读取日志文件中的型式1和打印包含型式1只行。停止搜索时PATTERN2在文件中被发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!