[英]How to format the output of a grep with a regex pattern to match between a string and character
I have been working on a bash script that greps occurences of a string from a logFile into an outputFile to monitor its frequency. 我一直在研究bash脚本,该脚本可以将字符串从logFile出现到outputFile中以监视其频率。 I want to filter this even further and use the result of that grep to then format a section of the string to be my end result.
我想进一步过滤并使用该grep的结果,然后将字符串的一部分格式化为我的最终结果。
Currently my grep is as follows to get the section of the logFile output that I need: 目前,我的grep如下获取我需要的logFile输出部分:
grep -n -A 1 "No entry for this particular code type" logFile.txt >> outputfile.txt
This gets the full line that starts with that string and will look like the following, with the value of code type changing throughout the logs constantly: "No entry for this particular code type, code type: 001123." 这将获得以该字符串开头的完整行,如下所示,代码类型的值在整个日志中不断变化:“此特定代码类型无条目,代码类型:001123。” etc.
等等
I want to parse the resulting lines like the above which are outputted from the grep, and just retrieve the value between the string "code type:" and the character ".". 我想解析从grep输出的上述结果行,只检索字符串“代码类型:”和字符“。”之间的值。 This would then give me values like 001123
这会给我类似001123的值
I have been looking online for a solution and nothing that I have tried has worked out. 我一直在网上寻找解决方案,但没有尝试过。 Any suggestions would be greatly appreciated.
任何建议将不胜感激。
You can use sed
to pull the number out using another regular expression: 您可以使用
sed
通过另一个正则表达式提取数字:
cat outputfile.txt | sed 's/.*code type: \(.*\)\./\1/'
The \\1
references the contents of the \\(.*\\)
part of the expression (the first match group ). \\1
引用表达式(第一个匹配组 )的\\(.*\\)
部分的内容。
You can do that using bash
built-in regEx
support. 您可以使用
bash
内置的regEx
支持来实现。 Assuming you have your output captured in a bash
variable 假设您将输出捕获在
bash
变量中
$ myString="No entry for this particular code type, code type: 001123."
$ [[ $myString =~ code\ type:(.*). ]] && subString="${BASH_REMATCH[1]}"
$
$ printf "%s\n" "$subString"
001123
(or) if you are OK to use grep
piped once more for regEx
capture, do (或)如果可以再次使用通过管道传递的
grep
进行regEx
捕获,请执行
$ <first_grep_command> | grep -Po "code type: \K.*(?=.)"
001123
where -P
flag for supporting only perl style regular expression matching and -o
to return only the matching string. 其中
-P
标志仅支持perl样式正则表达式匹配,而-o
标志仅返回匹配的字符串。
This one worked directly in my shell: 这直接在我的外壳中工作:
echo "No entry for this particular code type, code type: 001123." |grep -Po '[0-9]*'
meaning that this one could work in your case without too many pipes: 意思是说,这种方法可以在没有太多管道的情况下适用于您的情况:
grep -Po '[0-9]*' logfile.txt >>outputfile.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.