我从命令替换变量中获得以下输出:
'Enable succeeded: [stdout]Proc is not up yet, retrying...Proc is not up yet, retrying...Proc is not up yet, retrying...Proc is not up yet, retrying...Proc is not up yet, retrying...Proc is not up yet, retrying...[stderr]'
字符串 Proc尚未启动,正在重试... 的次数可能未知,并且由于我只需要捕获一个事件,所以我正在使用 grep -o- m1 即可达到目的。问题是我还需要允许我的变量捕获多个不同的特定字符串(例如 Proc已启动并正在运行!),类似地,该字符串可能会出现未知次数。
请注意,每次出现的字符串都用换行符分隔,理想情况下,我还要剥离使用grep -m1时保留在字符串末尾的最后\ n。
答案 0 :(得分:0)
急救!
$ cat lines.txt
This matches regexp a.
This also matches regexp a.
This matches regexp b.
This also matches regexp a.
And this matches regexp b.
$ perl -nE 'say $& if m?regexp a? || m?regexp b?' lines.txt
regexp a
regexp b
答案 1 :(得分:0)
如果您要grep多个表达式,请用\|
分隔它们,如下所示:
grep "Proc is not up yet, retrying...\|Proc is up and running!"
答案 2 :(得分:0)
我知道您要求使用正则表达式,但是使用 uniq 可能就足够了:
Proc is not up yet, retrying...
>!#<@!<@
Proc is not up yet, retrying...
Proc is up and running!
Proc is up and running!
Proc is up and running!
??????????????????????????????
Proc is up and running!
Proc is done running.
cat sample_text | grep -o '^Proc is.*' | uniq
Proc is not up yet, retrying...
Proc is up and running!
Proc is done running.
如果您觉得它有用,请务必查看它的手册页,其中包括计算重复行等功能,可能会派上用场!