Super User is a question and answer site for computer enthusiasts and power users. 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've lots of files which lines looks like


where # is a number. Now i want to show only the parts


in the bash, each in a new line of course.

share|improve this question
up vote 2 down vote accepted

Assuming you want XY to be any two letters...

Something like

grep -Eo "[A-Z]{2}[0-9]{2}_[0-9]{4}" file

Would match [two letters][two numbers]_[four numbers]

If you wanted, it could be a little more exact, making sure there's a / infront and a _ afterwards, but this is a starting point.

Put this together from man grep:

-E, --extended-regexp
          Interpret PATTERN as an extended regular expression (see below)

-o, --only-matching
          Show only the part of a matching line that matches PATTERN.


share|improve this answer

I suppose that XY and YZ are the same, and I'll treat them just as XY

egrep "XY[0-9]{2}_[0-9]{4}" filename -o

The key is the -o option that shows only the matching text.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .