Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2004
    Posts
    52

    Question Unanswered: 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

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    man paste
    man pr

  3. #3
    Join Date
    Feb 2004
    Posts
    52
    thanks for the help !

  4. #4
    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

  5. #5
    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

  6. #6
    Join Date
    Feb 2004
    Posts
    52
    Thanks guys.

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

    Serg

  7. #7
    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,

  8. #8
    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];
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •