Re: [Help-bash] what's the best way to join two parts
From:
Greg Wooledge
Subject:
Re: [Help-bash] what's the best way to join two parts
Date:
Fri, 30 Dec 2011 08:23:48 -0500
User-agent:
Mutt/1.4.2.3i
On Thu, Dec 29, 2011 at 11:26:12PM +0800, lina wrote:
> $ cat a_3.txt
> aaa
> bbb
>
> 123
> 321
>
> what if I want to join two parts together, the output wish to be
>
> aaa 123
> bbb 321
Read the first part into an array (or a temp file)....
> and then
> paste b_1.txt b_2.txt
So you already know about the temp file answer, but you don't want to
do that. OK. Then let's use an array:
#!/usr/bin/env bash
# Load the first part of the file into array "arr", and stop reading when
# we find a blank line.
unset arr i
while read -r; do
if [[ $REPLY = "" ]]; then
break
fi
arr[i++]=$REPLY
done
# Now continue reading the second part of the file.
i=0
while read -r; do
echo "${arr[i++]} $REPLY"
done