Re: problems with grep



On 2006-11-15, ltarc3@xxxxxxxxx wrote:
I'm having a weird problem with grep. The context isn't that
important, but I will give it to you anyway.

I want to write a script that will look at other scripts and find
variables that are all caps, underscores, and digits. I had a fairly
complicated command with seds and awks and greps that was giving me
problems. I worked my way back to the simplest of greps where I am
just trying to match one uppercase character and now I am absolutely
stymied.

I've run this on FC3, CentOS 4.3.

Am I missing something obvious?? Here is an example:

[root@myhost tmp]# cat testfile
ABC=
abc=
XYZ=
xyz=
123
ABC_123
abc_123
XYZ_123
xyz_123

[root@myhost tmp]# grep "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]" testfile
ABC=
XYZ=
ABC_123
XYZ_123

[root@myhost tmp]# grep "[A-Z]" testfile
ABC=
abc=
XYZ=
xyz=
ABC_123
abc_123
XYZ_123
xyz_123

Why is this matching lower-case strings?

Presumably because your locale folds interleaves and lower case to
emulate case insensitivity.

[root@myhost tmp]# grep "^[A-Z_0-9]" testfile
ABC=
XYZ=
xyz=
123
ABC_123
XYZ_123
xyz_123

And why does this match xyz but not abc ????

Your collation sequence is probably aAbBcCdDeE....xXyYzZ, so [A-Z]
does not include "a".

It's just so weird.

I always use LC_ALL=C.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.



Relevant Pages

  • Re: problems with grep
    ... I want to write a script that will look at other scripts and find ... And why does this match xyz but not abc ???? ...
    (comp.unix.shell)
  • Re: Delete mails by subject using mailx ?
    ... mailbox having subject xyz" ... I want to run this using a crontab scheduled script. ... echo "oldmailbox is locked. ... # replace the oldmailbox with newmailbox. ...
    (comp.mail.misc)
  • Re: script to take backup
    ... >> i want to create a script to take backup of specific directories. ... >> how can i list only abc, xyz and bcd directories in the search. ...
    (comp.unix.aix)
  • Expect: spawned process in sleep state (log_file problem?)
    ... I have a working Expect script which spawns another tcl script ... #-- First, spawn the xyz script ... puts "abc is $abc" ... the xyz script ran in background. ...
    (comp.lang.tcl)
  • Re: ksh script to parse name/value pairs
    ... On 2009-04-14, inetquestion wrote: ... I started working on the script below, but felt I was going in the ... data="987654321 xyz ... Each name-value pair is now a positional parameter: ...
    (comp.unix.shell)