简体   繁体   English

如何使用正则表达式模式格式化grep的输出以在字符串和字符之间进行匹配

[英]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.

相关问题 匹配字符串中至少 1 个数字和 1 个字符的正则表达式模式 - Regex pattern to match at least 1 number and 1 character in a string 正则表达式将主题标签字符与 javascript 中的字符串格式匹配 - Regex to match hashtag character with string format in javascript 正则表达式:如何替换匹配模式中的字符 - Regex: how to replace a character inside a match pattern 正则表达式匹配最后一个字符和字符串 - Regex to match between last character and string 正则表达式和JS如何正则表达式匹配“和部分模式之间的字符串(匹配前几个字符并且没​​有特殊模式) - Regex & JS how to regex match a string between " and partial patterns (match first few characters and no special pattern) 如何对重叠的字符集进行模式匹配? (使用正则表达式和grep) - How to pattern-match an overlapping set of characters? (using regex and grep) grep 正则表达式 - 如何匹配相同的字符对? - grep regex - how do I match same character pairs? 如何匹配正则表达式中特定模式之间的内容 - how to match something in between a particular pattern in regex 正则表达式-仅当组之间有指定的字符串时,才能从字符到字符匹配 - Regex - Match from Character to Character only if a specified String is in between the Group 更改正则表达式模式以一次将字符串匹配一个字符 - Change regex pattern to match string one character at a time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM