Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 在bash中的引号后提取一个数字_Regex_Bash - Fatal编程技术网

Regex 在bash中的引号后提取一个数字

Regex 在bash中的引号后提取一个数字,regex,bash,Regex,Bash,我有一个像这样的输出 [1] "0 found |2018-07-15 22:21:09 - no new item is submitted " 如何在bash中使用正则表达式通过“grep”提取0 谢谢大家! 如果使用awk正常。请您尝试以下内容,并让我知道这是否有助于您 awk 'match($0,/\"[0-9]+/){print substr($0,RSTART+1,RLENGTH-1)}' Input_file 或 尝试此grep选项: grep -Po '^([0-9]+) (

我有一个像这样的输出

[1] "0 found |2018-07-15 22:21:09 - no new item is submitted "
如何在bash中使用正则表达式通过“grep”提取0


谢谢大家!

如果使用
awk
正常。请您尝试以下内容,并让我知道这是否有助于您

awk 'match($0,/\"[0-9]+/){print substr($0,RSTART+1,RLENGTH-1)}' Input_file


尝试此
grep
选项:

grep -Po '^([0-9]+) (?=found)'


上面的一行使用了模式
^([0-9]+)(?=found)
,表示匹配行首的一个数字,该数字后面紧跟着文本
found

有一个sed解决方案

$ sed 's/^.*"\(.*\) found .*/\1/' <<< '[1] "0 found |2018-07-15 22:21:09 - no new 
item is submitted "'
0

$sed的/^.*\(.*\)找到了./\1/'我还提出了这个解决方案:

$LOG_FILE是我将要写入所有输出的文件

grep -oE '\[1\] " *[0-9]' $LOG_FILE | cut -d"\"" -f2 | head -1

awk-F'[“]'{print$3}'
也许
grep-oP'\K\d+'
有帮助如果你真的在
“0…
寻找
0
,那么一个简单的
-o
(仅匹配)
-w
(word)设置就是你所需要的,例如
grep-ow 0'。然后只需回显您的字符串(或使用*here字符串*),例如
echo[1]“0已找到”| 2018-07-15 22:21:09-未提交任何新项目”| grep-ow 0”,这将导致一个单独的
0
$ perl -ne 'print $1 if /(\d+) found/' <<< '[1] "0 found |2018-07-15 22:21:09 - no 
new item is submitted "'
0
grep -oE '\[1\] " *[0-9]' $LOG_FILE | cut -d"\"" -f2 | head -1