String operations in shell
SHELL string operation
Bash Shell provides multiple string processing commands:
Awk command expr command string length $ {# ..} expr length awk length (s)
Instance:
String = "hello world"
$ {# String}
Expr length "$ string"
Note: Double quotation marks are required because strings contain spaces to match substrings.
Format: expr match $ string $ substring
Purpose: Match substring at the beginning of string and return the matched length. If substring does not match at the beginning of string, 0 is returned. substring can be a regular expression.
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
Purpose: match the first occurrence of a character in the substring string.
String = "welcome to our world"
Command |
Return Value |
Expr index "$ string" our |
5 |
Expr index "$ string" d |
20 |
Expr index "$ string" s |
0 |
It is found that the expr index function is to find the first common character between two strings.
Extract substring from left
$ {String: position }$ {string: position: length} is truncated from the right.
$ {String:-position} (there is a space behind the colon) $ {string :( position) }$ {string:-position: length }$ {string :( position): length}
Expr substr
Format: expr substr $ string $ position $ length
Difference from $ {}: the position of $ {} starts from 0 to mark the string; the position of expr sustr starts from 1 to mark the string.
String = "welcome to our world"
Command |
Return Value |
Echo $ {string: 1: 8} |
Elcome t |
Expr substr "$ string" 2 8 |
Elcome t |
Use a regular expression to intercept substrings.
The regular expression can only extract substrings starting or ending with 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 substring $ {string # substring}
Delete the shortest substring that matches the substring at the beginning of string $ {string # substring}
Delete the oldest string that matches substring at the beginning of string $ {string % substring}
Delete the shortest substring $ {string % substring} at the end of the string that matches the substring to delete the shortest substring at the end of the string.
Substring is not a regular expression.
20091114 Reading Hadoop
Command |
Result |
Echo "$ {another #2*1 }" |
114 Reading Hadoop |
Echo "$ {another #2*1 }" |
4 Reading Hadoop |
Echo "$ {another % a * p }" |
20091114 Reading H |
Echo "$ {another % a * p }" |
20091114 Re |
Replace substring $ {string/substring/replacement}
Only Replace the substring $ {string // substring/replacement} that matches the substring for the first time}
Replace all substrings that match the substring $ {string/# substring/replacement}
Replace the substring $ {string/% substring/replacement} that matches the substring at the beginning of string}
Replace the substring that matches the end of the string with the substring.
String = "20001020year20050509month"
Command |
Result |
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 |