Operating System - HP-UX
1833800 Members
2675 Online
110063 Solutions
New Discussion

Scripting question - joining files line by line

 
SOLVED
Go to solution
Jay Gaffney_4
Advisor

Scripting question - joining files line by line

Hi

I am trying to join two files line by line. The join(1) command chokes due to line length.

Lets say the files have a format comma sep. format like this -

File1
A1,A2,A3,...,A255
1,aaa,aaa,aaa,...,aaa
2,bbb,bbb,bbb,...,bbb

File2
B1,B2,B3,...,B255
1,ccc,ccc,ccc,...,ccc
2,ddd,ddd,ddd,...,ddd

I want to end up with -
A1,A2,...,A255,B1,B2,...,B255
1,aaa,...,aaa,1,ccc,ccc,...,ccc
2,bbb,...,bbb,2,ddd,ddd,...,ddd

Not a straight cat, but a concatenation on a line by line basis.
join chokes, "join: Too many fields in line A1" I don't know enough Perl, :-(

Any ideas??

THX,

Jay
4 REPLIES 4
Roger Baptiste
Honored Contributor
Solution

Re: Scripting question - joining files line by line

Jay,

PASTE it!!

#paste file1 file2 >> file3

will do what you are looking for.

cheers
-raj
Take it easy.
James R. Ferguson
Acclaimed Contributor

Re: Scripting question - joining files line by line

Hi Jay:

How about this:

#!/usr/bin/sh
#
F1=/tmp/file1
F2=/tmp/file2
#
while read R1
do
read R2 < $F2
echo $R1 $R2 >> /tmp/output
done < $F1
#
exit 0
#.end.

Regards!

...JRF...
Michael Tully
Honored Contributor

Re: Scripting question - joining files line by line

Hi,

As long as each of the files have the same
number of lines you could try the paste.

paste -d file1 file2 > file 3

HTH
-Michael
Anyone for a Mutiny ?
Sridhar Bhaskarla
Honored Contributor

Re: Scripting question - joining files line by line

If it is in the same format as you specified, I will try to write a crude program to see if it helps.

cp file1 file1$$
cp file2 file2$$
for i in `cat file1$$`
do
LINE1=`sed -n '1p' file1$$`
LINE2=`sed -n '1p' file2$$`
echo $LINE1,$LINE2 >> result
sed '1d' file1$$ >> t
mv t file1$$
sed '1d' file2$$ >> t
mv t file2$$
LEN1=`wc -l file1$$|awk '{print $1}' `
LEN2=`wc -l file2$$|awk '{print $1}' `
if [ $LEN1 = 0 ]
then
cat file2$$ >> result
exit
fi
if [ $LEN2 = 0 ]
then
cat file1$$ >> result
exit
fi
done

rm file1$$ file2$$

You may have to replace for with while if the format is not the same and if there are any spaces or tabs in the lines.

PS: I don't know if this works. I don't have a system in front of me. Butyou can use this logic.


-Sri
You may be disappointed if you fail, but you are doomed if you don't try