grep / sed + regex : possible bug ?
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
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
.* : lets start with whatsoever
/Name/ : followed by (verbatim) /Name/ (/is no special character in
[^/][^]* : followed by anything except '/' with a length of at least 1
So I put the above list into a file called x
cat x | grep '/Name/[^/][^/]*'
blaba/Name/aaa/1 : _/_1 not in regex, strange
blaba/Name/bb/3 : _/_3 not in regex, strange
blaba/Name/Ccccc/5 : _/_5 not in regex, strange
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
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
i. e., the whole list, altho no / may follow the /Name/.
Maybe somebody can shed some light on this murky matter.
(As I am subscribed to the list, you needn't reply by personal e-mail)