1 / 10

Strings in BASH

Strings in BASH. Variables are strings by default We are going to look at some ways to manipulate strings in BASH. String Length. ${# string} expr length $string These are the equivalent of strlen () in C . expr "$string" : '.*' stringZ =abcABC123ABCabc

urian
Download Presentation

Strings in BASH

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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} exprlength $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 # 123456 ... echo `expr index "$stringZ" C12` # 6 # C position. echo `expr index "$stringZ" c` # 3

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

  6. stringZ=abcABC123ABCabc # 0123456789..... # 0-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!!

More Related