Tux

...making Linux just a little more fun!

2-cent Tip: using class of characters in grep

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Wed, 16 Dec 2009 22:59:28 +0700

How do you catch a range or group of characters when using grep? Of course you can always do something like:

$ grep -E '[a-z]+' ./test.txt
to show lines in file "test.txt" that contain at least single character between 'a' to z in lower case.

But there is other way (and hopefully more intuitive...for some people). Let's do something like above, but the other way around. Tell grep to show the name of text files that doesn't contain upper case characters

$ grep -E -v -l '[[:upper:]]+' ./*
Here, upper is another way of saying [A-Z]. Note the usage of double brackets! I got trapped once, thinking that I should only use single bracket and wonder why it didn't work....

There are more classes you can use. Check "info grep" in section 5.1 "Character class"

-- 
regards,
 
Mulyadi Santosa
Freelance Linux trainer and consultant
 
blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Wed, 16 Dec 2009 16:43:28 +0000

2009/12/16 Mulyadi Santosa <[email protected]>:

> How do you catch a range or group of characters when using grep? Of
> course you can always do something like:
> $ grep -E '[a-z]+' ./test.txt
> to show lines in file "test.txt" that contain at least single
> character between 'a' to z in lower case.
>
> But there is other way (and hopefully more intuitive...for some
> people). Let's do something like above, but the other way around. Tell
> grep to show the name of text files that doesn't contain upper case
> characters
> $ grep -E -v -l '[[:upper:]]+' ./*
> Here, upper is another way of saying [A-Z]. Note the usage of double
> brackets! I got trapped once, thinking that I should only use single
> bracket and wonder why it didn't work....

There's also the '-P' ('Perl-like') switch that's kinda hit-and-miss, but at least manages to handle Unicode named properties ('\p{Lu}' for upper case, etc.)

-- 
<Leftmost> jimregan, that's because deep inside you, you are evil.
<Leftmost> Also not-so-deep inside you.


Top    Back