User Tools

Site Tools


bash:how_to:string_substitutions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bash:how_to:string_substitutions [2018/08/03 15:32]
Rob Stroess
bash:how_to:string_substitutions [2018/08/03 18:52] (current)
Rob Stroess
Line 2: Line 2:
 ^  string parameter substitution  ^^^^^ ^  string parameter substitution  ^^^^^
 ^operation^what^code^example using:\\ export TST='abcabcaa'^result from\\ (abcabcaa)^ ^operation^what^code^example using:\\ export TST='abcabcaa'^result from\\ (abcabcaa)^
-|remove|first occurence of substring '__xyz__' in string|STR=${STR/'__xyz__'}|echo ${TST/'bc'}|aabcaa\\  (a<del>bc</del>abcaa)|+|remove|first occurrence of substring '__xyz__' in string|STR=${STR/'__xyz__'}|echo ${TST/'bc'}|aabcaa\\  (a<del>bc</del>abcaa)|
 |:::|substring '__xyz__' everywhere in string|STR=${STR<nowiki>//</nowiki>'__xyz__'}|echo ${TST<nowiki>//</nowiki>'bc'}|aaaa\\  (a<del>bc</del>a<del>bc</del>aa)| |:::|substring '__xyz__' everywhere in string|STR=${STR<nowiki>//</nowiki>'__xyz__'}|echo ${TST<nowiki>//</nowiki>'bc'}|aaaa\\  (a<del>bc</del>a<del>bc</del>aa)|
 |:::|substring '__xyz__' at very beginning of string (prefix)|STR=${STR#'__xyz__'}|echo ${TST#'abca'}|bcaa\\  (<del>abca</del>bcaa)| |:::|substring '__xyz__' at very beginning of string (prefix)|STR=${STR#'__xyz__'}|echo ${TST#'abca'}|bcaa\\  (<del>abca</del>bcaa)|
-|:::|substring '__xyz__' at very end of string (suffix)|STR=${STR%__xyz__}|echo ${TST%'bcaa'}|abca\\  (abca<del>bc</del>aa)|+|:::|substring '__xyz__' at very end of string (suffix)|STR=${STR%__xyz__}|echo ${TST%'bcaa'}|abca\\  (abca<del>bcaa</del>)|
 |:::|first __n__ characters|STR=${STR:__n__}|echo ${TST:2}|cabcaa\\  (<del>ab</del>cabcaa)| |:::|first __n__ characters|STR=${STR:__n__}|echo ${TST:2}|cabcaa\\  (<del>ab</del>cabcaa)|
 |:::|last __n__ characters|STR=${STR:-__n__}|echo ${TST::-3}|abcab\\  (abcab<del>caa</del>)| |:::|last __n__ characters|STR=${STR:-__n__}|echo ${TST::-3}|abcab\\  (abcab<del>caa</del>)|
Line 13: Line 13:
 |convert|to upper case|STR=${STR<nowiki>^^</nowiki>}|echo ${TST<nowiki>^^</nowiki>}|ABCABCAA| |convert|to upper case|STR=${STR<nowiki>^^</nowiki>}|echo ${TST<nowiki>^^</nowiki>}|ABCABCAA|
 |:::|to lower case|STR=${STR,,}|echo ${TST,,}|abcabcaa| |:::|to lower case|STR=${STR,,}|echo ${TST,,}|abcabcaa|
-|cut|from begin of string to first end of substring '__xyz__'|STR=${STR#*'__xyz__'}|echo ${TST#*'bc'}|abcaa\\  (abc<del>abcaa</del> FAULTY)|+|cut|from begin of string to first end of substring '__xyz__'|STR=${STR#*'__xyz__'}|echo ${TST#*'bc'}|abcaa\\  (<del>abc</del>abcaa)|
 |:::|from begin of string to last end of substring '__xyz__'|STR=${STR##*'__xyz__'}|echo ${TST##*'bc'}|aa\\  (<del>abcabc</del>aa)| |:::|from begin of string to last end of substring '__xyz__'|STR=${STR##*'__xyz__'}|echo ${TST##*'bc'}|aa\\  (<del>abcabc</del>aa)|
-|:::|from first end of substring '__xyz__' to end of string|STR=${STR%"__xyz__'*}|echo ${TST%'bc'*}|abca\\  (a<del>bc</del>abcaa)| +|:::|from last occurrence of substring '__xyz__' to end of string|STR=${STR%'__xyz__'*}|echo ${TST%'bc'*}|abca\\  (abca<del>bcaa</del>)| 
-|:::|from first end of substring '__xyz__' to end of string|<nowiki>STR=${STR%%</nowiki>'__xyz__'*}|<nowiki>echo ${TST%%</nowiki>'c'*}|ab\\  (<del>abc</del>abcaa)|+|:::|from first occurrence of substring '__xyz__' to end of string|<nowiki>STR=${STR%%</nowiki>'__xyz__'*}|<nowiki>echo ${TST%%</nowiki>'c'*}|ab\\  (ab<del>cabcaa</del>)|
  
 //work in progress// //work in progress//
advertising
bash/how_to/string_substitutions.txt · Last modified: 2018/08/03 18:52 by Rob Stroess

Impressum Datenschutz