STRING HANDLING IN SHELL SCRIPT

 Calculate the length of a String echo "Enter the string..." read text # Process 1 length=\${#text} echo "Length = \$length" # Process 2 length=`echo \$text | wc -c` # It includes the new line character so you need to subtract 1 to get actual result length=`expr \$length - 1` echo "Length = \$length" # Process 3 # It gives an error if any space include the string length=`expr length \$text` echo "Length = \$length" Calculate the number of character and word present in a string echo "Enter the string....." read text c=`echo \$text | wc -c` w=`echo \$text | wc -w` echo "Number of Characters = \$c" echo "Number of word = \$w" Pick each Character from a String echo "Enter the string..." read text len=\${#text} for i in {1..\$len} do ch=`echo \$text | cut -c\$i` echo "\$ch" done Pick substrings from the given string text="Shell Programming" end=5 substring=`echo \$text | cut -c-\$end` echo "Substring 1 = \$substring" start=7 end=17 substring=`echo \$text | cut -c\$start-\$end` echo "Substring 2 = \$substring" Find the number of Vowel present in a string echo "Enter the string..." read text # Calculate the length of the given string the given string len=\${#text} vowel=0 for i in {1..\$len} do # Pick each character from the given string ch=`echo \$text | cut -c\$i` # Count number of vowel present in the given string case \$ch in [AaEeIiOoUu]) vowel=`expr \$vowel + 1` ;; esac done echo "Number of Vowel Present in the String = \$vowel" Find the number of Alphabet present in a string echo "Enter the string..." read text len=\${#text} alpha=0 for i in {1..\$len} do ch=`echo \$text | cut -c\$i` # Count number of alphabet present in the given string case \$ch in [a-z] | [A-Z]) alpha=`expr \$alpha + 1` ;; esac done echo "Number of Alphabet Present in the String = \$alpha" Find the number of Consonant present in a string echo "Enter the string..." read text len=\${#text} vowel=0 alpha=0 for i in {1..\$len} do ch=`echo \$text | cut -c\$i` # Count number of vowel present in the given string case \$ch in [AaEeIiOoUu]) vowel=`expr \$vowel + 1` ;; esac # Count number of alphabet present in the given string case \$ch in [a-z] | [A-Z]) alpha=`expr \$alpha + 1` ;; esac done consonant=`expr \$alpha - \$vowel` echo "Number of Consonant = \$consonant" Find the number of Digit and Space present in a string echo "Enter the string..." read text len=\${#text} digit=0 space=0 for i in {1..\$len} do ch=`echo \$text | cut -c\$i` # Count number of digit present in the given string case \$ch in [0-9]) digit=`expr \$digit + 1` ;; esac # Count number of space present in the given string if [ "\$ch" == " " ] then space=`expr \$space + 1` fi done echo "Number of Digit = \$digit" echo "Number of Space = \$space" Write a shell script to accept a string as input and convert the lower case characters in the string to upper case and upper case characters to lower case characters. Print the final string. echo "Enter the string....." read text len=\${#text} temp="" for i in {1..\$len} do ch=`echo \$text | cut -c\$i` case \$ch in # Convert upper case character to lower case character [A-Z]) ch=`echo \$ch | tr '[A-Z]' '[a-z]'` temp="\$temp\$ch" ;; # Convert lower case character to upper case character [a-z]) ch=`echo \$ch | tr '[a-z]' '[A-Z]'` temp="\$temp\$ch" ;; # Unchange other characters *) temp="\$temp\$ch" esac done echo "String = \$temp" Write a shell script to reverse a given string as input echo "Enter the string....." read text len=\${#text} temp="" for((i=\$len;i>=1;i--)) do ch=`echo \$text | cut -c\$i` temp="\$temp\$ch" done echo "After reverse the string is = \$temp"

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.

MORE TOPIC

WHAT WE DO

 Follow Us 