我有2个文件: /tmp/first.txt和 /tmp/last.txt
cat /tmp/first.txt
john
adam
max
cat /tmp/last.txt
smith
moore
caviar
我想结合这两个文件的内容,像这样的东西(输出):
john smith
adam moore
max caviar
我已经做过的事情:
first=()
getfirst() {
i=0
while read line # Read a line
do
array[i]=$line # Put it into the array
i=$(($i + 1))
done < $1
}
getfirst "/tmp/first.txt"
for a in "${array[@]}"
do
echo "$a"
done
last=()
getlast() {
i=0
while read line # Read a line
do
array[i]=$line # Put it into the array
i=$(($i + 1))
done < $1
}
getlast "/tmp/first.txt"
for b in "${array[@]}"
do
echo "$b"
done
我做了一些相似的事情(使用迭代):
for x in {1..2}
do
echo $a[$x] $b[$x];
done
但输出仅为:
max caviar
答案 0 :(得分:6)
更简单的解决方案是使用paste
:
$ paste -d ' ' first.txt last.txt
john smith
adam moore
max caviar
如果您没有paste
,请使用数组:
$ first=($(cat first.txt))
$ last=($(cat last.txt))
$ for ((i = 0; i < 3; ++i)); do echo ${first[$i]} ${last[$i]}; done
john smith
adam moore
max caviar