Summary of Shell,awk,sed interception string method in Linux

Source: Internet
Author: User

Shell

The first type:

${parameter%word} minimal truncation of word from behind
${parameter%%word} to maximize the truncation of word from the back
${parameter#word} minimal truncation of word from the front
${parameter# #word} to maximize the truncation of word from the front

Word can be a specific string, or it can be a pattern string.

Example:

Str= ' http://www. your domain name. com/cut-string.html '

Echo ${str%/*}
Results: http://www. your domain name. com

Echo ${str%%/*}
Result: http:

Echo ${str#*//}
Result: www. your domain name. com/cut-string.html

Echo ${str##*/}
Results: cut-string.html

The second type:

${VARIABLE:N1:N2}: Intercept variable variable the N2 character starting at index N1 from the left. N1 represents the index, which starts at 0, and N2 indicates the number of characters intercepted.

Variants are as follows:

${VARIABLE:N1}: Intercept variable variable all characters starting at index N1 from the left.

${VARIABLE:0-N1:N2}: Intercept variable variable the N2 character starting with the N1 character from the right.

${VARIABLE:0-N1:N2}: Intercept variable variable all characters starting with the N1 character from the right.

Example:

Variable= ' http://www. your domain name. com/cut-string.html '

Echo ${variable:0:4}
Result: http

Echo ${variable:7}
Result: www. your domain name. com/cut-string.html

Echo ${variable:0-15:10}
Results: cut-string

Echo ${variable:0-15}
Results: cut-string.html

The third type:

With other shell commands, such as cut

There are several main options for the Cut command:

echo $variable | Cut-c1-4
Result: http

echo $variable | cut-c8-
Result: www. your domain name. com/cut-string.html

echo $variable | Cut-d ":"-f1
Result: http

Summary of Shell,awk,sed interception string method in Linux

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.