10

I want to use a regex to find a pattern in a file. That pattern may be in the middle of a line, but I don't want the whole line. I tried grep -a pattern file but this returns the entire line that contains the regex. The following is an example of what I'm trying to do. Does anyone know a way to do this?

Example:

Input: AAAAAAAAAAAAAXxXxXxXxBananasyYyYyYyYBBBBBBBCCCCCC

Regex: Xx.*yY

Ouput: XxXxXxXxBananasyYyYyYyY

MiketheCalamity
  • 1,229
  • 1
  • 15
  • 32
  • possible duplicate of [Can grep show only words that match search pattern?](http://stackoverflow.com/questions/1546711/can-grep-show-only-words-that-match-search-pattern) – Joe Apr 05 '14 at 08:20

3 Answers3

11

you were close, you need the -o flag

grep -o 'Xx.*yY' <<<AAAAAAAAAAAAAXxXxXxXxBananasyYyYyYyYBBBBBBBCCCCCC
XxXxXxXxBananasyYyYyYyY
iruvar
  • 22,736
  • 7
  • 53
  • 82
  • 3
    Note: The `-o` flag is nonstandard, although it exists on both BSD and GNU userland systems. – kojiro Apr 05 '14 at 00:42
  • 1
    @JS웃 It's available on a freshly built FreeBSD 10 I have right here as well as OS X (Mavericks), which says `BSD` at the bottom of the `grep` manpage. – kojiro Apr 05 '14 at 02:59
  • @kojiro Sorry my bad. I got confused with `-P` option. My apologies. I wrote `-o` option and was looking for `-P`. – jaypal singh Apr 05 '14 at 03:36
5

Use the -o option to print just the part of the line that matches the regexp

grep -o pattern file
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

In addition to grep -o (the simplest way), there are a couple of other options:

  1. In bash, without relying on any particular implementation of grep:

    $ regex='Xx.*yY'
    $ [[ AAAAAAAAAAAAAXxXxXxXxBananasyYyYyYyYBBBBBBBCCCCCC =~ $regex ]]
    $ echo ${BASH_REMATCH[0]}
    XxXxXxXxBananasyYyYyYyY
    
  2. Using expr, which is a little unwieldy (in part because the regular expression is implicitly anchored to the beginning of the string), but is defined by the POSIX standard so it should work on any POSIX platform, regardless of the shell used.

    $ expr AAAAAAAAAAAAAXxXxXxXxBananasyYyYyYyYBBBBBBBCCCCCC : '[^X]*\(Xx.*yY\)'
    XxXxXxXxBananasyYyYyYyY
    
chepner
  • 497,756
  • 71
  • 530
  • 681