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

I'm having an issue with sed not returning what I expect when using a regex pattern to clean up some output from a mysql query.

mysql -uroot -p -e 'SELECT `path` FROM db.media_gallery' | sed -n -r 's/[^\/]+([^\s]+).*/\1/p'

The query returns a bunch of rows such as these:

| /media/M/W/lmt_MWS04_8.png    |
| /media/M/W/lmt_MWS02_11.png   |

When I pipe this through the sed command listed above, it returns this:


Where did "media" go? Why does it randomly remove a part of my string?

Does anyone know how to fix my regex?

share|improve this question
"media" goes away because it is matched by the [^\/]+ part of your regex, which isn't included in your output. We can't fix your regex unless we know what you were trying to do. Are you trying to trim whitespace? – wingedsubmariner Sep 19 '13 at 0:58

No need for sed, the real problem is you're using mysql's prettified output that's intended for human rather than automated consumption. This is a simple task that mysql is more than capable of.

mysql -N -s -r -uroot -p -e 'SELECT `path` FROM db.media_gallery'

will return:


from 'man mysql':

--skip-column-names, -N
--silent, -s
--raw, -r
share|improve this answer

The "media" matched "[^/]+" so it was replaced by the stuff in the parentheses. If you want it, you need to open the parentheses before that regex clause.

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.