简体   繁体   中英

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. 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.

Currently my grep is as follows to get the section of the logFile output that I need:

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." 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 ".". This would then give me values like 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:

cat outputfile.txt | sed 's/.*code type: \(.*\)\./\1/'

The \\1 references the contents of the \\(.*\\) part of the expression (the first match group ).

You can do that using bash built-in regEx support. Assuming you have your output captured in a bash variable

$ 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

$ <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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM