Operating System - HP-UX
1834227 Members
2685 Online
110066 Solutions
New Discussion

Re: Merging two files or three files in column wise.

 
SOLVED
Go to solution
jayachandran.g
Regular Advisor

Merging two files or three files in column wise.

hi all

i have 3 files say file1,file2, file3

one file is having only counters, say file1

the next two files (file2,file3) have the values for the counters of file1.

now i want to merge all the files so that i will get

file1 file2 file3
counter value1 value2

there are lots of counters like this.

and paste command failed to do this.

thank you
6 REPLIES 6
Alan Meyer_4
Respected Contributor
Solution

Re: Merging two files or three files in column wise.

echo "file1 file2 file3" >outfile
cat file1 |while read counter ;do
cat file2 |while read value1 ;do
cat file3 |while read value2 ;do
echo "$counter $value1 $value2" >>outfile
done
done
done
" I may not be certified, but I am certifiable... "
jayachandran.g
Regular Advisor

Re: Merging two files or three files in column wise.

Hi Alan

Tried your command but it failed.

it gone on loop and it is not ending at all
now i tried with 2 files only.

the output was like this

counter1
counter2
counter3
value1
value2
value3
Chris Wilshaw
Honored Contributor

Re: Merging two files or three files in column wise.

How are you using the paste command? It should work.

eg:

create file1 containing
111
222
333
444

file2 containing
aaa
bbb
ccc
ddd

file3 containing
zzz
yyy
xxx
www

running "paste file1 file2 file3" gives the tab-separated output

111 aaa zzz
222 bbb yyy
333 ccc xxx
444 ddd www
Alan Meyer_4
Respected Contributor

Re: Merging two files or three files in column wise.

I forgot about paste. That's the best solution.
" I may not be certified, but I am certifiable... "
jayachandran.g
Regular Advisor

Re: Merging two files or three files in column wise.

Sorry..

the problem was there in my command..
cris and alan it worked file thank you
thanks a lot.
Alan Meyer_4
Respected Contributor

Re: Merging two files or three files in column wise.

Here's a better version (after a bit more thought)

echo "f1 f2 f3"
x=1
cat f1 |while read counter[$x] ;do
(( x = x + 1 ))
done
x=1
cat f2 |while read value1[$x] ;do
(( x = x + 1 ))
done
x=1
cat f3 |while read value2[$x] ;do
(( x = x + 1 ))
done

(( max = x - 1 ))
x=1

while [ "$x" -le "$max" ] ;do
echo ${counter[$x]} ${value1[$x]} ${value2[$x]}
(( x = x + 1 ))
done
" I may not be certified, but I am certifiable... "