Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I simply need to get the match from a regular expression:

$ cat myfile.txt | SOMETHING_HERE "/(\w).+/"

The output has to be only what was matched, inside the parenthesis.

Don't think I can use grep because it matches the whole line.

Please let me know how to do this.

share|improve this question

Use the -o option in grep.


$ echo "foobarbaz" | grep -o 'b[aeiou]r'
share|improve this answer
Good grief... Do you have any idea how many times I wrestled with sed backreferences to do that? – Insyte Aug 6 '09 at 17:36
The o option to grep/egrep returns only what matched the entire regular expression, not just what is in () like he asked for. – Kyle Brandt Aug 6 '09 at 17:59
However, that is a very good thing to know anyways :-) – Kyle Brandt Aug 6 '09 at 18:00
@KyleBrandt: To match only one part (e.g.: the parenses) it's possible to mark the rest with a look ahead or look behind: (?<= ) and (?= ) – DrYak Jan 20 '15 at 13:09

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

The letter a is returned. That might be not what you want though. If you tell us what you are trying to match, you might get better help. $1 is whatever was captured in the first set of parenthesis. $2 would be the second set etc.

share|improve this answer
I was just trying to match what is in parenthesis. Seems like passing it to a perl or a php script might be the answer. – Alex L Aug 6 '09 at 18:01

Because you tagged your question as bash in addition to shell, there is another solution beside grep :

Bash has its own regular expression engine since version 3.0, using the =~ operator, just like Perl.

now, given the following code:

DATA="test <Lane>8</Lane>"

if [[ "$DATA" =~ \<Lane\>([[:digit:]]+)\<\/Lane\> ]]; then
        echo $BASH_REMATCH
        echo ${BASH_REMATCH[1]}
  • Note that you have to invoke it as bashand not just sh in order to get all extensions
  • $BASH_REMATCH will give the whole string as matched by the whole regular expression, so <Lane>8</Lane>
  • ${BASH_REMATCH[1]} will give the part matched by the 1st group, thus only 8
share|improve this answer
Dear @DrYak, I hope you're not parsing XML with regex here.. :) – Jun 16 at 12:43
It's even worse. I'm parsing a horrible mix of XML and FASTA data (which both use the > symbol for entirely different purposes) as spewed out by the SANSparallel fast largescale alignement software. Of course both formats are spewed interlaced without any escaping. So it's impossible to throw some standard XML library at this. And I'm using Bash regex at this point of the code because I only need to extract a couple of data, and 2 regex do the job much better for me than writing a dedicated parser for this mess. #LifeInBioinformatics – DrYak Jun 18 at 10:24
In other words: there's a point where extracting 1 single number is simpler to do with a regex rathan than dancing the whole XML tango – DrYak Jun 18 at 10:25
Hah, gotcha! :) – Jul 4 at 10:07

This will accomplish what you are requesting, but I don't think it is what you really want. I put the .* in the front of the regex to eat up anything before the match, but that is a greedy operation, so this only matches the penultimate \w character in the string.

Note that you need to escape the parens and the +.

sed 's/.*\(\w\).\+/\1/' myfile.txt
share|improve this answer

2 Things:

  • As stated by @Rory, you need the -o option, so only the match are printed (instead of whole line)
  • In addition, you neet the -P option, to use Perl regular expressions, which include useful elements like Look ahead (?= ) and Look behind (?<= ), those look for parts, but don't actually match and print them.

If you want only the part inside the parensis to be matched:

grep -oP '(?<=\/\()\w(?=\).+\/)' myfile.txt

if file contains the sting /(a)5667/, grep will print 'a', because:

  • /( are found by \/\(, but because they are in a look-behind (?<= ) they are not reported
  • a is matched by \w and is thus printed (because of -o )
  • )5667/ are found b< \).+\/, but because they are in a look-ahead (?= ) they are not reported
share|improve this answer
    sed -n "s/^.*\(captureThis\).*$/\1/p"

-n      don't print lines
s       substitute
^.*     matches anything before the captureThis 
\( \)   capture everything between and assign it to \1 
.*$     matches anything after the captureThis 
\1      replace everything with captureThis 
p       print it
share|improve this answer

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.