String processing-$ {#}, expr length, expr index, expr match, extract substrings

Source: Internet
Author: User
{#}, Expr length "$ Str" [[email protected] shell] # STR = "My name is mah" [[email protected] shell] # echo $ {# STR} 14 [[email protected] shell] # expr length $ strexpr: syntax error [[email protected] shell] # expr length "$ Str" # Double quotation marks must be added to 14 ####### expr Index "$ Str" Mah [[email protected] shell] # STR = "My name is mah" [[email protected] shell] # expr Index "$ Str" mah12 [[email protected] shell] # expr Index "$ str "O # No, returns 00 [[email protected] shell] # expr Index "$ Str" ay # Although ay exists, however, the returned result is Y location 2 [[email protected] shell] # expr Index "$ Str" an4

###### Expr match $ string $ substring

Matches the substring string at the beginning of string and returns the length of the matched string.

[[Email protected] shell] # STR = "My name is mah" [[email protected] shell] # expr match "$ Str" My * 2 [[email protected] shell] # expr match "$ Str" Mah # although the string contains Mah, however, mah does not start with 0 [[email protected] shell] # expr match "$ Str" M * 1 [[email protected] shell] # expr match "$ Str" M. * # regular expression. * Indicates all 14
#### $ {STR: Position: length} string pruning, two Methods: [email protected] shell] # STR = "ABC def Ghi" [[email protected] shell] # echo $ {STR: 0} ABC def Ghi [[email protected] shell] # echo $ {STR: 0: 3} #0 indicates the first digit, 3 represents the length ABC [[email protected] shell] # echo $ {STR: 0: 5} ABC d [[email protected] shell] # echo $ {STR: -3} #-There is no space at the front, show all strings ABC def ghiyou have new mail in/var/spool/mail/root [[email protected] shell] # echo $ strabc def Ghi [[email protected] shell] # echo $ {STR: -3 }#-trim the Ghi [[email protected] shell] # echo $ {STR: -6} EF Ghi [[email protected] shell] # expr substr "$ Str" 1 3 # expr substr "$ Str" format. The first digit is the 1st digit, there must be two parameters: ABC [[email protected] shell] # expr substr "$ Str" 1 5abc d

###### Use a regular expression when processing a string. Note: There must be spaces on both sides of the colon; otherwise, the syntax is incorrect.

Substring starting with a string: Two Methods

[[Email protected] shell] # STR = "012345 my name is mah" [[email protected] shell] # expr match "$ Str" '\ ([0-9] * \) '# Method 1: Use match012345 [[email protected] shell] # expr "$ Str":' \ ([0-9] * \) '# Method 2: Use the colon 012345

Cut the substring at the end of the string: the difference is that \ (add before .*

[[Email protected] shell] # echo $ str012345 my name is mah [[[email protected] shell] # expr match "$ Str "'. *\(... \)'#. it indicates a character mah [[email protected] shell] # expr "$ Str ":'. *\(... \) 'mah


###### Delete a substring: Format: $ {}

Category 1: delete from the beginning

[[Email protected] shell] # echo $ str012345 my name is mah [[[email protected] shell] # echo $ {STR #0123} 45 my name is mah [[email protected] shell] # echo $ {STR #345} 012345 my name is mah [[email protected] shell] # echo $ {STR #0*5} # Here * is not a regular expression, it only indicates all characters from 0 to 5, my name is mah [[[email protected] shell] # echo $ {STR #0*5 }## greedy mode my name is mah

Category 2: deleting from the end

[[Email protected] shell] # echo $ {STR % mah} 012345 my name is [[email protected] shell] # echo $ {STR % m * h} 012345 [[ email protected] shell] # echo $ {STR % m. * h} # It can be seen that * is not a regular expression 012345 my name is mah

####### Replace a string

[[Email protected] shell] # STR = "0011 my name is mah 0011" [[email protected] shell] # echo $ {str/0*1/YYY} YYY [[ email protected] shell] # echo ${str/0 **/YYY} YYY [[[email protected] shell] # echo $ {str/0*0/YYY} # * not a regular expression, however, it indicates all the characters yyy11 [[[email protected] shell] # echo $ {STR // 0*0/YYY} yyy11 [[email protected] shell] # echo ${str/0 * m/YYY} yyyah 0011 another two special replacements #1. replace [[email protected] shell] # echo $ {str/#0011/MAH} mah my name is mah 0011 #2. replace [[email protected] shell] # echo $ {STR} 0011 my name is mah 0011 [[email protected] shell] # echo $ {str/% 0011/MAH} 0011 my name is mah 0011 [[email protected] shell] # STR = "123 my name is mah" [[email protected] shell] # echo $ {str/% m * H /MAH} 123 MAH

This article is from the "8176010" blog and will not be reproduced!

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.