Shell BASICS (6) -- string and array, shell -- String Array
For text processing, it is still relatively weak to use shell alone. So shell references the awk and sed commands. We will not talk about this today.
One String
A string is the most commonly used and useful data type in shell programming. Because you define a variable, the stored value is a string. A string can be enclosed in single quotation marks, double quotation marks, or no quotation marks.
A=1name="lottu"feel='happy'
Processing strings
1. splice the string echo "$ {name} feels $ {feel}" # That is, space is the String concatenation
2. evaluate the string length echo $ {# name} echo 'expr length $ name' echo $ {name} | awk '{print length ($0 )} '# awk has a variety of functions, so it can be fully used for reference.
3. Capture the string echo $ {name: 1: 4} # output ottu. The subscript starts from 0. This is different from other languages. Expr substr "$ name" 1 4 echo $ {name} | awk '{print substr ($, 4)}' # The output is ottu.
4. index to search for substrings -- return the location where the first occurrence occurs; if not found, 1 expr index "$ name" lo is returned.
5. match substring -- returns the length of matched substring. If no substring is found, 0 expr match "$ name" lott is returned. #4 is returned.
6. replace the substring $ {string/substring/replacement} to replace only the matching character of the substring once, $ {string // substring/replacement} indicates replacing all matched substrings of substrings with str = "you and me, go your home. OK? You "echo $ {str/you/tom} echo $ {str // you/tom}
Conclusion: The command expr can be flexibly used to achieve the above implementation effects using awk. I am very familiar with awk. If you have any questions about text processing, contact me.
2 array
Bash supports one-dimensional arrays (multidimensional arrays are not supported), and the array size is not limited. Similar to the C language, the subscript of an array element starts from 0. To obtain elements in an array, use the subscript. The subscript can be an integer or an arithmetic expression, and its value must be greater than or equal to 0.
1. Define the array in Shell and use parentheses to represent the array. The array elements are separated by spaces. The general format of the defined array is: array_name = (value1... valuen) vname = ("lottu" "li0924" "0924" "tom") 2. print the array value echo $ {vname [2]} # output "0924". What do you understand? Because the subscript of the shell array starts from 0. Use @ or * to obtain all elements in the array echo $ {vname [@]} or echo $ {vname [*]} 3. the length of the retrieved array is almost the same as that of the string. echo $ {# vname [*]} # The output is 4; evaluate the length of a value under an array. echo $ {# vname [1]} # output is 64. delete array unset command unset varray_name [n]: delete nth + 1 unset varray_name Delete array varray_name5. get some arrays -- format: $ {array name [@ or *]: starting position: length} $ vname = ("lottu" "li0924" "0924" "tom ") $ echo $ {vname [*]: 0: 2} lottu li0924 6. extended read command to define the Array Using the parameter a $ read-p "please input your number:"-a num please input your number: 1 5 6 8 $ echo $ {num [*]} 1 5 6 8
Shell obtains characters row by row and assigns values to the array
Because you used the pipeline to pass data to while, the pipeline will start a "sub-process", while is executed in the sub-process, the variables in the child process are not returned to the parent process. Therefore, after the while process ends, you can see that arr is empty, because the arr in the while process is the variable of the child process. You can change it to the following:
I = 0 while read linedo arr [$ {I}] = 'echo $ {line} | awk-F ": "'{print $1}'' (++ I) done <a.txt echo "$ {arr [@]}"
In linux, A = "1 2 3", $ A is an array? Or a string?
$ A is A string in shell.
String to array
Str = "12 34 56"
Arr = ($ str)
Count =$ {# arr [@]}
For (I = 0; I <count; I = I + 1 ))
Do
Echo "$ I = arr [$ I]"
Done
Doudouclever.blog.163.com/..21354/