Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings in BASH.

Similar presentations


Presentation on theme: "Strings in BASH."— Presentation transcript:

1 Strings in BASH

2 Variables are strings by default
We are going to look at some ways to manipulate strings in BASH

3 String Length ${#string} expr length $string These are the equivalent of strlen() in C. expr "$string" : '.*' stringZ=abcABC123ABCabc echo ${#stringZ} # 15 echo `expr length $stringZ` # 15 echo `expr "$stringZ" : '.*'` # 15

4 Index expr index $string $substring
Numerical position in $string of first character in $substring that matches. stringZ=abcABC123ABCabc # echo `expr index "$stringZ" C12` # 5 # C position. echo `expr index "$stringZ" c` #2

5 Substring Extraction ${string:position} Extracts substring from $string at $position to the end of the string. ${string:position:length} Extracts $length characters of substring from $string at $position.

6 stringZ=abcABC123ABCabc
# # based indexing. echo ${stringZ:0} # abcABC123ABCabc echo ${stringZ:1} # bcABC123ABCabc echo ${stringZ:7} # 23ABCabc echo ${stringZ:7:3} # 23A # Three characters of substring. # Is it possible to index from the right end of the string? echo ${stringZ:-4} # abcABC123ABCabc # Defaults to full string, as in ${parameter:-default}. # However . . . echo ${stringZ:(-4)} # Cabc echo ${stringZ: -4} # Cabc The position and length arguments can be "parameterized," that is, represented as a variable, rather than as a numerical constant.

7 Substring Removal ${string#substring}
Deletes shortest match of $substring from front of $string. ${string##substring} Deletes longest match of $substring from front of $string. stringZ=abcABC123ABCabc # |----| shortest # | | longest echo ${stringZ#a*C} # 123ABCabc # Strip out shortest match between 'a' and 'C'. echo ${stringZ##a*C} # abc # Strip out longest match between 'a' and 'C'.

8 Substring Replacement
${string/substring/replacement} Replace first match of $substring with $replacement. ${string//substring/replacement} Replace all matches of $substring with $replacement.

9 stringZ=abcABC123ABCabc echo ${stringZ/abc/xyz} # xyzABC123ABCabc # Replaces first match of 'abc' with 'xyz'. echo ${stringZ//abc/xyz} # xyzABC123ABCxyz # Replaces all matches of 'abc' with # 'xyz'. echo "$stringZ" # abcABC123ABCabc # The string itself is not altered! # Can the match and replacement strings be parameterized? match=abc repl=000 echo ${stringZ/$match/$repl} # 000ABC123ABCabc # ^ ^ ^^^ echo ${stringZ//$match/$repl} # 000ABC123ABC000 # Yes! ^ ^ ^^^ ^^^ echo # What happens if no $replacement string is supplied? echo ${stringZ/abc} # ABC123ABCabc echo ${stringZ//abc} # ABC123ABC # A simple deletion takes place.

10 Palindrome Assignment
Write a BASH shell script to determine if a word, supplied as input from the user, is a palindrome or not. Remember a palindrome is a word that is the same backwards as it is forwards. For example: racecar, noon, mom are palindromes. Once you have accomplished this you can determine if a sentence is a palindrome – you have to strip off punctuation and spaces and then test to see if it is a palindrome. For example, Madam, I’m Adam, and Go hang a salami, I’m a lasagna hog, are both palindromes. Have fun!!


Download ppt "Strings in BASH."

Similar presentations


Ads by Google