1

我有以下文件

acc1:server1:server2:acc1234:blah:blah
acc2:server1:server5:acc4321:blah:blah
acc3:server1:server3:acc2222:blah:blah

上述文件的每一行都是数组中的一个元素。我只需要得到前三个项目accX:serverX:serverX

如何使用 ksh93 只提取每个元素的前三项而不使用“:”?

谢谢!

4

2 回答 2

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
于 2013-06-22T19:25:42.603 回答
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
于 2013-06-22T20:00:39.657 回答