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 want to parse my sources.list to extract the list of repositories. I have:

## Some comment
deb precise stable
deb-src precise stable
deb precise experimental # my current favorite

I want: precise stable precise experimental

So I need: only lines with “deb” at the beginning and until the end of the line or a # character, but excluding it. So far I have:

grep -o "^deb .*"

But how do I match # or end of line without matching the #?

share|improve this question
up vote 6 down vote accepted

Using grep:

grep -Po '(?<=^deb\s).*?(?=#|$)' inputFiles 

Based on @kopischke's suggestion,

grep -Po '(?<=^deb\s)[^#]*' inputFiles

Using sed:

sed -nr '/^deb\s/s;^deb\s([^#]*)#?.*$;\1;p' inputFiles

Usingawk(This solution is based on number of fixed fields):

awk '/^deb /{print $2,$3,$4}' inputFiles

share|improve this answer
+1 for the grep Perl regex (never noticed that one, guess “Perl” made me skip over it :)). However, you don’t need the lookahead to exclude the hash – a negated character class, i.e. [^#]*, will do just fine (line end doesn’t need to be expressly matched – grep matches line by line anyway). – kopischke Jun 11 '12 at 13:15

No need to use sed or awk for such a simple match; just have your regex grab any character but a hash by using a negated character class:

grep -o "^deb [^#]*"

If you need to filter out the leading “deb ”, a simple loop will do:

while read line; do
    echo "${line#deb }"
done <(grep -o "^deb [^#]*")

Edit: a cleaner, one line solution is to use a Perl regex with grep -P, which allows for lookbehind assertions (see Prince John Wesley’s answer).

share|improve this answer
I don't think the OP want the initial "deb ". – cYrus Jun 10 '12 at 18:13
@cYrus: you could be right – the posted regex suggests otherwise, but the expected result is admittedly missing the leading “deb ”. Amended my answer to account for both cases. – kopischke Jun 11 '12 at 0:23

You must log in to answer this question.

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