Linux shell string operations

Source: Internet
Author: User

For details about linux shell string operations, string operations are often involved in shell batch processing. There are many command statements, such as awk and sed, which can perform various string operations. In fact, shell has a series of built-in operation symbols, which can achieve similar results. As you know, using internal operators will omit the time to start external programs, so the speed will be very fast. I. determine the meaning of the expression for reading string values. The value of the variable var $ {var} is the same as that of $ var $ {var-DEFAULT}. If var is not declared, use $ DEFAULT as its value * $ {var:-DEFAULT}. If var is not declared or its value is null, use $ DEFAULT as its value * $ {var = DEFAULT}. If var is not declared, use $ DEFAULT as its value * $ {var: = DEFAULT} If var is not declared or its value is null, $ DEFAULT is used as its value * $ {var + OTHER} If var is declared, the value is $ OTHER. Otherwise, it is a null String $ {var: + OTHER}. If var is set, the 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, Print $ ERR_MSG * $ {! Varprefix *} matches all variables declared starting with varprefix $ {! Varprefix @} matches all previously declared variables starting with varprefix with "*", which does not mean: of course, if the variable var has been set, its value is $ var. ii. string operation (length, read, replace) expression meaning $ {# string} string Length $ {string: position} in $ string, extract substring $ {string: position: length} from position $ position in $ string, starting from the position $ position, extract the substring $ {string # substring} with the length of $ length from the beginning of the variable $ string, delete the substring $ {string # substring} that matches the shortest $ substring from the beginning of the variable $ string, delete the substring $ {string % substring} with the longest match $ substring from the end of the variable $ string and delete the shortest match $ substri Starting from the end of the variable $ string, the ng substring $ {string % substring} deletes the substring $ {string/substring/replacement} That has the longest match with $ substring using $ replacement, to replace the first matched $ substring $ {string // substring/replacement} with $ replacement, replace all matched $ substring $ {string/# substring/replacement}. If the $ string prefix matches $ substring, $ replacement is used to replace the matched $ substring $ {string/% substring/replacement}. If the $ string suffix matches $ substring, $ replacement is used to replace the matched $ substring Description: "* $ substring" can be Regular Expressions. instance: Read: java code $ echo $ {abc-'OK'} OK $ echo $ abc $ echo $ {abc = 'OK'} OK $ echo $ abc OK # If abc does not declare "= ""abc is also assigned. $ Var1 = 11; var2 = 12; var3 = $ echo $ {! V @} var1 var2 var3 $ echo $ {! V *} var1 var2 var3 #$ {! Varprefix *} and $ {! Varprefix @} is similar. You can use the variable name prefix to search for Defined variables, whether or not they are null. 1, get the string length C code string = abc12342341 // do not have spaces on the two sides of the equal sign echo $ {# string} // result 11 expr length $ string // result 11 expr "$ string ": ". * "// result 11 there must be a space on the two sides of the semicolon. Here, the root match usage is similar to 2, position of the string C code expr index $ string '20160301' // Result 4 the subscript of the string is the C code str = "abc" expr index $ str ""# 1 expr index $ str "B" #2 expr index $ str "x" #0 expr index $ str "" #0 this method reminds me of js indexOf, various Languages have similar operations on strings. If you have a basic language, you can quickly learn shell. 3. the maximum length from the start of the string to the substring. C code expr match $ string 'abc. * 3 '// result 9 I personally think this function is of little use. Why should it start from the beginning. 4. string truncation C code echo $ {string: 4} // 2342341 start to capture all the strings following echo $ {string: 3: 3} // 123 capture the next three echo $ {string: 3: 6} // 3rd capture the Next Six echo $ {string: -4} // 2341: there is a space on the right side to intercept the last 4 echo $ {string :(-4 )} // 2341 same as expr substr $ string 3 3 // 123 truncate the next three digits of C code str = "abcdef" expr substr "$ str" 1 3 # From the first the location starts with three characters, abc expr substr "$ str" 2 5 # Get 5 characters from the second position, bcdef expr substr "$ str" 4 5 # Get 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 second position to the left, abcde echo $ {str :(-4 ): 3} # extract 6 characters from the last and second places to the left. The method above cde reminds me of the substr function of php, which is followed by the same rule. 5. match is also different from match in C code // example 3. The length of matching characters is shown above, the following is the matched content expr match $ string '\ ([a-c] * [0-9] * \)' // abc12342341 expr $ string: '\ ([a-c] * [0-9] \)' // abc1 expr $ string :'. * \ ([0-9] [0-9] [0-9] \) '// 341 display the Matching content in the brackets, is there any similarity between the use of other brackets? 6. Capture unmatched content C code echo $ {string # a * 3} // 42341 starts from the left side of $ string, remove the shortest matching substring echo $ {string # c * 3} // abc12342341 and nothing matches echo $ {string # * c1 * 3} // 42341 from the left side of $ string start, remove the shortest matched substring echo $ {String # a * 3} // 41 start from the left of $ string and remove the longest matching substring echo $ {string % 3*1} // abc12342 from the right of $ string, remove the shortest matched substring echo $ {string % 3*1} // abc12 from the right of $ string, remove the longest matched substring C code str = "abbc, def, ghi, abcjkl "echo $ {str # a * c} # output, def, ghi, abcjkl a well number (#) indicates to extract the shortest matching from the left side (remove the abbc string here) echo $ {str # a * c} # output jkl. The two pound signs (#) are used to extract the longest match from the left side (Here we take abbc, def, ghi, abc string removed) echo $ {str # "a * c"} # output abbc, def, ghi, abcjkl because str does not contain the "a * c" substring echo $ {str # "a * c "} # Output abbc, def, ghi, abcjkl 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 one percent sign (%) indicates the shortest matching echo $ {str % B * l} # a two percent signs (%) the longest matching echo $ {str % a * c} # abbc, def, ghi, abcjkl is intercepted from the right. Note that the string must start with the first character, you can remember this from the last one. the pound sign (#) is usually used to represent a number, which is placed in front; The percent sign (%) unmounts the digit; or, in this way, in the keyboard layout, the pound sign (#) is always on the left (that is, the Front) of the percent sign (% ). 7, match and replace C code echo $ {string/23/bb} // abc1bb42341 replace echo $ {string // 23/bb} // abc1bb4bb41 double slash replace all matched echo $ {string/# abc/bb} // bb12342341 # start with what to match, ^ in the root php is a bit like echo $ {string/% 41/bb} // abc123423bb % at what end to match. $ in the root php is a bit like C code str = "apple, tree, apple tree "echo $ {str/apple/APPLE} # Replace the first apple echo $ {str // apple/APPLE} # Replace all apple echo $ {str/# apple/ APPLE} # If the string str starts with apple, replace it with APPLE echo $ {str/% apple/APPL E} # If the string str ends with apple, use APPLE to replace it with C code $ test = 'C:/windows/boot. ini '$ echo $ {test/\ // \} c: \ windows/boot. ini $ echo $ {test // \// \\} c: \ windows \ boot. ini # $ {Variable/search/replace value} A "/" indicates replacing the first variable, "//" indicates replacing all. When the search appears: "/" Please add an escape character. 8. Compare C code [["a.txt" = a *] # pattern matching [["a.txt" = ~. *\. Txt] # logical truth (regex matching) [["abc" = "abc"] # logical truth (string comparision) [["11" <"2"] # logical truth (string comparision), compared by ascii value 9. connect C code s1 = "hello" s2 = "world" echo ${s1 }$ {s2} # Of course, write $ s1 $ s2 in this way, but it is best to add braces 10. java code $ 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 % /*} # $ {Variable name # substring Regular Expression} starts with a string and starts with a substring to delete matching expressions. # $ {Variable name % substring Regular Expression} starts from the end of the string with substring to delete the matching expression. # Note: $ {test # */} and $ {test %/*} are the simplest methods to get the file name or directory address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.