Reputation: 1
I have 2 files: /tmp/first.txt and /tmp/last.txt
cat /tmp/first.txt
john
adam
max
cat /tmp/last.txt
smith
moore
caviar
I want to combine the contents of these two files, something (output) like this:
john smith
adam moore
max caviar
what i've already did:
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
I've done some look alike (using iteration):
for x in {1..2}
do
echo $a[$x] $b[$x];
done
but the output is only:
max caviar
Upvotes: 1
Views: 55
Reputation: 124646
A simpler solution is using paste
:
$ paste -d ' ' first.txt last.txt
john smith
adam moore
max caviar
If you don't have paste
, then using arrays:
$ 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
Upvotes: 6