Shell string manipulation
The bash shell provides several commands for string processing:
String length
- ${#..}
- Length of expr
- Length of awk (s)
Instance:
string= "Hello World"
$ {#string}
Expr Length "$string"
Note: Double-quotes are required. Because the string has spaces
Matching substrings
Format: Expr match $string $substring
Function: matches the substring at the beginning of the string, returns the matched length, and does not match at the beginning of string substring returns 0,substring can be a regular form
String= "Welcome to our World"
Command |
return value |
Expr match "$string" w.* |
20 |
Expr match "$string" ou.* |
0 |
Index of common characters
Format: Expr index $string $sunstring
Function: Matches the first occurrence of a character in a string of substring
String= "Welcome to our World"
Command |
return value |
Expr index "$string" our |
5 |
Expr index "$string" D |
20 |
Expr index "$string" s |
0 |
Perform the discovery. The function of expr index is to look for the first common character between two strings
Intercept sub-strings
- Intercept from left
- ${string:position}
- ${string:position:length}
- Intercept from right
- ${string:-position} (there is a space after the colon)
- ${string: (Position)}
- ${string:-position:length}
- ${string: (position): length}
Expr substr
Format: Expr substr $string $Position $length
The difference from ${}: ${} position starts from 0 to string, and the position of expr sutstr starts from 1 to the string label
String= "Welcome to our World"
Command |
return value |
Echo ${string:1:8} |
elcome T |
Expr substr "$string" 2 8 |
elcome T |
The regular form intercept sub-string
Using a regular table can only extract substrings at the beginning or end of a string.
-Expr Match $string ' \ ($substring\) '
-Expr $string: ' \ ($substring\) '
Command |
return value |
Expr match "$another" "[0-9]*" |
8 |
Expr match "$another" \ ([0-9]*\)] |
20091114 |
Expr "$another": "\ ([0-9]*\)" |
20091114 |
Note: There are spaces on both sides of the colon
Delete a child string
- ${string#substring}
Delete the shortest substring that matches the substring at the beginning of a string
- ${string# #substring}
Delete the oldest string that matches the substring at the beginning of the string
- ${string%substring}
Delete the shortest substring that matches the substring at the end of a string
- ${string%%substring}
- Delete the oldest string that matches the substring at the end of the string
SUBSTRING is not a regular form of expression.
20091114 Reading Hadoop
Command |
Results |
echo "${another#2*1}" |
Reading Hadoop |
echo "${another# #2 *}" |
4 Reading Hadoop |
echo "${another%a*p}" |
20091114 Reading H |
echo "${another%%a*p}" |
20091114 Re |
Replace substring
- ${string/substring/replacement}
Replace only substrings that match substring for the first time
- ${string//substring/replacement}
Replace all substrings that match substring
- ${string/#substring/replacement}
Replace substring with substring at the beginning of string
- ${string/%substring/replacement}
Replace substring with substring at the end of string
String= "20001020year20050509month"
Command |
Results |
Echo ${string/200/201} |
20101020year20050509month |
Echo ${string/200/201} |
20101020year20150509month |
Echo ${string/r*h/} |
20001020yea |
echo ${string/#2000/2010} |
20101020year20050509month |
Echo ${string/%month/month} |
20001020year20050509month |
String manipulation in the shell