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