Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Usually, grep searches for all lines containing a match for the pattern/parameter I specify.

I would like to match just the pattern (i.e. not the whole line).

So, if a file contains the lines:

We said that we'll come.
Unfortunately, we were delayed.
Now, we're on our way.
Didn't I say we'd come?

I want to find all contractions starting with "we" (regex pattern: we\'[a-z]+/i); I'm looking for the output:


How do I do this (with grep or another Unix/Windows command-line tool)?

share|improve this question
I took a look at the 'grep' man page: and nothing suggested what you want is possible. You might want to try using 'sed' or 'awk'. If really feeling adventurous, even write a 2-5 line script in PERL. – Rolnik Jan 3 '11 at 20:41
up vote 3 down vote accepted

Use the -o option:

grep -E -i -o "we'[a-z]+" file.txt

Note that this is not universally portable to all grep implementations, though.

share|improve this answer
My grep has it, so that works for me! – palswim Jan 5 '11 at 0:38

I'd prefer Perl for something like this:


use strict;
use warnings;

open FH, "< parse.txt" or die $!;

    while($_ =~ /\b(we\'\w+)\b/g)
        print $1."\n";

close FH;

Input text:

Some text we're test we'll why we're.
More text we'll we're.


share|improve this answer
Instead of hardcoding the filename, you should make it accept a filename as an argument and work as a filter. Also, I believe the OP wanted case-insensitive. – Dennis Williamson Jan 3 '11 at 21:35
@Dennis it was an example... – John T Jan 3 '11 at 23:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.