Shell string Processing

Source: Internet
Author: User
Tags lowercase

One, the string slice:

${#var}: Returns the length of the string variable var
${var:offset}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, to the last part, where the value of offset is between 0 and ${#var}-1 (after bash4.2, negative values are allowed)
${var:offset:number}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, the part of the length number
${var:-length}: Takes the rightmost few characters of a string, note that there must be a white space character after the colon
${var:offset:-length}: Skips the offset character from the leftmost side, always to the right before the Lengh character from the rightmost
${var:-length:-offset}: First from the right to the left to the length of the beginning, and then to the right to the right side of the content between the offset characters, note:-length Before the space, the same expression, offset performance behavior and the array is reversed.

Example of a string slice:

  1. A=' abcdefghijklmn '
  2. echo ${#a} #输出字符串a的总长度
  3. 14
  4. echo ${A:4} #输出字符串a从0开始数, containing all elements after the 4th element
  5. Efghijklmn
  6. echo ${A:4:3} #输出字符串a从0开始数, containing 3 elements after the 4th element
  7. Efg
  8. echo ${A:-4} #从字符串a的右侧向左取4个元素, must have a null character after the colon
  9. Klmn
  10. echo ${A:5:-2} #输出字符串a从0开始数, contains the 5th element of the right number of I to the bottom 3 elements, note that this usage depends on the bash version, try not to use.
  11. Fghijkl
  12. echo ${A:-5:-2} #从字符串a的右侧向左数5个, then to the right to the last 3 characters, note that this usage depends on the bash version, try not to use it.
  13. Jkl
  14. echo ${A:-5:2} #从字符串a的右侧向左数5个, and then 2 characters to the right
  15. Jk
Second, based on the pattern to take the substring

${var#*word}: Where word can be specified by any character function: From left to right, find the var variable stored in the string, the first occurrence of Word, delete all characters from the beginning of the string to the first occurrence of Word characters
${var##*word}: Ditto, greedy mode, different, delete is all the content between the beginning of the string and the last character specified by word
${var%word*}: Where word can be any character specified; function: From right to left, find the var variable stored in the string, the first occurrence of word, delete the last character of the string to the left to the first occurrence of all characters between word characters;
${var%%word*}: Ditto, except delete all characters from the rightmost character of the string to the left to the last occurrence of word characters;

Character Processing instances:

    1. a= Abcdefg_a_ Fghijklmn '
    2. Echo ${ a#*FG} # From the left, delete the start position to the first FG, including FG
    3. _a_fghijklmn
    4. Echo ${ a##*FG} #贪婪模式, left to delete, remove start position to last FG, including FG
    5. hijklmn
    6. Echo ${ a%fg*} #从右边开始删, delete the end position to the first FG, including FG
    7. abcdefg_a_
    8. Echo $< Span class= "Br0" >{ a%%fg*} #贪婪模式, delete from the right, remove the focus position to the last FG, including FG
    9. abcde
Third, find and replace the string

${VAR/PATTERN/SUBSTR}: Finds the string that is represented by Var, the first time it is matched to by pattern, and replaces it with SUBSTR
${VAR//PATTERN/SUBSTR}: Find the string represented by Var, all strings that can be matched to by pattern, and replace it with SUBSTR
${var/#pattern/substr}: Finds the string that is represented by Var in the string that the beginning of the line is matched to by pattern, in order to substr replace the
${VAR/%PATTERN/SUBSTR}: Finds the string in the string represented by Var, where the end of the line is matched by pattern, to substr replace the

To find a replacement instance:

  1. A=' abcdefg_a_fghijklmn '
  2. echo ${a/fg/dd} #替换从左边数第一个fg为dd
  3. Abcdedd_a_fghijklmn
  4. echo ${a//fg/dd} #替换字符串中所有的fg为dd
  5. Abcdedd_a_ddhijklmn
  6. echo ${/#ab/dd} #如果开头是ab则替换成dd
  7. Ddcdefg_a_fghijklmn
  8. echo ${/#fd/dd} #如果开头是fd则替换成dd
  9. ABCDEFG_A_FGHIJKLMN #开头不是fd不做替换
  10. echo ${a/%mn/dd} #结尾是mn则替换成dd
  11. Abcdefg_a_fghijkldd
  12. echo ${a/%fd/dd} #如果结尾是fd则替换成dd
  13. ABCDEFG_A_FGHIJKLMN #结尾并不是fd不做替换
Iv. Finding and deleting strings

Similar to find substitution, except that there are no replacement values

${var/pattern}: Delete the string in the string represented by Var for the first time that the pattern is matched to
${var//pattern}: Removes all strings that are matched by pattern in the string represented by Var
${var/#pattern}: Removes all strings in the string represented by Var that match the beginning of pattern
${var/%pattern}: Removes all strings in the string represented by Var that match pattern to the end of the line

As with the find replacement syntax, there is no substitute value or slash, and this is not a demonstration.

Five, character case conversion

${var^^}: Converts all lowercase letters in VAR to uppercase
${var,}: Converts all uppercase letters in VAR to lowercase

Summary: The use of string processing inside the shell can improve a lot of productivity and save some of the machine performance, but note that the value of the string processing variable does not change the data of the variable, but only when the output is changed.

Vi. assigning values to string variables

Assigning a value to a variable using a string can replace the partial if else to make the syntax more concise.

Shell string Processing

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.