This shows you the differences between two versions of the page.
bash:how_to:string_substitutions [2018/08/03 06:24] Rob Stroess |
bash:how_to:string_substitutions [2024/02/16 01:04] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
===== bash: "How To" ... utilize string substitutions ===== | ===== bash: "How To" ... utilize string substitutions ===== | ||
^ 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| | + | |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%%//%%'__xyz__'}|echo ${TST%%//%%'bc'}|aaaa| | + | |:::|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| | + | |:::|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| | + | |:::|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| | + | |:::|first __n__ characters|STR=${STR:__n__}|echo ${TST:2}|cabcaa\\ (<del>ab</del>cabcaa)| |
- | |:::|last __n__ characters|STR=${STR:-__n__}|echo ${TST::-3}|abcab| | + | |:::|last __n__ characters|STR=${STR:-__n__}|echo ${TST::-3}|abcab\\ (abcab<del>caa</del>)| |
- | |extract|all characters after (!) position __n__\\ //(first char position: __n__=0)//|STR=${STR:__n__}|echo ${TST:1}|bcabcaa| | + | |extract|all characters after (!) position __n__\\ //(first char position: __n__=0)//|STR=${STR:__n__}|echo ${TST:1}|bcabcaa\\ (<del>a</del>bcabcaa)| |
- | |:::|__m__ characters after (!) position __n__|STR=${STR:__n__:__m__}|echo ${TST:1:5}|bcabc| | + | |:::|__m__ characters after (!) position __n__|STR=${STR:__n__:__m__}|echo ${TST:1:5}|bcabc\\ (<del>a</del>bcabc<del>aa</del>)| |
- | |:::|characters after (!) position __n__ until end of string minus __m__|STR=${STR:__n__:-__m__}|echo ${TST:1:-1}|bcabca| | + | |:::|characters after (!) position __n__ until end of string minus __m__|STR=${STR:__n__:-__m__}|echo ${TST:1:-1}|bcabca\\ (<del>a</del>bcabca<del>a</del>)| |
+ | |convert|to upper case|STR=${STR<nowiki>^^</nowiki>}|echo ${TST<nowiki>^^</nowiki>}|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\\ (<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 last occurrence of substring '__xyz__' to end of string|STR=${STR%'__xyz__'*}|echo ${TST%'bc'*}|abca\\ (abca<del>bcaa</del>)| | ||
+ | |:::|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// | ||
- |