[ALUG] Regex Help

Brett Parker iDunno at sommitrealweird.co.uk
Tue Jul 13 16:54:10 BST 2010


On 13 Jul 16:47, Brett Parker wrote:
> On 13 Jul 16:28, Richard Parsons wrote:
> > Friends,
> >
> > I admit that I am terrible with regex.  I need a script to output all  
> > words beginning with "$" from a text file.  Here's what I've got so far:
> >
> > cat file.txt | grep "$" | sed <some-regex>
> 
> How about:
> 
>     sed -e 's#[^$]*\(\$[^[:space:]]*\)#\1 #g; s#\(.*\)\([$][[:alpha:]]*\).*$#\1\2#; s# #\n#g;' file.txt

Doesn't work if there's not a $ on a line, though, it then splits that
line up... so, a revision later...

    sed -e '/[$]/ { s#[^$]*\(\$[^[:space:]]*\)#\1 #g; s#\(.*\)\([$][[:alpha:]]*\).*$#\1\2#; s# #\n#g; p; }; d;' file.txt

But, Martijn has a neater solution in the form:
    grep -E -o '\$\w+' file.txt

(Dunno if he's going to post that though :)
-- 
Brett Parker



More information about the main mailing list