buggrep - Bugs: bug #34772, Backslash escape character ignored...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #34772: Backslash escape character ignored for \] inside [ ]

Submitted by:  Thomas Käfer <kaefert>
Submitted on:  Tue 08 Nov 2011 06:49:03 AM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: Invalid
Privacy: PublicAssigned to: None
Open/Closed: Closed

Wed 28 Dec 2011 02:13:23 PM UTC, comment #3:

"globs" is what the shell uses: foo.* expands to any file with the first four bytes of "foo." and any number of trailing bytes.
"BRE" is Basic Regular Expression, which is what grep uses: foo.* expands to any string with the first three bytes of "foo", followed by the '.' meta-character which matches any character, occurring zero or more times.

Eric Blake <ericb>
Project Member
Sun 13 Nov 2011 10:54:09 AM UTC, comment #2:

Ups.. Okey, thanks Eric for that detailed explanation of what my misunderstanding was. I didn't know that there are definitions of regex syntax that don't contain \ as escape character no matter where in the regex it occurs.

Also a huge thanks for the hint with grep -v and the find command, although in that particular case I didn't wanted to have recursion. (but what are "globs" & "BRE"?)

Thomas Käfer <kaefert>
Tue 08 Nov 2011 06:27:52 PM UTC, comment #1:

I'm closing this as invalid - the only bug here is your mis-understanding of the documentation.

POSIX requires grep to handle Basic Regular Expressions, as defined at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html

In that definition, section 9.3.5 specifies how a bracket expression behaves. Among other things, "The <right-square-bracket> ( ']' ) shall lose its special meaning and represent itself in a bracket expression if it occurs first in the list (after an initial <circumflex> ( '^' ), if any). Otherwise, it shall terminate the bracket expression, unless it appears in a collating symbol (such as "[.].]" ) or is the ending <right-square-bracket> for a collating symbol, equivalence class, or character class. The special characters '.' , '*' , '[' , and '\\' ( <period>, <asterisk>, <left-square-bracket>, and <backslash>, respectively) shall lose their special meaning within a bracket expression."

Thus, "^[^\]]*$' is the regular expression that searches for beginning of line, followed by any number of characters that match the bracket expression [^\] (basically, any character except backslash), followed by a literal right bracket, followed by end of line, and grep is correctly interpreting this.

If you want to find all lines that don't contain ], then use the regular expression "^[^]]$", which is the beginning of line, followed by the bracket expression [^]] (any number of non-right-bracket characters), followed by end of line.

Your "counterexample" of "^[^\[]*$" was not all lines that don't contain [, but rather all lines that contain neither [ nor backslash.

Other things to think about: why not use grep -v to invert which lines are printed? By doing that, you no longer have to use ^, *, or $, since a single ] anywhere in the line excludes that entire line:

ls | grep -v ]

Also, ls | grep is inefficient, especially if you want directory recursion. You may want to consider using find for the job, especially since that gives you recursion capabilities (although then you are dealing with globs rather than BRE):

find . ! -name ']'

Eric Blake <ericb>
Project Member
Tue 08 Nov 2011 06:49:03 AM UTC, original submission:

ls | grep "^[^\]]*$"
should give alls folders and files that don't contain the character ]
--> this does not work.

ls | grep "^[^\[]*$"
This however does correclty give all lines that dont contain the character [

grep -V
GNU grep 2.5.4

Thomas Käfer <kaefert>

 

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by ericb (Posted a comment)
  • -unavailable- added by kaefert (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 4 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Wed 28 Dec 2011 02:13:23 PM UTCericbSummaryBackslash escape character ignored for \\] inside [ ]=>Backslash escape character ignored for \] inside [ ]
    Tue 08 Nov 2011 06:27:52 PM UTCericbStatusNone=>Invalid
      Open/ClosedOpen=>Closed
      SummaryBackslash escape character ignored for \\] inside [ ]=>Backslash escape character ignored for \] inside [ ]

    Back to the top


    Powered by Savane 3.1-cleanup1