Shell string processing

Source: Internet
Author: User
Shell string interception; shell string interception problem: 1. Linuxshell intercepts the first eight digits of the character variable, the method is as follows: 1. exprsubstr $ a182.echo $ a | awk & amp; lsquo; {printsubstr (, 1, 8)} & amp; rsquo; 3. echo $ a | cut-c1-84...

Shell string processing

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.

$ Myvar1_foodforthought.jpg
$ Echo $ {MYVAR # * fo}
$ Echo $ {MYVAR # * fo}

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}
$ Echo $ {EXCLAIM: 3: 7}

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


This article is from LinuxLeon"

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: 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.