Shell string Truncation

Source: Internet
Author: User

Shell string truncation problem:
1. Linux Shell intercepts the first eight characters of the character variable by using the following methods:
1. expr substr "$ A" 1 8
2. Echo $ A | awk '{print substr (, 1, 8 )}'
3. Echo $ A | cut-c1-8
4. Echo $
5. expr $ :'\(.\\).*'
6. Echo $ A | dd BS = 1 COUNT = 8 2>/dev/null

2. truncate by specified string
1. Method 1:
$ {Varible # * string} captures the string after the last string from left to right.
$ {Varible # * string} captures the string after the first string from left to right.
$ {Varible % string *} captures the string after the last string from the right to the left
$ {Varible % string *} captures the string after the first string from the right to the left.
"*" Is only a wildcard.

Example:
$ Myvar1_foodforthought.jpg
$ Echo $ {myvar # * fo}
Rthought.jpg
$ Echo $ {myvar # * fo}
Odforthought.jpg

2. Method 2: $ {Varible: N1: N2}: intercept the string from N1 to N2.

You can use another form of variable extension to select a special character string based on the specific character offset and length. Enter the following lines in Bash:
$ Exclaim = Cowabunga
$ Echo $ {exclaim: 0: 3}
Cow
$ Echo $ {exclaim: 3: 7}
Abunga

String Truncation in this form is very simple. You only need to use a colon to separate the start character and the length of the substring.

3. Split according to specified requirements:
For example, get the suffix
Ls-Al | cut-d "."-F2

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.