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

grep print only match group
grep non capturing group
sed capture group
mac grep capture group
sed print capture group
grep lookahead
grep capture group sed
grep print capture only

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!


If you have either pcregrep or pcre2grep you can use the -o1 command-line flag to request that only capture group 1 is output. (Or change 1 to some other number if there are more captures in the regex.)

You can use the -oN command more than once if you want to output more than one capture group.

As far as I know, grep -P does not implement this extension. You'll find pcre2grep in Debian/Ubuntu package pcre2-utils. pcregrep is in package pcregrep.

Can grep output only specified groupings that match?, Replace foobar word other stuff by word , print only if a replacement is done. pcregrep has a smarter -o option that lets you choose which capturing groups you  There are many useful flags such as -E(extended regular expression) or -P(perl like regular expression), -v(–invert, select non-matching lines) or -o(show matched part only). So some day I want to output capture group only. After Googling, many people are actually suggesting sed–sadly I am not good with sed.


There is a tricky way with Perl mode

$ echo "hello1, please match me" | rev | grep -oP 'em hctam esaelp ,\K[0-9]olleh' | rev
hello1

essentially using \K lookbehind by reversing the input and search terms.

You can outsource reversing the search term to rev as well.

$ echo hello1, please match me | 
  rev | 
  grep -oP "$(echo hello1K\\, please match me | rev)" | 
  rev

grep and show capture group only, So some day I want to output capture group only. After Googling, many people are actually suggesting sed–sadly I am not good with sed. Grep Console recognises capture groups and allows you to assign a distinct style to each capture group. So far, we’ve only used the “whole line” group, which is present for every regular expression and applies, as the name says, to the entire matched line.


Just an awk version.

awk -F, '/hello[0-9]+, please match me/ {print $1}' file
hello1

Can GNU Grep output a selected group?, You can use sed for this. On BSD sed : echo "foo 'bar'" | sed -E "s/.*'([^']+)'.*/\\1/". Or, without the -E option: sed "s/.*'\([^']\+\)'.*/\1/". This doesn't work for multiline  The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. grep -x “phoenix number3” * The output shows only the lines with the exact match. If there are any other words or characters in the same line, the grep does not include it in the search results.


Don't need the whole line, just the match from regular expression , As stated by @Rory, you need the -o option, so only the match are printed (​instead of whole line) if file contains the sting /(a)5667/ , grep will print 'a', because: that supports capturing sub matches (Named or Numbered Capturing Groups). If you want only what is in the parenthesis, you need something that supports capturing sub matches (Named or Numbered Capturing Groups). I don't think grep or egrep can do this, perl and sed can. For example, with perl: If a file called foo has a line in that is as follows: /adsdds / And you do: perl -nle 'print $1 if /\/(\w).+\//' foo


Capture groups with grep perl regular expression, Capture groups with grep perl regular expression for use of capture groups with \K . To return only the contents of the capture group, use -o . Viewing grep output in color. If we use the --color option, our successful matches will be highlighted for us: Viewing line numbers of successful matches. It will be even more useful if we know where the matching line appears in our file. If we specify the -n option, grep will prefix each matching line with the line number:


Regular Expression Reference: Capturing Groups and Backreferences, They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex  grep "^balaram" test.txt. You should see the following output: balaram balaram is a good boy balarampur is a village. Next, display all the lines which start with the word balaram only: grep -w "^balaram" test.txt. You should see the following output: balaram balaram is a good boy. Next, find the number of blank lines in the file test.txt: grep


egrep(1): print lines matching pattern, grep, egrep, fgrep - print lines matching a pattern -w, --word-regexp: Select only those lines containing matches that form whole words. (for fields and groups of context lines) with escape sequences to display them in color on the terminal. -T, --initial-tab: Make sure that the first character of actual line content lies on a  grep searches one or more input files for lines that match a regular expression and writes each matching line to standard output. In this article, we’re going to explore the basics of how to use regular expressions in the GNU version of grep , which is available by default in most Linux operating systems.