您的位置:首页 > 运维架构 > Shell

shell 按行读取并保存成数组

2012-11-15 15:42 357 查看
博主:http://mark-ztw.iteye.com/blog/1535480

shell 按行读取并保存成数组

从ip.txt里读取IP.然后把IP地址赋值到一个数组里.

IP文件如下:

Address: 220.181.26.163

Address: 220.181.26.174

Address: 220.181.26.175

Address: 220.181.26.176

Address: 220.181.19.228

Address: 220.181.19.229

Address: 220.181.26.161

Address: 220.181.26.162

Shell代码

方法一:

for x in ` awk '{print $2}' ip.txt `

{

echo $x

}

方法二:

ARRAY=($(awk '{print $2}' ip.txt))

方法三:

n=0;while read a b;do array[$n]=$b;((n++));done<ip.txt

方法四:

n=1

while ((n<=$(cat ip.txt|wc -l)))

do

ipaddr[$n]=$(cat ip.txt|sed -n "${n}p"|awk '{print $2}')

((n+=1))

done

n=`expr $n - 1`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: