| 1 | #!/bin/bash |
| 2 | # array-strops.sh: String operations on arrays. |
| 3 | |
| 4 | # Script by Michael Zick. |
| 5 | # Used in ABS Guide with permission. |
| 6 | # Fixups: 05 May 08, 04 Aug 08. |
| 7 | |
| 8 | # In general, any string operation using the ${name ... } notation |
| 9 | #+ can be applied to all string elements in an array, |
| 10 | #+ with the ${name[@] ... } or ${name[*] ...} notation. |
| 11 | |
| 12 | |
| 13 | arrayZ=( one two three four five five ) |
| 14 | |
| 15 | echo |
| 16 | |
| 17 | # Trailing Substring Extraction |
| 18 | echo ${arrayZ[@]:0} # one two three four five five |
| 19 | # ^ All elements. |
| 20 | |
| 21 | echo ${arrayZ[@]:1} # two three four five five |
| 22 | # ^ All elements following element[0]. |
| 23 | |
| 24 | echo ${arrayZ[@]:1:2} # two three |
| 25 | # ^ Only the two elements after element[0]. |
| 26 | |
| 27 | echo "---------" |
| 28 | |
| 29 | |
| 30 | # Substring Removal |
| 31 | |
| 32 | # Removes shortest match from front of string(s). |
| 33 | |
| 34 | echo ${arrayZ[@]#f*r} # one two three five five |
| 35 | # ^ # Applied to all elements of the array. |
| 36 | # Matches "four" and removes it. |
| 37 | |
| 38 | # Longest match from front of string(s) |
| 39 | echo ${arrayZ[@]##t*e} # one two four five five |
| 40 | # ^^ # Applied to all elements of the array. |
| 41 | # Matches "three" and removes it. |
| 42 | |
| 43 | # Shortest match from back of string(s) |
| 44 | echo ${arrayZ[@]%h*e} # one two t four five five |
| 45 | # ^ # Applied to all elements of the array. |
| 46 | # Matches "hree" and removes it. |
| 47 | |
| 48 | # Longest match from back of string(s) |
| 49 | echo ${arrayZ[@]%%t*e} # one two four five five |
| 50 | # ^^ # Applied to all elements of the array. |
| 51 | # Matches "three" and removes it. |
| 52 | |
| 53 | echo "----------------------" |
| 54 | |
| 55 | |
| 56 | # Substring Replacement |
| 57 | |
| 58 | # Replace first occurrence of substring with replacement. |
| 59 | echo ${arrayZ[@]/fiv/XYZ} # one two three four XYZe XYZe |
| 60 | # ^ # Applied to all elements of the array. |
| 61 | |
| 62 | # Replace all occurrences of substring. |
| 63 | echo ${arrayZ[@]//iv/YY} # one two three four fYYe fYYe |
| 64 | # Applied to all elements of the array. |
| 65 | |
| 66 | # Delete all occurrences of substring. |
| 67 | # Not specifing a replacement defaults to 'delete' ... |
| 68 | echo ${arrayZ[@]//fi/} # one two three four ve ve |
| 69 | # ^^ # Applied to all elements of the array. |
| 70 | |
| 71 | # Replace front-end occurrences of substring. |
| 72 | echo ${arrayZ[@]/#fi/XY} # one two three four XYve XYve |
| 73 | # ^ # Applied to all elements of the array. |
| 74 | |
| 75 | # Replace back-end occurrences of substring. |
| 76 | echo ${arrayZ[@]/%ve/ZZ} # one two three four fiZZ fiZZ |
| 77 | # ^ # Applied to all elements of the array. |
| 78 | |
| 79 | echo ${arrayZ[@]/%o/XX} # one twXX three four five five |
| 80 | # ^ # Why? |
| 81 | |
| 82 | echo "-----------------------------" |
| 83 | |
| 84 | |
| 85 | replacement() { |
| 86 | echo -n "!!!" |
| 87 | } |
| 88 | |
| 89 | echo ${arrayZ[@]/%e/$(replacement)} |
| 90 | # ^ ^^^^^^^^^^^^^^ |
| 91 | # on!!! two thre!!! four fiv!!! fiv!!! |
| 92 | # The stdout of replacement() is the replacement string. |
| 93 | # Q.E.D: The replacement action is, in effect, an 'assignment.' |
| 94 | |
| 95 | echo "------------------------------------" |
| 96 | |
| 97 | # Accessing the "for-each": |
| 98 | echo ${arrayZ[@]//*/$(replacement optional_arguments)} |
| 99 | # ^^ ^^^^^^^^^^^^^ |
| 100 | # !!! !!! !!! !!! !!! !!! |
| 101 | |
| 102 | # Now, if Bash would only pass the matched string |
| 103 | #+ to the function being called . . . |
| 104 | |
| 105 | echo |
| 106 | |
| 107 | exit 0 |
| 108 | |
| 109 | # Before reaching for a Big Hammer -- Perl, Python, or all the rest -- |
| 110 | # recall: |
| 111 | # $( ... ) is command substitution. |
| 112 | # A function runs as a sub-process. |
| 113 | # A function writes its output (if echo-ed) to stdout. |
| 114 | # Assignment, in conjunction with "echo" and command substitution, |
| 115 | #+ can read a function's stdout. |
| 116 | # The name[@] notation specifies (the equivalent of) a "for-each" |
| 117 | #+ operation. |
| 118 | # Bash is more powerful than you think! |