Article Title: string processing in BASH. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
It is transferred from the shell version for my future study. The body is as follows:
Get Length
% X = "abcd"
# Method 1
% Expr length $ x
4
# Method 2
% Echo $ {# x}
4
# Method 3
% Expr "$ x ":".*"
4
# Expr help
# STRING: REGEXP anchored pattern match of REGEXP in STRING
Search for substrings
% Expr index $ x "B"
2
% Expr index $ x ""
1
% Expr index $ x "B"
2
% Expr index $ x "c"
3
% Expr index $ x "d"
4
Obtain the substring
# Method 1
# Expr Startpos length
% Expr substr "$ x" 1 3
Abc
% Expr substr "$ x" 1 5
Abcd
% Expr substr "$ x" 2 5
Bcd
# Method 2
# $ {X: pos: lenght}
% Echo $ {x: 1}
Bcd
% Echo $ {x: 2}
Cd
% Echo $ {x: 0}
Abcd
% Echo $ {x: 0: 2}
AB
% Pos = 1
% Len = 2
% Echo $ {x: $ pos: $ len}
Bc
Match Regular Expression
# Print matching length
% Expr match $ x "."
1
% Expr match $ x "abc"
3
% Expr match $ x "bc"
0
The beginning and end of a string
% X = aabbaarealwwvvww
% Echo "$ {x % w * w }"
Aabbaarealwwvv
% Echo "$ {x % w * w }"
Aabbaareal
% Echo "$ {x # a * }"
Lwwvvww
% Echo "$ {x # a * }"
Bbaarealwwvvww
Where, # indicates the header, because # on the keyboard is on the left of $.
Here, % indicates % because % on the keyboard is on the right of $.
A single parameter indicates the minimum matching, and a double parameter indicates the maximum matching.
That is to say, when there are multiple matching schemes, select the maximum length or the minimum length of the matching.
String replacement
% X = abcdabcd
% Echo $ {x/a/B} # Replace only one
Bbcdabcd
% Echo $ {x // a/B} # Replace all
Bbcdbbcd
Regexp cannot be used, *? .