我有以下文件
acc1:server1:server2:acc1234:blah:blah
acc2:server1:server5:acc4321:blah:blah
acc3:server1:server3:acc2222:blah:blah
上述文件的每一行都是数组中的元素。我只需要获得前三项accX:serverX:serverX
。
如何使用ksh93在没有“:”的情况下仅拉出每个元素的前三项?
谢谢!
答案 0 :(得分:1)
这适合你吗?
$ set -A myarr acc1:server1:server2:acc1234:blah:blah acc2:server1:server5:acc4321:blah:blah acc3:server1:server3:acc2222:blah:blah
$ $ for ele in ${myarr[@]}; do ele=${ele%:*:*:*}; echo ${ele//:/ }; done
acc1 server1 server2
acc2 server1 server5
acc3 server1 server3
答案 1 :(得分:0)
#!/bin/ksh
file=~/kshinput.txt
index=0
while read line
do
myarray[$index]=${line%:*:*:*}
((index=index+1))
done <"$file"
for i in {0..$index}
do
echo "$i: ${myarray[$i]}"
done