3

I want to parse my sources.list to extract the list of repositories. I have:

## Some comment
deb http://some.vendor.com/ubuntu precise stable
deb-src http://some.vendor.com/ubuntu precise stable
deb http://some.othervendor.com/ubuntu precise experimental # my current favorite

I want:

http://some.vendor.com/ubuntu precise stable
http://some.othervendor.com/ubuntu 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 #?

2 Answers 2

7

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

1
  • 2
    +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, 2012 at 13:15
3

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).

2
  • I don't think the OP want the initial "deb ".
    – cYrus
    Jun 10, 2012 at 18:13
  • 1
    @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, 2012 at 0:23

You must log in to answer this question.

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