Text slicing and parameter manipulation

Source: Internet
Author: User

This strategy examines some simple text substitution techniques and the parameter extensions available in bash, and these simple techniques may be useful in some ways.

Below, I will explain this part in the form of an instance.

Replace part of the text in a variable:

The commands you use are as follows:

Var= "This is a line of text"
Echo ${var/line/replaced}

The contents of the output are as follows:

This is a replaced of text

We can see line being replaced by replaced.

We can generate substrings by specifying the starting position and length of the string, as follows:

This is a replaced of text

We can see line being replaced by replaced.

We can generate substrings by specifying the starting position and length of the string, as follows:

string= "ABCDEFGHIJKLMNOPQRSTUVXXYZ"
Echo ${string:4}

The output results are as follows:

Efghijklmnopqrstuvxxyz

Print the eighth character starting with the fifth character:

Echo ${string:4:8}

The output results are:

Efghijkl

The index of the starting character is counted from 0, and we can count the last character index to 1. However, if you use a negative number as an index, you must enclose the negative number in parentheses, for example (-1) is the index of the last character.

echo ${string: (-1)}

The output results are:

Z

echo ${string: (-2): 2}

The output results are:

Yz

Here's a screenshot of the demo:

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.