Shell string manipulation methods and examples

Source: Internet
Author: User
Tags string back

1. String length:

Var=123abc456echo ${#var}9expr length $var 9

2. Specify character interception

Echo ${var:5} #截取前五个字符c456echo ${var:2:5} #截取第2到第5个字符3abc4echo ${var: 3} 3 characters after intercept (with space on the left) 456

3. Interception of content in the specified format

String interception of Linux is useful. There are eight ways.

Suppose there is a variable var=http://www.aaa.com/123.htm

1. # Intercept, delete the left character, leave the right character.

Echo ${var#*//}

where Var is the variable name and the # is an operator, *//indicates that the first//number and all characters on the left are deleted from the left.

Delete http://

The result is: www.aaa.com/123.htm

2. # # Intercept, delete the left character and leave the right character.

Echo ${var##*/}

##*/indicates that the last (rightmost) one/number and all characters to the left are deleted from the left.

That is, delete http://www.aaa.com/

The result is 123.htm.

3.% Intercept, delete right character, leave left character

Echo ${var%/*}

%/* indicates that the first/second and right characters are deleted from the right.

The result is: http://www.aaa.com

4. The percent number intercept, delete the right character, leave the left character

Echo ${var%%/*}

%%/* indicates that the last (leftmost) one/number and right character are deleted from the right.

The result is: http:

5. Start with the first few characters on the left and the number of characters

Echo ${var:0:5}

0 represents the first character on the left and 5 indicates the total number of characters.

The result is: http:

6. Start with the first few characters on the left and continue to the end.

Echo ${var:7}

7 means that the 8th character on the left begins, until the end.

The result is: www.aaa.com/123.htm

7. Start with the first few characters on the right and the number of characters

Echo ${var:0-7:3}

0-7 means that the seventh character starts at the right, and 3 indicates the number of characters.

The result is: 123

8. Start with the first few characters on the right and continue to the end.

Echo ${var:0-7}

The expression starts at the seventh character on the right and continues to the end.

Result is: 123.htm

Note: (The first character on the left is denoted by 0, and the first character on the right is denoted by 0-1)

$ echo ${var:5}//$ echo ${var:2-5}htm$ echo $varhttp://www.aaa.com/123.htm

#和 # # Indicates the deletion starts from the left. A # Indicates the deletion from the left to the first specified character; Two # indicates the deletion from the left to the last specified character.

% and percent of% are removed from the right. A% represents the deletion from the right to the first specified character; Two% indicates the deletion from the left to the last specified character.

The deletion includes the specified character itself.

Calculating the length of a character also uses length:

string= "Hello,everyone My name is Xiaoming"

Expr Length "$string"

Output: 34

Note: string strings have spaces inside, so you need to add double quotes

When using the expr command, the operators in an expression must contain spaces around them, and if they do not contain spaces, the expression itself will be output:

Expr 5+6//Direct Output 5+6

Expr 5 + 6//Output 11

For some operators, we also need to use the symbol "\" to escape, otherwise we will prompt a syntax error.

Expr 5 * 6//Output error

Expr 5 \* 6//Output 30


The use of shell is a method of string back-write

1. By Rev Order

[Email Protected]~$>echo 12345|rev54321

2. Through awk

Echo Abcd|awk ' {for (i=1;i<=length;i++) {line=substr ($0,i,1) line}} End{print line} '

①, substr ($0,i,1), which takes the current character starting from index I, taking the current bit

The length of the current string, which is 3, ②

③, Line=substr ($0,i,1) line; Keep three values in the memory stack, then print out the CBA

substr ($3,6,2)---> indicates that the 6th character in the 3rd field starts with a 2-character end.
substr ($3,6)---> representation starts from the 6th character in the 3rd field, until the end

3. Using Python

echo "123456" | Python-c ' Print (Input () [::-1]) '




This article refers to:

Http://www.runoob.com/linux/linux-shell-variable.html

Http://song3304.blog.163.com/blog/static/28262506201102882833450/

Shell string manipulation methods and examples

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.