Abstract:

String-related operations are often involved in shell batch programs. There are many command statements, such as awk,sed can do various string operations. In fact, the shell has a series of built-in operation symbols, which can achieve a similar effect. As you know, using internal operators will omit the time to start external programs, so the speed is very fast.

String-related operations are often involved in shell batch programs. There are many command statements, such as awk,sed can do various string operations. In fact, the shell has a series of built-in operation symbols, which can achieve a similar effect. As you know, using internal operators will omit the time to start external programs, so the speed is very fast.

Judge the value of the string read

expression meaning
${var} The value of var is the same as $var
${var-DEFAULT} If var is not declared, $DEFAULT is used as its value *
${var:-DEFAULT} If var is not declared, or its value is null, then $DEFAULT is used as its value *
${var=DEFAULT} If var is not declared, $DEFAULT is used as its value *
${var:=DEFAULT} If var is not declared, or its value is null, then $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 *
The ${! varprefix*} Matches all previously declared variables starting with varprefix
The ${! varprefix@} Matches all previously declared variables starting with varprefix

The “*” does not mean: of course, if the var variable is already set, then the value is $var.

[chengmo@localhost ~]$ echo ${abc-‘ok’}

ok

[chengmo@localhost ~]$ echo $abc

[chengmo@localhost ~]$ echo ${abc=’ok’}

ok

[chengmo@localhost ~]$ echo $abc

ok

If ABC does not declare “=”, it will assign a value to ABC.

[chengmo@localhost ~]$ var1=11; var2=12; var3=

[chengmo@localhost ~]$ echo ${! v@}

var1 var2 var3

[chengmo@localhost ~]$ echo ${! v*}

var1 var2 var3

The ${! Varprefix *} and ${! Similar to varprefix@}, you can search for defined variables, whether null or not, by using the prefix character of the variable name.

String operations (length, read, replace)

expression meaning
${#string} $string length
${string:position} In $string, the substring is extracted from the position $position
${string:position:length} In $string, a substring of length $length is extracted starting at position $position
${string#substring} Remove the shortest substring matching $substring from the beginning of the variable $string
${string##substring} From the beginning of the variable $string, remove the longest substring matching $substring
${string%substring} Remove the shortest substring matching $substring from the end of the variable $string
${string%%substring} Remove the longest string matching $substring from the end of the variable $string
${string/substring/replacement} Use $replacement to replace the first matching $substring
${string//substring/replacement} Use $replacement instead

all

$substring matching

${string/#substring/replacement} If $string

The prefix

If $substring is matched, $replacement is used to replace the matched $substring

${string/%substring/replacement} If $string

The suffix

If $substring is matched, $replacement is used to replace the matched $substring

Note: “*$substring “can be one

Regular expression

.

The length of the 1.

[web97@salewell97 ~]$ test=’I love china’

[web97@salewell97 ~]$ echo ${#test}

12

${# variable name} gets the length of the string

2. Intercept the string

[chengmo@localhost ~]$ test=’I love china’

[chengmo@localhost ~]$ echo ${test:5}

e china

[chengmo@localhost ~]$ echo ${test:5:10}

e china

${variable name: start: length} returns a substring

3. Delete the string

[chengmo@localhost ~]$ test=’c:/windows/boot.ini’

[chengmo@localhost ~]$ echo ${test#/}

c:/windows/boot.ini

[chengmo@localhost ~]$ echo ${test#*/}

windows/boot.ini

[chengmo@localhost ~]$ echo ${test##*/}

boot.ini

[chengmo@localhost ~]$ echo ${test%/*}

c:/windows

[chengmo@localhost ~]$ echo ${test%%/*}

${variable name # subString regular expression} is equipped with substring at the beginning of the string, and the expression matching is deleted.

${variable name % SUBString regular expression} is equipped with substring from the end of the string to delete matching expressions.

Note: ${test##*/} and ${test%/*} are the easiest ways to get the file name and directory address, respectively.

4. String replacement

[chengmo@localhost ~]$ test=’c:/windows/boot.ini’

[chengmo@localhost ~]$ echo ${test/\//\\}

c:\windows/boot.ini

[chengmo@localhost ~]$ echo ${test//\//\\}

c:\windows\boot.ini

${variable} / find/replace value a replace “/” said the first, replace all “/ /” said, when find there: “/” please add the escape character ‘\ / “said.

Three, performance comparison

In shell, awk,sed,expr, etc. Let’s do a performance comparison.

[chengmo@localhost ~]$ test=’c:/windows/boot.ini’

[chengmo@localhost ~]$ time for i in $(seq 10000); do a=${#test}; done;

The real 0 m0. 173 s

The user 0 m0. The 139 s

Sys 0 m0. The 004 s

[chengmo@localhost ~]$ time for i in $(seq 10000); do a=$(expr length $test); done;

The real 0 m9. 734 s

The user 0 m1. 628 s

Hundreds of times faster, calls external command processing, and the performance of built-in operators is very different. In shell programming, try to use built-in operators or functions. With AWk,sed produces similar results.

Linux shell string operation (length, search, replace) details, if you need to reprint please contact the original blogger.

Copyright Notice: The content of this article is contributed by Internet users, copyright belongs to the author, the community does not have the ownership, also do not assume the relevant legal responsibility. If you find any content suspected of plagiarism in our community, you are welcome to send an email to [email protected] to report and provide relevant evidence. Once verified, our community will immediately delete the content suspected of infringement.

The original link