If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Merging files in a single one

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-24-04, 13:18
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Question Merging files in a single one

Hi all.

Hope someone can help on this one.

I have several files, each of them containing a single colum
of numbers. How can I create a single file containing all these
different columns ?

file1 :
1
3
4
5

file2 :
-3
0
10
4

file3 :
3
6
3
7

after merging files :
1 -3 3
3 0 6
4 10 3
5 4 7


I appreciate any help ! (the real problem contains 400 files
with 200 rows each...)

Thanks,

Serg
Reply With Quote
  #2 (permalink)  
Old 05-24-04, 14:17
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
man paste
man pr
Reply With Quote
  #3 (permalink)  
Old 05-24-04, 15:01
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
thanks for the help !
Reply With Quote
  #4 (permalink)  
Old 05-25-04, 03:48
supinformix supinformix is offline
Registered User
 
Join Date: Apr 2004
Location: Brussels
Posts: 57
open different file descriptors :
exec 3 <f1
exec 4 <f2
exec 5 <f3
while read F1
do
read F2 < &4
read F3 < &5
echo "$F1 \n $F2 \n $F3" >>output
done < &3

problem is what if file f2 has less rows than f1.
__________________
Yves & Willy
Reply With Quote
  #5 (permalink)  
Old 05-25-04, 07:13
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally Posted by supinformix
open different file descriptors :
exec 3 <f1
exec 4 <f2
exec 5 <f3
while read F1
do
read F2 < &4
read F3 < &5
echo "$F1 \n $F2 \n $F3" >>output
done < &3

problem is what if file f2 has less rows than f1.
You can get round that something like this...

Code:
exec 3<file1 4<file2 5<file3
while read file1Var <&3 && isVarRead=true;
      read file2Var <&4 && isVarRead=true;
      read file3Var <&5 && isVarRead=true;
      ${isVarRead-false}
do
  print ${file1Var} ${file2Var} ${file3Var}
  unset isVarRead
done
Admittedly, this does the same as 'paste' but it allows you to tinker with the 'print' line to output exactly what you would like.

You could also use awk, e.g. while (getline < "file1" > 0) { ...

Damian
Reply With Quote
  #6 (permalink)  
Old 05-25-04, 08:43
Serg Serg is offline
Registered User
 
Join Date: Feb 2004
Posts: 52
Thanks guys.

I was looking for some awk solution but your
suggestions were also great.

Serg
Reply With Quote
  #7 (permalink)  
Old 05-28-04, 15:40
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Hi Damian,
Can you walk through me the above code?
alsosintereseted to see awk solution for my post 'to find a match between two files'
dated May 28.

Thanks,
Reply With Quote
  #8 (permalink)  
Old 05-28-04, 16:51
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Assuming all your input files have the same numbe rof records/lines:

nawk -f sdk.awk file*

Code:
{
  arr[FNR] = (NR == FNR) ? $0 : arr[FNR] OFS $0
}

END {
  for(i=1; i <= FNR; i++)
    print arr[i];
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On