Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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:

    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
    I am not getting what is wrong. I am not getting answer. Is anyone can help me?

    Best Regards,

  2. #2
    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

  3. #3
    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.

    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
    Can you help me to short out this problem?

    Thanking You,

  4. $spacer_open
    $spacer_close
  5. #4
    My code works for me...
    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 ~]$
    Perhaps show your text file and how you are calling it.

  6. #5
    Thanks for your quick reply. I have solved my code. now it's working fine. Thanks once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •