[ILUG] regular expression question

Padraig Brady padraig at antefacto.com
Tue Nov 13 17:10:48 GMT 2001


Fergal Daly wrote:

> Under tcsh the grep dies with an error unless I change the "s to 's. This is 
> because the shell sees $\ as a variable. Maybe in your shell $\ is not an 
> unknown variable and so grep executes but does the wrong thing. Try replacing 
> grep with echo and see if the regexp is printed out correctly.


Using bash, so this is not an issue.
> 
> Also, one thing that seems wrong is the fact that you only check the [^/]+ 
> after /lib/ and not /usr/lib/.


I was just simplifying.


> As a Perl bigot, I think you should use egrep or grep -E to get rid of those 
> backslashes too!
> 
> Anyway, this seems to do what you require
> 
> cat input.txt | grep -E '^(/usr/lib/|^/lib/)[^/]+$'


Hmm grep -E works in both cases. I thought the -E just
changed the way the parsing was done for \ ?
Have I found a bug?

[padraig at pixelbeat findul]$ cat input.txt

/lib/libthread_db-1.0.so
/usr/lib/gphoto/libgphoto_canon.so.0
/usr/lib/lib_alchemistmodule.so

[padraig at pixelbeat findul]$ grep -E '(^/usr/lib/[^/]+$|^/lib/[^/]+$)' input.txt 
/lib/libthread_db-1.0.so
/usr/lib/lib_alchemistmodule.so


[padraig at pixelbeat findul]$ grep -E '(^/usr/lib/[^/]*$|^/lib/[^/]*$)' 
input.txt
/lib/libthread_db-1.0.so
/usr/lib/lib_alchemistmodule.so

[padraig at pixelbeat findul]$ grep '\(^/usr/lib/[^/]+$\|^/lib/[^/]+$\)' input.txt 


[padraig at pixelbeat findul]$ grep '\(^/usr/lib/[^/]*$\|^/lib/[^/]*$\)' input.txt 
/lib/libthread_db-1.0.so
/usr/lib/lib_alchemistmodule.so


thanks,

Padraig.


 
> Fergal
> 
> On Tuesday 13 November 2001 16:15, Padraig Brady wrote:
> 
>>[padraig at pixelbeat findul]$ cat input.txt
>>/lib/libthread_db-1.0.so
>>/usr/lib/gphoto/libgphoto_canon.so.0
>>/usr/lib/lib_alchemistmodule.so
>>
>>
>>OK I want to only show files in /lib/ or /usr/lib/
>>so I'm trying to exclude lines that have a / after
>>these 2 strings.
>>So try 1: Match 1 or more characters not /
>>
>>[padraig at pixelbeat findul]$ cat input.txt |
>>grep "\(^/usr/lib/[^/]+$\|^/lib/\)"
>>/lib/libthread_db-1.0.so
>>
>>This didn't match the last entry, why?
>>
>>The following works but is not just robust?
>>[padraig at pixelbeat findul]$ cat input.txt |
>>grep "\(^/usr/lib/[^/]*$\|^/lib/\)"
>>/lib/libthread_db-1.0.so
>>/usr/lib/lib_alchemistmodule.so
>>
>>Padraig.
>>







More information about the ILUG mailing list