Summary of 7 shell string manipulation methods and instances sharing _linux shell

Source: Internet
Author: User

Each language has his own string manipulation method, the shell is the same, the following in an example of the way, a simple introduction of common methods.

1, get string length

Copy Code code as follows:

string=abc12342341//equals two sides without spaces
echo ${#string}//Results 11
Expr length $string//Result 11
Expr "$string": ". *"//Result 11 semicolon two sides to have a space, here: the use of the root match is almost

2, where the string is located

Copy Code code as follows:

The subscript for expr index $string ' 123 '//Result 4 string is starting from 0

This method makes me think of JS indexof, all kinds of language for the operation of the string of the general direction are similar, if there is a language basis, learning shell will soon.

3, the maximum length from the beginning of the string to the substring

Copy Code code as follows:

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

Personally think this function is not very useful, why should start from the beginning.

4, String interception

Copy Code code as follows:

Echo ${string:4}//2342341 intercept all strings after starting at the 4th bit
Echo ${string:3:3}//123 intercept from the 3rd bit, 3 digits behind.
Echo ${string:3:6}//123423 intercept from the 3rd bit, 6 digits behind.
echo ${string:-4}//2341:4 digits on right with space interception
echo ${string: (-4)}//2341 ditto
Expr substr $string 3 3//123 intercept the back 3 from the 3rd bit

The above method reminds me of the PHP substr function, which is the same as the rules that are intercepted later.

5, match the display content

Copy Code code as follows:

In Example 3 there is a match and match here, which shows the length of the matching character, and the following is the matching 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 matching contents in parentheses

Is there any similarity between the use of parentheses in the root bracket?

6, intercept the mismatched content

Copy Code code as follows:

Echo ${string#a*3}//42341 starting from the left of $string, removing the shortest matching substring
Echo ${string#c*3}//abc12342341 so nothing matches to
Echo ${string#*c1*3}//42341 starting from the left of $string, removing the shortest matching substring
echo ${string# #a *3}//41 starts at the left of $string and removes the longest matching substring
Echo ${string%3*1}//abc12342 from the right of the $string to remove the shortest matching substring
Echo ${string%%3*1}//abc12 from the right of the $string to remove the longest matching substring

Notice here that you must start with the first character of the string, or start at the last,

7, matching and replacing

Copy Code code as follows:

Echo ${string/23/bb}//abc1bb42341 Replace once
Echo ${string//23/bb}//abc1bb4bb41 double slash replaces all matches
echo ${string/#abc/bb}//bb12342341 #以什么开头来匹配, root PHP is a bit like
Echo ${string/%41/bb}//abc123423bb% to match at what end, the $ in root PHP is a bit like

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.