grep / sed + regex : possible bug ?
Hi everybody
I have a bunch of directories where somewhere in the path exists ONE and
only one directory with a certain Name
so the whole path would look something like .*/Name/.*. Unfortunately I
do not know how many directories will precede /Name/ so all I can do is
to check for the number of dirs following /Name/
Now I want to find all strings (as delivered by find) / directories
ending in Name/one_dir_only
.*/Name will also not be acceptable, as it is not followed by a subdir.
So of the following list I would like to extract those marked with a *
* blaba/Name/aaa : ending in /Name/whatsoever with no further /
blaba/Name/aaa/1 : Names/aaa/1 does not fit the pattern : 1 dir too
many
blaba/name/aaa/2 : as does aaa/2 : same
* blaba/Name/bb : ending in /Name/whatsoever with no further /
blaba/Name/bb/3 : /3 unwanted
blaba/Name/Ccccc/5 : /5 unwanted
* blaba/Name/Cc&DD : ending in /Name/whatsoever with no further /
blaba/Name/Cc&DD/2 : /2 unwanted
(find might deliver ./blabla/.... but that's includes in .*/Name/)
so an easy regex would be .*/Name/[^/][^/]* for "easy" regex
or .*/Name/[^/]+ for extended regex
read
.* : lets start with whatsoever
/Name/ : followed by (verbatim) /Name/ (/is no special character in
regexes)
[^/][^]* : followed by anything except '/' with a length of at least 1
(first [^/])
So I put the above list into a file called x
x :
blaba/Name/aaa
blaba/Name/aaa/1
blaba/name/aaa/2
blaba/Name/bb
blaba/Name/bb/3
blaba/Name/Ccccc/5
blaba/Name/Cc&DD
blaba/Name/Cc&DD/2
but
cat x | grep '/Name/[^/][^/]*'
yields
blaba/Name/aaa
blaba/Name/aaa/1 : _/_1 not in regex, strange
blaba/Name/bb
blaba/Name/bb/3 : _/_3 not in regex, strange
blaba/Name/Ccccc/5 : _/_5 not in regex, strange
blaba/Name/Cc&DD
blaba/Name/Cc&DD/2 : _/_2 not in regex, strange
however and quite strangely
cat x | grep '/Name/[^/][^/]*$'
as well as
cat x | grep '^.*/Name/[^/][^/]*$'
deliver the correct result, namely
blaba/Name/aaa
blaba/Name/bb
blaba/Name/Cc&DD
but what does $ (EOL) have to do with it.
Looks like a bug in grep.
The same sad result also comes from sed
cat x | sed -ne '/.*\/Names\/[^/][^/]*/p'
cat x | sed -ne '/.*\/Names\/[^/][^/]*$/p' # with EOL
cat x | sed -ne '/.*\/Names\/[^/]\+/p' # with + (masked)
cat x | sed -ne '/\/Names\/[^/]\+/p' # prefix omitted
all yield
blaba/Names/aaa
blaba/Names/aaa/1
blaba/names/aaa/2
blaba/Names/bb
blaba/Names/bb/3
blaba/Names/Ccccc/5
blaba/Names/Cc&DD
blaba/Names/Cc&DD/2
i. e., the whole list, altho no / may follow the /Name/.
Maybe somebody can shed some light on this murky matter.
TIA
Axel Schlicht
(As I am subscribed to the list, you needn't reply by personal e-mail)
Reply to: