Write the commands to count the number of characters stored in the shell variable text. Then write the commands to count all the alphabetic characters. (Hint: Use sed and wc.) What happens to special character sequences such as \n if they're stored inside text?

What will be an ideal response?


First command:
count=$(echo $text | wc -c)
Technically the wc -c command actually returns a count of all characters plus the end of line character too, so bonus points for an answer that takes that into account like:
count="$(echo $text | wc -c)" ; count=$(( $count - 1 ))
You can also accomplish this with a shell variable trick too: count=${#text}
Second script:
count="$(echo $text | sed 's/[^a-zA-Z]//g' | wc -c)"
With the same caveat about that extra character for the end of line. Special characters like \n are interpreted as a sequence of two individual characters, the \ is removed as not being alphabetical and it's counted as an n, a single character
Also, a more portable way to solve this problem is to use the language-specific character set notation of [:alpha:] instead of an explicit range. This allows languages like Spanish and German to be properly processed and analyzed:
count="$(echo $text | sed 's/[^[:alpha:]]//g' | wc -c)"

Computer Science & Information Technology

You might also like to view...

This is a version of VoIP. It is designed to make and deliver phone calls.

What will be an ideal response?

Computer Science & Information Technology

What should you install in Windows Server 2008 R2 that provides increased availability of applications?

A. Failover Clustering B. System Center C. Microsoft Storage Server D. Active Directory

Computer Science & Information Technology

Which of the following states does not have security breach law?

A. Alabama B. Texas C. California D. Massachusetts

Computer Science & Information Technology

Each letter of the word Mississippi is placed on a piece of paper and all 11 pieces of paper are placed in a hat. Three letters are selected at random from the hat and are replaced. Find the probability that the first letter selected is a vowel, the second letter is a consonant, and the third letter is a consonant.

A.
B.
C.
D.
E.

Computer Science & Information Technology