Linux Shell Programming---Text slicing technology in the shell

Source: Internet
Author: User

Shell The technique of text slicing in

In the shell there is also a string slice this thing exists, has been used in Python slices, the following record the use of slices in the shell.


(1) , replacing some of the text in the variable contents

$var = "This was a line of text"

$ echo${var/line/replaced}

This is areplaced of text "

Line be replaced by replaced .



(2) , specifying the starting position and length of the string to generate the substring

The syntax is as follows:

${variable_name:start_position:length}


Print Section 5 after a character:

$string =abcdefghijklmnopqrstuvwxyz

$ echo${string:4}

Efghijklmnopqrstuvwxyz



from the first 5 characters start, print 8 characters:

$ echo${string:4:8}

Efghijkl


the index of the starting character is from 0 start counting. We can also count from the back forward and write the last character index as -1. However, if you use negative numbers as index values, you must enclose the negative numbers in parentheses. ( -1) is the index of the last character.

Echo${string: (-1)}

Z

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

Yz





This article is from the "operation and maintenance of micro-letter" blog, please be sure to keep this source http://weixiaoxin.blog.51cto.com/13270051/1963717

Linux Shell Programming---Text slicing technology in the shell

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.