I am trying to find a way for grep to output only the content of a capturing group. For instance, if I have the following file:
hello1, please match me
hello2, please do not match me
I would like
grep -Eo '(hello[0-9]+), please match me' file
To output hello1
. However it outputs hello1, please match me
.
Now, I know that grep -Po 'hello[0-9]+(?=, please match me)'
will do the trick, but I'm thinking there must be a way to simply return a capturing group, but I couldn't find any info (on the net and in man grep
).
Is it possible, or are capturing groups only meant to be backrefenced ? It would seem weird to me if there was no way of doing that.
Thank you for your time, and feel free to critique the way this post is constructed!
How to Exclude a Single Word with grep. The most simple way to exclude lines with a string or syntax match is by using grep and the -v flag. The output will be the example. txt text file but excluding any line that contains a string match with “ThisWord”.
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
Exclude Words and Patterns By default, grep is case-sensitive. This means that the uppercase and lowercase characters are treated as distinct. To ignore the case when searching, invoke grep with the -i option. If the search string includes spaces, you need to enclose it in single or double quotation marks.
This question was asked ten years ago, so I won't mark it as duplicate. Also I noticed no sed solution was given since OP asked an answer without:
sed -nr 's/(hello[0-9]+), please match me/\1/p' test.txt
-n
stands for quiet (won't print anything except if explicitly asked)-r
allows use of extented regular expressions (avoids here using \
before parenthesis)s/reg/repl/p
command means "if regexp reg
matches the current line, replace it by captured text by repl
, and prints it (/p
)"If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With