I have a txt file like this:
asdfasdf
ffaCover 91adf
ffffa
I want to use grep or some linux tool to capture the 1 or 2 digits following the space after 'Cover'. In some programming languages I would use the regex library to match /^Cover (\\d\\d?)$/. Then there would be some way to get at the 1 or 2 digits inside the parenthesis. Is there a way to do this using grep?
Just pipe to grep -oP
:
grep -Po 'Cover \K[0-9]{1,2}'
91
\\K
in PCRE regex resets all previously matched information.
I had always done something like this:
grep -Po '(?<=Cover\s)(\d\d?)'
The -o
option to grep
makes it print out only the matching part of the pattern, which does not include the zero-width lookbehind assertion.
But, I did not know about the \\K
option in anubhava's answer before, which seems cleaner.
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.