06. Arrays and Strings (Part 2)
Strings
- Strings are characters that are grouped together as one data value
- Strings have space as delimiters, so if spaces are part of a string, use quotes to preserve the spaces and keep everything within the quotes as one string
- When working with strings, it is often necessary to edit or process only a part of a string
- Even though it is easy on the command line to use vi to edit a string of characters, it is not possible to do so in a script
- In a script, some utilities are often used to edit strings:
- sed, combined with regular expressions
- tr
- awk, when the string has a regular delimiter
- The bash shell also has a set of string operations that can be used in a script to manipulate strings
String Length
- To get the length (the number of characters) of a string:
${#stringName}
- Example:
name=“abc def”
echo $name print abc def
echo ${#name} print 7
- Review
- Note that “abc def” above needs to be in quotes because of the space character
- Without quotes, abc will be stored in name, and def will cause an error
- To store both abc and def without using quotes, we need to use an array: name=( abc def )
- name[0] will be abc and name[1] will be def
Position of a Substring
- To find the position of the first character in a substring within a string:
expr index $string $substring
- the value returned is the position of the first character of the substring that can be found in the string
- position in a string starts at 1
- Example:
str=“123456789”
expr index “$str” 234 print 2 (not index 1)
expr index “$str” 432 print 2 (2 is the first character found)
Substring Extraction by Index
- Just like with a filter, the extraction copies out a substring from the string. It does not change the string itself.
- To extract a substring from a string:
${stringName:$startIndex:$length}
- startIndex is the index to start the extraction. It can be a value in a variable
- length is the number of characters extracted. It can be a value in a variable
- if there is no length, then extract to the end of the string
- index of characters starts at 0
- Example:
str=“123456789”
echo ${str:4:3} print 567
echo ${str:4} print 56789
Substring Extraction with Shortest Match
- Just like with a filter, the extraction copies out a substring from the string. It does not change the string itself.
- To extract a substring from a string with shortest match, starting at the beginning of the string:
${stringName#pattern}
- To extract a substring from a string with shortest match, starting at the end of the string:
${stringName%pattern}
- To be extracted, the substring must match pattern, and pattern can contain wildcards (not regex)
- Example:
str=“my.script.example.sh”
echo ${str#*.} print script.example.sh - my. is the shortest substring that matches the *. pattern and is extracted
echo ${str%.*} print my.script.example - .sh is the shortest substring that matches the pattern .* and is extracted
Substring Extraction with Longest Match
- Just like with a filter, the extraction copies out a substring from the string. It does not change the string itself.
- To extract a substring from a string for with longest match, starting at the beginning of the string:
${stringName##pattern}
- To extract a substring from a string for with longest match, starting at the end of the string:
${stringName%%pattern}
- To be extracted, the substring must match pattern
- pattern can contain wildcards (not regex)
- Example:
str=“my.script.example.sh”
echo ${str##*.} print sh - my.script.example. is the longest substring that matches the *. pattern
echo ${str%%.*} print my - .script.example.sh is the longest substring that matches the pattern .*
Substring Substitution
- Just like with a filter, the substitution works with a copy of the string. It does not change the string itself.
- To substitute a substring at the first match:
${stringName/pattern/replacementStr}
- pattern is the pattern that substring must match. pattern can contain wildcards (not regex)
- replacementStr is the substring that will replace the part of the string that matches the pattern
- Example:
str=“myscriptexample.sh”
echo ${str/e/E} # print myscriptExample.sh - note that only first e matches, second e is ignored
echo ${str/./} # print myscriptexamplesh - replaced . with nothing, or delete .
- To substitute globally multiple substrings in the string:
${stringName//pattern/replacementStr}
- pattern is the pattern that substring must match. pattern can contain wildcards (not regex)
- replacementStr is the substring that will replace the part of the string that matches the pattern
- Example:
str=“this is cis 18c”
echo ${str//is/IS} # print thIS IS cIS 18c - all is match the pattern and are replaced with IS
echo ${str// /-} # print this-is-cis-18c - all spaces match the pattern and are replaced with -
Substring Substitution at Begin or End of String
- To substitute when a substring at the beginning the string matches:
${stringName/#pattern/replacementStr}
- pattern is the pattern that substring at the beginning of the string must match. pattern can contain wildcards (not regex)
- replacementStr is the substring that will replace the matched substring
- To substitute when a substring at the end the string matches:
${stringName/%pattern/replacementStr}
- pattern is the pattern that substring at the beginning of the string must match. pattern can contain wildcards (not regex)
- replacementStr is the substring that will replace the matched substring
- Example:
str=“my.script.example”
echo ${str/#my/your} print your.script.example
echo ${str/%my/your} print my.script.example (no substitute since there is no match)