Results 1 to 5 of 5
Hello Guys,
I am writing a script which check each line for how many Uppercase and Lowercase letter of a given file. Please check my script as follow:
Code:
l=0
...
- 11-09-2011 #1Just Joined!
- Join Date
- Oct 2011
- Posts
- 17
Trying to find number of uppercase and lowercase letters.
Hello Guys,
I am writing a script which check each line for how many Uppercase and Lowercase letter of a given file. Please check my script as follow:
I am not getting what is wrong. I am not getting answer. Is anyone can help me?Code:l=0 while read line do echo Line `expr $l + 1` has ` tr -cd "[A-Z]" < $line | wc -c` uppercase letters and `tr -cd "[a-z]" < $line | wc -c` lowercase letters. l=`expr $l + 1` done < $1
Best Regards,
- 11-10-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
How about using grep instead of tr?
Code:#!/bin/bash [ $# -ne 1 ] && echo Give me a file && exit 1 file=$1 i=1 while read line; do printf "\nLine $i: \"$line\"\n" printf "Lower case: " echo $line|/bin/grep -o [a-z]|wc -l printf "Upper case: " echo $line|/bin/grep -o [A-Z]|wc -l i=$(( $i + 1 )) done < $file
- 11-10-2011 #3Just Joined!
- Join Date
- Oct 2011
- Posts
- 17
Thanks atreyu, but your code showing No match found. My folllowing code is working partially. It is skiping some lines and also giving either Lowercase or Uppercase.
Can you help me to short out this problem?Code:l=0 while read line do lo=`line | tr -cd "[a-z]" | wc -c` echo $lo echo Line `expr $l + 1` has `line | tr -cd "[A-Z]" | wc -c` uppercase letters an d `line | tr -cd "[a-z]" | wc -c` lowercase letters. l=`expr $l + 1` done < $1
Thanking You,
- 11-10-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
My code works for me...
Perhaps show your text file and how you are calling it.Code:[user@host ~]$ [user@host ~]$ cat test.sh #!/bin/bash [ $# -ne 1 ] && echo Give me a file && exit 1 file=$1 i=1 while read line; do printf "\nLine $i: \"$line\"\n" printf "Lower case: " echo $line|/bin/grep -o [a-z]|wc -l printf "Upper case: " echo $line|/bin/grep -o [A-Z]|wc -l i=$(( $i + 1 )) done < $file [user@host ~]$ [user@host ~]$ cat blah.txt AAAAAAAAAAAAAa bb ccCC dddDDD asdfaslkdsdfas [user@host ~]$ [user@host ~]$ sh test.sh blah.txt Line 1: "AAAAAAAAAAAAAa" Lower case: 1 Upper case: 13 Line 2: "bb" Lower case: 2 Upper case: 0 Line 3: "ccCC" Lower case: 2 Upper case: 2 Line 4: "dddDDD" Lower case: 3 Upper case: 3 Line 5: "" Lower case: 0 Upper case: 0 Line 6: "asdfaslkdsdfas" Lower case: 14 Upper case: 0 [user@host ~]$
- 11-10-2011 #5Just Joined!
- Join Date
- Oct 2011
- Posts
- 17
Thanks for your quick reply. I have solved my code. now it's working fine. Thanks once again.


Reply With Quote