Bash Shell String Operations Summary _linux Shell

Source: Internet
Author: User

1. Take the length

Copy Code code as follows:
Str= "ABCD"
Expr Length $str # 4
echo ${#str} # 4
Expr "$str": ". *" # 4

It seems that the second type is commonly used
2. Find the position of substring
Copy Code code as follows:
Str= "ABC"
Expr index $str "a" # 1
Expr index $str "B" # 2
Expr index $str "x" # 0
Expr index $str "" # 0

3. Select substring
Copy Code code as follows:
Str= "ABCdef"
Expr substr "$str" 1 3 # starting from the first position 3 characters, ABC
Expr substr "$str" 2 5 # Take 5 characters starting from the second position, Bcdef
Expr substr "$str" 4 5 # starting from fourth position 5 characters, Def
Echo ${str:2} # Extracts the string from the second position, Bcdef
Echo ${str:2:3} # Extracts 3 characters starting from the second position, BCD
echo ${STR: (-6): 5} # from the penultimate position to the left to extract the string, ABCDE
echo ${STR: (-4): 3} # Extract 6 characters to the left from the penultimate position, CDE

4. Intercept substring
Copy Code code as follows:
Str= "ABBC,DEF,GHI,ABCJKL"
echo ${str#a*c} # OUTPUT, def,ghi,abcjkl a well sign (#) to intercept the shortest match from the left (the ABBC string is removed here)
echo ${str# #a *c} # Output JKL, two well sign (# #) to intercept the longest match from the left (the ABBC,DEF,GHI,ABC string is removed here)
echo ${str# "A*c"} # Output ABBC,DEF,GHI,ABCJKL because there is no "a*c" substring in str
echo ${str## "A*c"} # Output ABBC,DEF,GHI,ABCJKL empathy
Echo ${str#*a*c*} # Empty
Echo ${str##*a*c*} # Empty
echo ${str#d*f) # output ABBC,DEF,GHI,ABCJKL,
echo ${str#*d*f} # OUTPUT, GHI,ABCJKL
Echo ${str%a*l} # Abbc,def,ghi a percent semicolon (%) that captures the shortest match from the right
Echo ${str%%b*l} # A two percent sign (%) indicates that the longest match is intercepted from the right
Echo ${str%a*c} # ABBC,DEF,GHI,ABCJKL
Memory can be so, the pound (#) is usually used to represent a number, it is placed in front of the percent (%) of the unload number behind; Or this memory, in the keyboard layout, the pound sign (#) is always on the left side of the percent (%) (that is, the front)

5. String replacement
Copy Code code as follows:
Str= "apple, tree and apple tree"
Echo ${str/apple/apple} # Replaces the first occurrence of Apple
Echo ${str//apple/apple} # replaces all Apple
echo ${str/#apple/apple} # If string str starts with Apple, replace it with Apple
Echo ${str/%apple/apple} # If string str ends with Apple, replace it with Apple

6. Compare
Copy Code code as follows:
[["A.txt" = *]] # logical true (pattern matching)
[["A.txt" =~. *\.txt]] # logical true (regex matching)
[["ABC" = "abc"]] # Logical True (string comparision)
[[< 2]] # logical True (string comparision), by ASCII value comparison

7. Connection
Copy Code code as follows:
s1= "Hello"
S2= "World"
Echo ${s1}${s2} # Of course it $s 1$s2, but it's best to add braces.

8. Replace all strings in a batch of files

Copy Code code as follows:
For I in File_list
Todo
VI $i <<-!
: g/xxxx/s//xxxx/g
: Wq
!
Done

9. Flip

Method One:
Use Rev command

Method Two:
Writing a script implementation

Copy Code code as follows:
#!/usr/bin/awk-f
################################################################
# Description:duplicate Rev in Awk
################################################################
{
Revline = ""
for (i=1;i<=length;i++)
{
Revline = substr (, i,1) revline
}
}
End{print Revline}


10. Matching

Copy Code code as follows:
Grep
Egrep
Fgrep

11. Get the number of repetitions of a character in a string

Copy Code code as follows:
echo $a |tr "x" "\ n" |wc-l

The resulting results need to be subtracted by 1.
Or
Copy Code code as follows:
echo $a |awk-f "x" ' {Print NF-1} '


12. How to insert a character in the middle of every two characters in a string
Using SED

Copy Code code as follows:
echo $test |sed ' s/. /&[insert char]/g '

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.