Shell: scripts
Opened this issue · 1 comments
xvno commented
Refs
- Shell Scripting Tutorial: https://www.shellscript.sh/index.html
- How to Compare Strings in Bash: https://linuxize.com/post/how-to-compare-strings-in-bash/
- RegEx: Find IP Addresses in a File Using Grep: https://www.shellhacks.com/regex-find-ip-addresses-file-grep/
- How to split a string into an array: https://stackoverflow.com/questions/10586153/how-to-split-a-string-into-an-array-in-bash
- How to use a key-value dictionary: https://www.xmodulo.com/key-value-dictionary-bash.html
xvno commented
用括号包围字串列表即可实现
-
方法一: 借助于{str//,/}来处理
[root@host ~]# str="ONE,TWO,THREE,FOUR"
[root@host ~]# arr=(${str//,/})
[root@host ~]# echo ${arr[@]}
ONE TWO THREE FOUR
-
方法二: 借助于tr命令来处理
[root@host ~]# str="ONE,TWO,THREE,FOUR"
[root@host ~]# arr=(`echo $str | tr ',' ' '`)
[root@host ~]# echo ${arr[@]}
ONE TWO THREE FOUR
-
方法三: 借助于awk命令来处理
[root@host ~]# str="ONE,TWO,THREE,FOUR"
[root@host ~]# arr=($(echo $str | awk 'BEGIN{FS=",";OFS=" "} {print $1,$2,$3,$4}'))
[root@host ~]# echo ${str[*]}
-
方法四: 借助于IFS来处理分隔符
[root@host ~]# str="ONE,TWO,THREE,FOUR"
[root@host ~]# IFS=","
[root@host ~]# arr=(str)
[root@host ~]# echo ${str[@]}