AWK输出到bash数组 [英] AWK output to bash Array
本文介绍了AWK输出到bash数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将简单命令的内容放入bash数组中,但是我有点麻烦.
Im trying to put the contents of a simple command in to a bash array however im having a bit of trouble.
df -h | awk '{ print $5" "$6 }'
给出了我系统上文件系统中使用的百分比 输出看起来像这样:
gives percentage used in the file systems on my system output looks like this:
1% /dev
1% /dev/shm
1% /var/run
0% /var/lock
22% /boot
22% /home
22% /home/steve
然后我想将这些行中的每行放入bash数组中 array = $(df -h | awk'{print $ 5 $ 6}')
I would then like to put each of these lines into a bash array array=$(df -h| awk '{ print $5 $6 }')
但是,当我打印出数组时,我得到以下信息:
However when I print out the array I get the following:
5%
/
1%
/dev
1%
/dev/shm
1%
/var/run
0%
/var/lock
22%
/boot
22%
/home
22%
/home/steve
Bash根据空格形成数组,而不是换行符,我该如何解决?
Bash is forming the array based on white spaces and not line breaks how can i fix this?
推荐答案
您需要重置IFS变量(用于数组的分度符).
You need to reset the IFS variable (the delimeter used for arrays).
OIFS=$IFS #save original
IFS=','
df -h | awk '{ print $5" "$6"," }'
这篇关于AWK输出到bash数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文