Shell Script Foundation II

Source: Internet
Author: User
Tags truncated

Processing of strings
1. Interception of strings
Method one, using the ${} expression
Format: ${var: Start position: Length}

Phone= "13788768897"
    # echo ${#Phone}    11                                

When you use ${} to intercept a string, the starting position starts at 0 (similar to the array subscript number)

Echo ${phone:1:6}
 378876

Method two, using expr substr
Format: Expr substr "$var" Start position length
When using expr substr to intercept a string, the starting number starts at 1, which is to be distinguished from the ${}.

Expr substr "$Phone" 1 6
 137887

Way three, using the cut split tool

Format: Echo $var | Cut-b start position-end position

Option-B means that the characters are truncated in bytes, where the starting position and end position can be omitted. When the starting position is omitted, it is considered to start at the 1th character (numbering is also starting from 1, similar to expr), and when the end position is omitted, it is considered to be truncated to the last.

echo $Phone | Cut-b 1-6
 137887      # echo $phone | cut -b 2,4,6,8  不连续截取       7679

2. Substitution of strings
Replace only the 1th substring
Format: ${var/old/new}

School= "Tarena IT Group"
     # echo  ${SCHOOL/r/RRRR}      TaRRRRena IT Group                替换全部子串

Format: ${var//old/new}

Echo ${school//r/rrrr}
TaRRRRena IT GRRRRoup

3. Matching deletion of strings
1) from left to right, shortest match delete

Format: ${variable name #* keyword}

Delete the 1th character from the left to the nearest keyword "oo" section, * as a wildcard understanding:

echo $MAIL//Confirm the value of the variable MAIL

/var/spool/mail/root

Echo ${mail#*/}

Var/spool/mail/root

2) left-to-right, longest match delete

Format: ${variable name ##* keyword}

Delete the 1th character from the left to the furthest word "oo" part:

Echo ${mail##*oo}
  t       # echo ${MAIL##*/}     root

3) Right-to-left, shortest match delete

Format: ${variable name% keyword *}

Delete the last 1 characters from the right to the left nearest keyword "oo" section, * do wildcard understanding

Echo ${mail%oo*}
/var/spool/mail/r     # echo ${MAIL%/*}    /var/spool/mail

4) Right-to-left, longest match delete

Format: ${variable name% keywords *}

Delete the last 1 characters from the right to the left furthest from the keyword "oo" section:

Echo ${mail%%oo*}
 var/sp

4. Handling of string initial values
Value only, ${var:-word}
If the variable var already exists and is not NULL, the value of the $var is returned; otherwise the string "word", the value of the original variable var is not affected
case where the value of the variable already exists
[[email protected] ~]# echo $SCHOOL//View the value of the original variable
Tarena IT Group.
[[email protected] ~]# echo ${school:-tarena}//Because SCHOOL already exists, the value of the output variable SCHOOL
Tarena IT Group.
[[email protected] ~]# echo $SCHOOL//The value of the original variable does not change
Tarena IT Group.
case where the value of the variable does not exist:

[[email protected] ~]# unset SCHOOL//Clear SCHOOL variable
[[email protected] ~]# echo ${school:-tarena}//Because SCHOOL no longer exists, output "Tarena"
Tarena
[[email protected] ~]# echo $SCHOOL//variable SCHOOL still not present

Shell Script Foundation II

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.