LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-27-2012, 04:44 AM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 233

Rep: Reputation: 6
why does grep read [a-z] as [A-Za-z]?


the issue:

Code:
teo@bolide:~$ echo "A" | grep "[a-z]"
A
teo@bolide:~$ echo "a" | grep "[A-Z]"
teo@bolide:~$

my system:
ubuntu 10.10 2.6.35-32
grep-2.6.3-3

Code:
teo@bolide:~$ echo $GREP_OPTIONS

teo@bolide:~$ alias grep
bash: alias: grep: not found
teo@bolide:~$ type grep
grep is /bin/grep
can anybody help me?
 
Old 04-27-2012, 04:49 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,

It depends on the locale (have a look at the man page).

Try this:
Code:
LANG=C echo "A" | grep "[a-z]"
Hope this helps.
 
Old 04-27-2012, 05:00 AM   #3
masavini
Member
 
Registered: Jun 2008
Posts: 233

Original Poster
Rep: Reputation: 6
i'm afraid id doesn't...

Code:
teo@bolide:~$ LANG=C echo "A" | grep "[a-z]"
A
 
Old 04-27-2012, 05:05 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,

How about this:
Code:
LC_ALL=C echo "A" | grep "[a-z]"
Hope this helps.
 
Old 04-27-2012, 05:11 AM   #5
masavini
Member
 
Registered: Jun 2008
Posts: 233

Original Poster
Rep: Reputation: 6
sorry, it still does not...

Code:
teo@bolide:~$ LC_ALL=C echo "A" | grep "[a-z]"
A
 
Old 04-27-2012, 05:15 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,

Strange, it should work. Quote from the grep man page:
Quote:
Within a bracket expression, a range expression consists of two charac-
ters separated by a hyphen. It matches any single character that sorts
between the two characters, inclusive, using the locale's collating
sequence and character set. For example, in the default C locale,
[a-d] is equivalent to [abcd]. Many locales sort characters in dictio-
nary order, and in these locales [a-d] is typically not equivalent to
[abcd]; it might be equivalent to [aBbCcDd], for example. To obtain
the traditional interpretation of bracket expressions, you can use the
C locale by setting the LC_ALL environment variable to the value C.
- What is your locale set to (execute locale and post the output).
- Which shell are you using?
 
Old 04-27-2012, 05:20 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi again,

I did some testing and it seems you need to export the LC_ALL setting to get this to work:
Code:
export LC_ALL=C echo "A" | grep "[a-z]"
Hope this helps.
 
1 members found this post helpful.
Old 04-27-2012, 05:24 AM   #8
masavini
Member
 
Registered: Jun 2008
Posts: 233

Original Poster
Rep: Reputation: 6
solved with:

Code:
echo "export LC_ALL=\"C\"" >> ~/.bashrc
thank you for your help...
 
Old 04-27-2012, 05:30 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,
Quote:
Originally Posted by masavini View Post
solved with:

Code:
echo "export LC_ALL=\"C\"" >> ~/.bashrc
Doing that will also set all the other LC_ entries to C, which might have side effects you don't want.

If you do not want to change the other LC_ settings you might want to add the export LC_ALL="C" to the front of your one-liners or as first entry after the hash-bang in a script (and not adding it to .bashrc).

Quote:
thank you for your help...
You're welcome
 
1 members found this post helpful.
Old 04-27-2012, 06:41 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
grep is the program that's affected by the environment setting, but as it's located in a pipe, it's running in a subshell. You have to export a value in order for the sub-process to copy it into its environment.

Or you could pass the value directly to the command that needs it instead:

Code:
echo "A" | LC_ALL=C grep "[a-z]"
By the way, what is your default language setting anyway? The locale command should tell you, if you don't know. AFAICT, the [a-z] range doesn't include upper case letters in the UTF-8 environment, which is the best encoding to use for international text support, and which most distros use by default these days.
 
Old 04-27-2012, 09:21 AM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,939

Rep: Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717
Quote:
Originally Posted by druuna View Post
Hi,

How about this:
Code:
LC_ALL=C echo "A" | grep "[a-z]"
Hope this helps.
That applies "LC_ALL=C" to the echo command, not the grep command. This should work:
Code:
echo "A" | LC_ALL=C grep "[a-z]"
 
Old 04-27-2012, 11:47 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Isn't that what I just said?
 
Old 04-27-2012, 02:01 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,939

Rep: Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717
Quote:
Originally Posted by David the H. View Post
Isn't that what I just said?
Oops, yes. I confess I didn't read much past "You have to export a value ...".
 
Old 04-27-2012, 03:13 PM   #14
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
grep offers predefined character classes. Does echo A | grep "[[:lower:]]" work for you?
 
Old 04-28-2012, 04:21 AM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by rknichols View Post
Oops, yes. I confess I didn't read much past "You have to export a value ...".
Eh, no problem. We all do it from time to time. I just screwed up in a similar fashion myself on another thread.


@PTrenholme: Good thinking. To be accurate, however, character classes like that are standard to regular expressions, and can be used in most regex-supporting programs, not just grep. I believe they can also be affected by the environment's sorting order too, in some cases.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read files from a folder and grep a string bhagirathi Programming 6 07-06-2009 06:27 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM
simple read $a / grep question sycamorex Programming 2 01-08-2008 12:07 PM
Grep command to read a file Sherlock Programming 2 02-02-2007 04:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration