Blog home: http://blog.csdn.net/minna_d
1. Calculate the string length:
echo ${#str}
2. Truncate the string by character:
${word:begin:length}
3. With%, percent, #, # #对字符串的切割:
%: Right-to-left non-greedy match
Percent: greedy match from right to left
#: No greedy match from left to right
# #: Greedy match from left to right
Usage Example: ${str%.*} ${str%%.*} ${str#*.} ${str##*.}
PS: "." This is just an ordinary point, not an arbitrary character.
[Email protected] ~/q/data/tmp_data]# url= "hotel.qunar.com" [[email protected] ~/q/data/tmp_data]# echo ${url%.*} Hotel.qunar[[email protected] ~/q/data/tmp_data]# echo ${url%%.*}hotel[[email protected] ~/q/data/tmp_data]# echo ${ url#*.} Qunar.com[[email protected] ~/q/data/tmp_data]# echo ${url##*.} Com
4. Using IFS to split a string
IFS is a built-in field delimiter, here is a good description of ifs: http://blog.chinaunix.net/uid-22566367-id-381955.html
PS: Doubt, for str= ' ls-al ' This way I can't figure out what the generated is an array? Or is it a string ?
[[email protected] ~/q/data/tmp_data]# ifs= "\ n" [[email protected] ~/q/data/tmp_data]# str= ' ls-al ' [[Email protected] ~/q/data/tmp_data]# echo $str total dosage 20drwxr-xr-x 2 root root 4096 January 9 13:52. drwxr-xr-x root root 409 6 January 7 19:53.. -rw-r--r--1 root root 12 January 9 13:51 1-rw-r--r--1 root root 10 January 9 13:52 2-rw-r--r--1 root root 28 January 9 13:51 2~[[email protected] ~/q/data/tmp_data]# echo ${str[@]} total usage 20drwxr-xr-x 2 root root 4096 January 9 13:52. drwxr-x R-x root root 4096 January 7 19:53. -rw-r--r--1 root root 12 January 9 13:51 1-rw-r--r--1 root root 10 January 9 13:52 2-rw-r--r--1 root root 28 January 9 13:51 2~[[email protected] ~/q/data/tmp_data]# [[email protected] ~/q/data/tmp_data]# echo ${str[*]} total usage 20drwxr-xr-x 2 root root 4096 January 9 13:52 drwxr-xr-x root root 4096 January 7 19:53.. -rw-r--r--1 root root 12 January 9 13:51 1-rw-r--r--1 root root 10 January 9 13:52 2-rw-r--r--1 root root 28 January 9 13:51 2~[[EMAIL&NBSp;protected] ~/q/data/tmp_data]# echo ${#str [@]}1
Part II:
1. Calculate the length of the array:
${#array [@]}
2. List the array index:
${!array[*]} or ${!array[@]}
3. Print all the values in the array:
${array[*]} or ${array[@]}
Iterate through the array elements in 4.shell:
For STR in ${array[@]};
Do
Echo $str;
Done
5. Defining an empty array
Uset Array
Normal array:
1. Initialize:
The first type of initialization
[Email protected] ~/q/data/tmp_data]# array_origin= (1 2 3 4) [[email protected] ~/q/data/tmp_data]# echo ${#array_origin [@]} 4
The second type of initialization
[Email protected] ~/q/data/tmp_data]# arra_origin[0]=00[[email protected] ~/q/data/tmp_data]# arra_origin[1]=11[[ Email protected] ~/q/data/tmp_data]# arra_origin[2]=22[[email protected] ~/q/data/tmp_data]# arra_origin[3]=33
2. Access method:
[[email protected] ~/q/data/tmp_data]# for item in ${arra_origin[@]}; do Echo $item; done00112233
<strong>ps: How to iterate through an array in awk: </strong><span style= "color: #FF6666;" ><strong>i represents the key, not the value, do not avoid it!!</strong></span>
[Email protected] ~/q/data/tmp_data]# echo "1" | awk ' begin{array[1]=11; array[2]=22;array[3]=33;array[4]=44} {for (i in array) {print i}} ' 1234[[email protected] ~/q/dat a/tmp_data]# echo "1" | awk ' begin{array[1]=11; array[2]=22;array[3]=33;array[4]=44} {for (i in array) {print array[i]}} ' 11223344
Associative arrays:
The first type of display declaration is initialized:
[Email protected] ~/q/data/tmp_data]# declare-a ass_array_1
The second inline index value list is initialized:
[[email protected] ~/q/data/tmp_data]# ass_array_2= (["Host1"]= "192.168.1.23" ["Ip1"]= "192.168.1.23")
Assignment Value:
[Email protected] ~/q/data/tmp_data]# ass_array["host"]= "192.168.1.23" [[email protected] ~/q/data/tmp_data]# Ass_ array["IP"]= "192.168.1.23"
visit:
Query value by key
[Email protected] ~/q/data/tmp_data]# echo ${ass_array_1["host"]}192.168.1.23
View all keys
[[email protected] ~/q/data/tmp_data]# echo ${!ass_array_1[*]}host1 IP
View All value
[Email protected] ~/q/data/tmp_data]# echo ${ass_array[*]}192.168.1.23 192.168.1.23
Shell string and array summary