linux shell string operation details

String related operations are often involved in shell batch programs. There are many command statements, such as awk and sed, which can perform various string operations. In fact, a series of operation symbols are built into the shell, which can achieve similar effects. As we all know, the use of internal operation symbols will omit the time to start external programs, so the speed will be very fast.

1, Judge read string value

expressionmeaning
${var}The value of the variable VaR is the same as $var
${var-DEFAULT}If var is not declared, take $DEFAULT as its value*
${var:-DEFAULT}If var is not declared, or if its value is empty, $DEFAULT is taken as its value*
${var=DEFAULT}If var is not declared, take $DEFAULT as its value*
${var:=DEFAULT}If var is not declared, or if its value is empty, $DEFAULT is taken as its value*
${var+OTHER}If var is declared, its value is $OTHER, otherwise it is a null string
${var:+OTHER}If var is set, its value is $OTHER, otherwise it is a null string
${var?ERR_MSG}If var is not declared, print $ERR_MSG *
${var:?ERR_MSG}If var is not set, $err is printed_ MSG *
${!varprefix*}Matches all variables previously declared starting with varprefix
${!varprefix@}Matches all variables previously declared starting with varprefix

Adding "*" does not mean: of course, if the variable VAR has been set, its value is $var

2, String operation (length, read, replace)

expressionmeaning
${#string}Length of $string
${string:position}stay s t r i n g in , from position Set string, from position string, extract the substring from position
${string:position:length}stay s t r i n g in , from position Set string, from position string, extract the substring with the length of $length from the position
${string#substring}Slave variable s t r i n g of open head , Delete except most short Horse match At the beginning of string, delete the shortest match At the beginning of string, delete the substring with the shortest matching substring
${string##substring}Slave variable s t r i n g of open head , Delete except most long Horse match At the beginning of string, delete the longest match At the beginning of string, delete the longest substring matching substring
${string%substring}Slave variable s t r i n g of junction tail , Delete except most short Horse match Delete the shortest match at the end of string At the end of string, delete the substring with the shortest matching substring
${string%%substring}Slave variable s t r i n g of junction tail , Delete except most long Horse match Delete the longest match at the end of string At the end of string, delete the longest substring matching substring
${string/substring/replacement}use r e p l a c e m e n t , come generation for The first one individual Horse match of replacement to replace the first matching replacement to replace the first matching substring
${string//substring/replacement}use r e p l a c e m e n t , generation for place have Horse match of replacement to replace all matching replacement to replace all matching substring s
${string/#substring/replacement}If s t r i n g of front Affix Horse match string prefix match If the prefix of string matches substring, use r e p l a c e m e n t come generation for Horse match reach of Replace to replace the matched Replace to replace the matching substring
${string/%substring/replacement}If s t r i n g of after Affix Horse match Suffix matching of string If the suffix of string matches substring, use r e p l a c e m e n t come generation for Horse match reach of Replace to replace the matched Replace to replace the matching substring

Description: "* $substring" can be a regular expression

Three examples:

0. Read:

$ echo ${abc-'ok'}  
ok  
$ echo $abc  
$ echo ${abc='ok'}  
ok  
$ echo $abc  
ok  
  
#If abc does not declare "=", abc will also be assigned a value.  
$ var1=11;var2=12;var3=  
$ echo ${!v@}             
var1 var2 var3  
$ echo ${!v*}  
var1 var2 var3  
  
#${! varprefix *} is similar to ${! varprefix @}. You can search for defined variables through the prefix character of variable name, whether they are null or not.  

1. Get string length

string=abc12342341          //There should be no spaces on both sides of the equal sign  
echo ${#string} / / result 11  
expr length $string         //Result 11  
expr "$string" : ".*"       //As a result, there should be a space on both sides of the 11 semicolon. The usage of root match here is similar  

2. String location

expr index $string '123'    //The subscript corresponding to the result 4 string starts from 1   

str="abc"  
expr index $str "a"  # 1  
expr index $str "b"  # 2  
expr index $str "x"  # 0  
expr index $str ""   # 0   

This method reminds me of js indexOf. The general direction of string operation methods in various languages is the same. If there is a language foundation, learning shell will be fast.

3. Maximum length from the beginning of the string to the substring

expr match $string 'abc.*3' //Result 9    

Personally, I don't think this function is very useful. Why start from the beginning.

4. String interception

echo ${string:4}      //2342341 intercept all subsequent strings from bit 4    
echo ${string:3:3}    //123 intercept the next three bits from the third bit    
echo ${string:3:6}    //123423 intercept the next 6 bits from bit 3    
echo ${string: -4}    //2341: there is a space on the right, and the last 4 digits are intercepted    
echo ${string:(-4)}   //2341 ibid    
expr substr $string 3 3   //123 intercept the next three bits from the third bit    

str="abcdef"  
expr substr "$str" 1 3  # Take 3 characters from the first position, abc  
expr substr "$str" 2 5  # Take 5 characters from the second position, bcdef   
expr substr "$str" 4 5  # Take 5 characters from the fourth position, def  
  
echo ${str:2}           # Extract the string from the second position, bcdef  
echo ${str:2:3}         # Extract 3 characters from the second position, bcd  
echo ${str:(-6):5}        # Extract the string from the penultimate position to the left, abcde  
echo ${str:(-4):3}      # Extract 6 characters to the left from the penultimate position, cde  

The above method reminds me that the rules for intercepting the substr function of php are the same.

5. Match display content

//There is also a match in example 3, which is different from the match here. The length of the matching character is displayed above, and the matching content is displayed below    
expr match $string '[a−c]∗[0−9]∗[a−c]∗[0−9]∗'  //abc12342341    
expr $string : '[a−c]∗[0−9][a−c]∗[0−9]'       //abc1    
expr $string : '.*[0−9][0−9][0−9][0−9][0−9][0−9]' //341 show matches in parentheses    

Is the use of parentheses similar to that of other parentheses,

6. Intercept mismatched content

echo ${string#A * 3} / / 42341 start from the left of $string and remove the shortest matching substring    
echo ${string#C * 3} / / abc12342341 so nothing matches    
echo ${string#*C1 * 3} / / 42341 start from the left of $string and remove the shortest matching substring    
echo ${string##A * 3} / / 41 start from the left of $string and remove the longest matching substring    
echo ${string%3*1}     //abc12342 starts from the right of $string and removes the shortest matching substring    
echo ${string%%3*1}    //abc12 starts from the right of $string and removes the longest matching substring    
str="abbc,def,ghi,abcjkl"  
echo ${str#a*c}     # output,def,ghi,abcjkl  A well number(#)Indicates that the shortest match is truncated from the left (the abbc string is removed here)  
echo ${str##a*c}    # output jkl,             Two well numbers(##)Indicates that the longest match is truncated from the left (here the abbc,def,ghi,abc strings are removed)  
echo ${str#"a*c"}   # Output abbc,def,ghi,abcjkl because there is no "a*c" substring in str  
echo ${str##"a*c"}  # Output abbc,def,ghi,abcjkl the same principle  
echo ${str#*a*c*}   # empty  
echo ${str##*a*c*}  # empty  
echo ${str#d*f)     # Output abbc,def,ghi,abcjkl,   
echo ${str#*d*f}    # Output, ghi,abcjkl     
  
echo ${str%a*l}     # abbc,def,ghi a percentage sign (%) indicates the shortest match truncated from the right   
echo ${str%%b*l}    # a two percent signs (%%) indicate the longest match truncated from the right  
echo ${str%a*c}     # abbc,def,ghi,abcjkl    

It should be noted here that you must start from the first character or the last character of the string. You can remember that the pound sign (#) is usually used to represent a number, which is placed in the front; the percent sign (%) is unloaded after the number; or you can remember that in the keyboard layout, the pound sign (#) is always on the left (i.e. in front) of the percent sign (%).

7. Match and replace

echo ${string/23/bb}   //abc1bb42341 replace once    
echo ${string//23 / BB} / / abc1bb4bb41 double slash replaces all matches    
echo ${string/#abc/bb} //bb12342341 # starts with what to match. The ^ in root php is a bit like    
echo ${string/%41/bb}  //abc123423bb% matches with what end. The $in root php is a bit like   

str="apple, tree, apple tree"  
echo ${str/apple/APPLE}   # Replace the first apple  
echo ${str//apple/APPLE}# replaces all apples  
  
echo ${str/#apple/APPLE}  # If the string str starts with apple, replace it with apple  
echo ${str/%apple/APPLE}  # If the string str ends with apple, replace it with apple  

$ test='c:/windows/boot.ini'  
$ echo ${test/\//\\}  
c:\windows/boot.ini  
$ echo ${test//\//\\}  
c:\windows\boot.ini  

#${variable / find / replace value} a "/" means to replace the first one and "/ /" means to replace all. When a: "/" appears in the search, please add an escape character "/".

8. Comparison

[[ "a.txt" == a* ]]        # Pattern matching  
[[ "a.txt" =~ .*\.txt ]]   # Regex matching  
[[ "abc" == "abc" ]]       # Logical truth (string comparison)   
[[ "11" < "2" ]]           # Logical truth (string comparison) is compared according to ascii value  

9. Connection

s1="hello"  
s2="world"  
echo ${s1}${s2}   # Of course, it's OK to write $s1$s2 like this, but it's best to add braces  

10. String deletion

$ test='c:/windows/boot.ini'  
$ echo ${test#/}  
c:/windows/boot.ini  
$ echo ${test#*/}  
windows/boot.ini  
$ echo ${test##*/}  
boot.ini  
$ echo ${test%/*}
c:/windows
$ echo ${test%%/*}  
 c:
#${Variable name #substring regular expression} is equipped with substring from the beginning of the string, and the expression on the matching is deleted. 
#${Variable name% substring regular expression} is equipped with substring from the end of the string. Delete the expression on the matching. 
#Note: ${test##*/},${test%/*} They are the easiest way to get the file name and directory address.

IV. original text link

  1. linux shell string operation details (length, read, replace, intercept, connect, compare, delete, position)

Keywords: Linux shell

Added by webhead on Thu, 23 Dec 2021 17:39:22 +0200