Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 从标准输出获取匹配后的文本_Regex_Linux_Bash_Shell_Grep - Fatal编程技术网

Regex 从标准输出获取匹配后的文本

Regex 从标准输出获取匹配后的文本,regex,linux,bash,shell,grep,Regex,Linux,Bash,Shell,Grep,我想在我使用grep或诸如此类的方法找到的特定单词后提取下一个单词/数字 作为一个例子,让我们假设这就是我在stdout中拥有的 string-a0 match-a1 string-a2 string-a3 string-b0 string-b1 match-b2 string-b3 match-c0 string-c1 string-c2 string-c3 我只想留下这个 string-a2 string-b3 string-c1 注意match-a1!=match-b2!=匹配-c0

我想在我使用grep或诸如此类的方法找到的特定单词后提取下一个单词/数字

作为一个例子,让我们假设这就是我在stdout中拥有的

string-a0 match-a1 string-a2 string-a3
string-b0 string-b1 match-b2 string-b3
match-c0 string-c1 string-c2 string-c3
我只想留下这个

string-a2
string-b3
string-c1
注意
match-a1!=match-b2!=匹配-c0

编辑

一个具体的例子

这是什么

open 0.23 date ...
close 1.52 date ...
open 2.34 date ...
close 5.92 date ...
open 10.78 date ...
close 24.21 date ...
total 45.3
我正在搜索单词
open
close
total
,因此输出应该是

0.23
1.52
2.34
5.92
10.78
24.21
45.3

这与一般情况不符,但适用于您的示例:

awk '/^open|^close|^total/{print $2}' input
对于一般情况,如果您对“字符串”的定义基于空白,那么您可能需要:

tr -s ' \n' \\n < input | awk 'p{print; p=0} {p=/^open|^close|^total/}'
tr-s'\n'\\n
您可以使用此awk打印搜索词后的下一个词:

awk -v s='^(open|close|total)$' '{for (i=1; i<NF; i++) if ($i ~ s) print $(i+1)}' 

0.23
1.52
2.34
5.92
10.78
24.21
45.3

awk-v s='^(open | close | total)$'{for(i=1;iwhy string-a2在第1行,为什么不string-a0?因为我想要匹配后的下一个字符串你能详细说明你的匹配条件吗?或者一个更好的例子说明你正在尝试做什么吗?我想…我会尝试againI不知道bash,但我想象正则表达式是
match-[abc][012](\s+)
并抓取第一个捕获组。$2索引不是特定的吗?我不一定知道下一个单词的位置(按索引),这就是为什么我将一般大小写与
string-*
match-*
放在一起。注意匹配位置的不同