Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell: scripts #166

Open
xvno opened this issue Aug 25, 2021 · 1 comment
Open

Shell: scripts #166

xvno opened this issue Aug 25, 2021 · 1 comment

Comments

@xvno
Copy link
Owner

xvno commented Aug 25, 2021

Refs

@xvno
Copy link
Owner Author

xvno commented Aug 25, 2021

用括号包围字串列表即可实现

  • 方法一: 借助于{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[@]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant