shell programming often encounters strings, file content-case conversions, and choosing the right commands in different scenarios can improve programming efficiency.
Applicable Scenarios
file contents or strings that need to be converted to uppercase and lowercase
string Case Substitution
lowercase Replace Uppercase
Echo " Hello World " TR ' [A-z] ' ' [A-z] ' Echo " Hello World " TR ' [: Lower:] ' ' [: Upper:] '
Uppercase Replace lowercase
Echo " Hello World " TR ' [A-z] ' ' [A-z] ' Echo " HELLO World " TR ' [: Upper:] ' ' [: Lower:] '
Uppercase and lowercase swaps
Echo " Hello World " TR ' [A-za-z] ' ' [A-za-z] '
file content case substitution < in situ replacement >
lowercase Replace Uppercase
sed ' s/[a-z]/\u&/g ' filename
Uppercase Replace lowercase
sed ' s/[a-z]/\u&/g ' filename
Replace the first letter of the word with uppercase
sed ' s/\b[a-z]/\u&/g ' filename
file content case substitution < required redirection >
TR 'A- z' 'A- z'< filename1 >filename2 #小写替换成大写TR 'A- z' 'A- z'< filename1 >filename2 #大写替换成小写awk '{print toupper ($)}'filename1 >filename2 #小写替换成大写awk '{print ToLower ($)}'filename1 > Filename2 #大写替换成小写
[Shell programming] File content case substitution