Shell string Operations (length, find, replace) detailed _linux shell

Source: Internet
Author: User
Copy Code code as follows:

Examples of string operations in work
Filename= '/home/admin/jobs/cnclickstat/dfsloader/loader.cfg '
#下面是使用shell字符串操作
buname1=${filename#*/jobs/} #去除 '/home/admin/jobs/cnclickstat/dfsloader/loader.cfg ' prefix get ' CnClickstat/DFSLoader/ Loader.cfg '
buname1=${buname1%%/*} #去除 ' cnclickstat/dfsloader/loader.cfg ' suffix gets ' cnclickstat '
echo $buName 1

#下面用awk获取需要的字符串内容
Buname2= ' echo $filename | Awk-f/' {printf ('%s ', $)} ';
Echo $buName 2

#下面使用cut获取需要的字符串内容
Buname3= ' echo $filename | cut-d/F 5 ';
Echo $buName 3

All above can obtain the result: Cnclickstat
String Operation example Ends


String-related operations are often involved in the shell batch process. There are a lot of command statements, such as: awk,sed can do various operations of string. In fact, the Shell has a series of operating symbols, you can achieve similar effects, you know, using the internal operator will omit the start of the external program and so on time, so the speed will be very fast.

First, judge read string value

An expression

Meaning

${var}

The value of the variable var , same as the $var

${var-default}

If var is not declared , then $DEFAULT as its value *

${var:-default}

If var is not declared , or if its value is null , then the value is $DEFAULT

${var=default}

If var is not declared , then $DEFAULT as its value *

${var:=default}

If var is not declared , or if its value is null , then the value is $DEFAULT

${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 , then print $ERR _msg *

${var:? ERR_MSG}

If var is not set , then print $ERR _msg *

${!varprefix*}

Match all previously declared variables preceded by Varprefix

${!varprefix@}

Match all previously declared variables preceded by Varprefix


Adding "*" does not mean: Of course, if Var has been set, 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 also assigns 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

${!varprefix*} is similar to ${!varprefix@}, you can search for a variable with a variable name prefix character, regardless of whether it is a null value or not.

Second, string operation (length, read, replace)

An expression

Meaning

${#string}

length of $string

${string:position}

In $string , the substring is extracted from the position $position

${string:position:length}

In $string , a substring of $length length is extracted from the position $position

${string#substring}

Deletes the substring of the shortest matching $substring from the beginning of the variable $string

${string# #substring}

Deletes the substring of the longest matching $substring from the beginning of the variable $string

${string%substring}

Deletes the substring of the shortest matching $substring from the end of the variable $string

${string%%substring}

Deletes the substring of the longest 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 of all matching $substring

${string/#substring/replacement}

If $string prefix matches $substring, use $replacement instead of matching $substring

${string/%substring/replacement}

If the $string suffix matches $substring, the $replacement is used instead of the matching $substring


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

1. Length

[Web97@salewell97 ~]$ test= ' I love '
[Web97@salewell97 ~]$ echo ${#test}
12
${#变量名} Get string length

2. Intercept string

[Chengmo@localhost ~]$ test= ' I love '
[Chengmo@localhost ~]$ Echo ${test:5}
E
[Chengmo@localhost ~]$ Echo ${test:5:10}
E
${variable Name: start: length} get substring

3. String deletion

[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} from the beginning of the string with substring, delete the expression on the match.
${variable name%substring regular expression} starts with substring at the end of the string, removing the expression on the match.
Note: ${test##*/},${test%/*} is the simplest way to get a filename or directory address.

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 "/" means replace the first one, "//" to replace all, when the lookup appears: "/" Add the escape character "\/" expression.

Third, performance comparison

In the shell, through awk,sed,expr and so on can be implemented, string above operations. Here's a performance comparison.
[Chengmo@localhost ~] $test = ' C:/windows/boot.ini '
[Chengmo@localhost ~]$ time to I in $ (seq 10000);d oa=${#test};d one;
Real 0m0.173s
User 0m0.139s
SYS 0m0.004s
[Chengmo@localhost ~]$ time to I in $ (seq 10000);d o a=$ (expr length$test);d one;
Real 0m9.734s
User 0m1.628s

The speed difference is hundredfold, calls the external command processing, and the built-in operator performance difference is very big. In shell programming, try to do it with built-in operators or functions. Similar results can occur with awk,sed.
Yorking Alan
Related Article

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.