Linux & UNIX
sed나 awk 대신에 parameter substitution 를 이용하자
fabxoe
2021. 7. 15. 21:16
string="TestT100String"
echo ${string//[^[:digit:]]/}
결과
Summary: String Manipulation and Expanding Variables
For your ready references here are all your handy bash parameter substitution operators. Try them all; enhance your scripting skills like a pro:
${parameter:-defaultValue} | Get default shell variables value |
${parameter:=defaultValue} | Set default shell variables value |
${parameter:?"Error Message"} | Display an error message if parameter is not set |
${#var} | Find the length of the string |
${var%pattern} | Remove from shortest rear (end) pattern |
${var%%pattern} | Remove from longest rear (end) pattern |
${var:num1:num2} | Substring |
${var#pattern} | Remove from shortest front pattern |
${var##pattern} | Remove from longest front pattern |
${var/pattern/string} | Find and replace (only replace first occurrence) |
${var//pattern/string} | Find and replace all occurrences |
${!prefix*} | Expands to the names of variables whose names begin with prefix. |
${var,} ${var,pattern} |
Convert first character to lowercase. |
${var,,} ${var,,pattern} |
Convert all characters to lowercase. |
${var^} ${var^pattern} |
Convert first character to uppercase. |
${var^^} ${var^^pattern} |
Convert all character to uppercase.. |
https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html
How To Use Bash Parameter Substitution Like A Pro
Explains how to use Bash Parameter Substitution for string matriculation and expansion - includes tons of practical examples.
www.cyberciti.biz
추가로 하나더! 이건 뭐지?
Bash, Dash, KornShell (ksh), Z shell (zsh) 에서 다음 처럼 테스트 할 수 있다.
test "${string#*$word}" != "$string" && echo "$word found in $string"
테스트 명령어?