LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep command for uppercase (https://www.linuxquestions.org/questions/linux-newbie-8/grep-command-for-uppercase-934954/)

Micky12345 03-17-2012 11:32 AM

grep command for uppercase
 
i wrote a command
Code:

grep '^[[:upper:]]' file1
I want to find lines of file1 which contains only capital letters , but when i execute above command

i got lines starting with capital letter but after that it may or maynot contain capital letters

contents of my file1 are
Code:

HELLO
hello
Hello
HEllO
HOW

IT shows output as
HELLO
Hello
HEllo
HOW


But its not what i am expecting i want only output as
HELLO
HOW

colucix 03-17-2012 11:38 AM

Your regular expression matches a single character at the beginning of each line. Instead, you need to match any number of upper-case characters till the end of the line (add the $ anchor):
Code:

grep '^[[:upper:]]*$' file1
Edit: the command above matches empty lines, as well. To match only lines containing at least one upper-case character, use the extended regular expression option and the + modifier:
Code:

grep -E '^[[:upper:]]+$' file1


All times are GMT -5. The time now is 11:53 PM.