Unexpected results with lists in GNU sed

I have been living with this problem with GNU sed v4.1.4 for a long time, but now I really need to figure it out.

When using a list in either an address or a search, the expression [a-z] is matching lower and upper-case letters. [A-Z] works as it should.

For example, if I run

sed -nr "/[a-z]/ p" temp.txt

Where temp.txt contains only

a
b
c
A
B
C

it matches every line.
If I run it using [A-Z] it matches only the upper-case letters as I would expect. Also, when I run

grep "/[a-z]/" temp.txt

It matches only the lower-case letters again as I expect. It seems only [a-z] is affected, and only with sed.

Does anyone know why this is? Maybe there is something wrong with the way I have the system set up, but everything else seems to be working ok.

Matt

hmm.. i am using 4.2.1 version and i dont see any issues.

$ sed -nr '/[a-z]/ p' temp.txt 
a
b
c

$ sed -n '/[a-z]/ p' temp.txt 
a
b
c

1) The first sed expression you gave does not match upper-case characters when I test it.

2) grep does not use / / as regular expression beginning and end markers; your grep expression couldn't possibly have worked.

So, unless your version of sed is extremely strange, I think you've simplified the problem a little too far, reducing it into something which doesn't actually demonstrate your problem. Could you post the actual expression you're having problems with please?

What happens if you use:

sed -nr "/[[:lower:]]/ p" temp.txt

Corona, sorry I got careless cutting and pasting, the grep command was

grep "[a-z]" temp2.txt

I just tried it again and the result was correct.

The sed example in the post is an example of an expression that exhibits the problem on my system (apparently not others'). I verified that also.

Here is something that is easier to cut and paste to the command line.

echo abAB | sed -r "s/[a-z]/x/g"

The result is xxxx on my machine. Not the expected xxAB.

Perhaps I do have a strange version of sed. One thought is whether it could be that the regex library not correct for the sed version. I don't know if that is the case or how to check.

Scrutinizer, Yes it works with classes, but that doesn't solve the problem.

If it really is a bug in sed, the obvious thing to do would be to upgrade your version of sed...

If your system has busybox on it, it may be a viable alternative.

$ busybox sed

Usage: sed [-efinr] SED_CMD [FILE]...

Options:
        -e CMD  Add CMD to sed commands to be executed
        -f FILE Add FILE contents to sed commands to be executed
        -i      Edit files in-place
        -n      Suppress automatic printing of pattern space
        -r      Use extended regex syntax

If no -e or -f is given, the first non-option argument is taken as the sed
command to interpret. All remaining arguments are names of input files; if no
input files are specified, then the standard input is read. Source files
will not be modified unless -i option is given.

$

I was thinking this might be a locale issue, therefore I suggested trying [[:lower:]] instead in post #4:

sed, a stream editor, 7 Reporting Bugs


  1. a-z ↩︎

Thanks everyone who commented on this. I updated the package and it works right now.

Matt