编程练习3-将文件a处理为文件b

初始文件a.txt

a b
c d
e f
o p
q r
s t

处理后文件b.txt

a b
a b c d
a b c d e f
o p
o p q r
o p q r s t
shell
#!/bin/bash

array1=($(awk '{print $1}' a.txt))
array2=($(awk '{print $2}' a.txt))

length=${#array1[@]}
size=$[$length/2]
cat /dev/null >b.txt

rec=()
for((i=0;i<$size;i++));do      #数组索引从零开始
rec=("${rec[@]}" ${array1[$i]} ${array2[$i]})
echo ${rec[@]} >>b.txt
done

rec=()
for((i=$size;i<$length;i++));do
rec=("${rec[@]}" ${array1[$i]} ${array2[$i]})
echo ${rec[@]} >>b.txt
done
原文地址:https://www.cnblogs.com/ellard/p/14174629.html