Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep - how to output only the content of a capturing group

Tags:

grep

bash

shell

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!

like image 426
Sami Avatar asked Oct 14 '19 14:10

Sami


People also ask

How do you grep everything except?

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

How do I match a group in regex?

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

How do I ignore something in grep?

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.


1 Answers

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)"
like image 67
Amessihel Avatar answered Sep 18 '22 13:09

Amessihel