Operating System - HP-UX
1748027 Members
4936 Online
108757 Solutions
New Discussion юеВ

Grep for strings in all caps?

 
SOLVED
Go to solution
dev44
Regular Advisor

Grep for strings in all caps?

Hi,

I have a file like this:

/directory/path/AAA/file.txt
/directory/path/bbb/file.txt
/directory/path/CCC/file.txt

I want to grep the file for anything in all caps. So that I would only get the output for AAA and CCC but not bbb because there are no caps. Is this possible?
whatever
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Grep for strings in all caps?

Hi:

Simply:

# grep [A-Z] file

or better:

# grep [[:upper:]] file

Regards!

...JRF...
dev44
Regular Advisor

Re: Grep for strings in all caps?

Perfect. Thanks James.
whatever
Steven Schweda
Honored Contributor

Re: Grep for strings in all caps?

> [...] anything in all caps.

"All caps" and "any caps" are not the same
thing. "AaA" is not "all caps". Different
regular expressions can distinguish among
the many possibilities.
OldSchool
Honored Contributor

Re: Grep for strings in all caps?

"/directory/path/AAA/file.txt
/directory/path/bbb/file.txt
/directory/path/CCC/file.txt

I want to grep the file for anything in all caps. So that I would only get the output for AAA and CCC but not bbb because there are no caps. Is this possible? "

of course, none of the above qualify as "all caps", grepping for [:upper:] will find any caps.
Bill Hassell
Honored Contributor

Re: Grep for strings in all caps?

Your requirement appears to be much more specialized. You want to grep for a directory that is all caps, possibly surrounded by lowercase directories and filenames. It's easy to see the caps in the example but grep doesn't care about slashes. The -w option will limit matches to only those items between slashes. The problem is that you'll need to create a regular expression that matches a variable number of UPPERCASE letters.


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: Grep for strings in all caps?

> Your requirement appears to be much more
> specialized.

I'd say that was not clearly specified.

> You want to [...]

I can only marvel at the psychic abilities
of anyone who knows what the original poster
wants. (Except, perhaps, the OP himself.)

Given the vagueness and/or ambiguity and/or
incompleteness of the problem statement, I'd
prefer to wait for something better before
claiming to have The Solution.

> The problem is that you'll need to create
> a regular expression that matches a
> variable number of UPPERCASE letters.

That may be _a_ problem, but something so
simple as
/[A-Z]*/
might do the job. Not knowing what the job
actually is makes it hard to say.

> Perfect. [...]

But, hey. If he's happy, who am I to argue?