how to select, copy columns of different file to form new files

5,198

I think you could do this with paste, and possibly join.

paste file1 file2 > temp1
paste temp1 file3 > temp2
paste tmpe2 file4 > final

That should work if the files contain station 1 thru n data in order. If not, you will need to get the files aligned somehow. If the "stattion n" notation is part of every file, you should sort them:

sort -o file1 file1
sort -o file2 file2
... and so forth

Then, you could do join instead of paste as above. join has some tricky options about putting lines in output that don't have a match in the other file, so you may have to read the join man page very carefully and experiment a bit to get what you want.

Share:
5,198

Related videos on Youtube

AiB
Author by

AiB

Updated on September 18, 2022

Comments

  • AiB
    AiB almost 2 years

    I have four files each contain daily station data.

    • file 1: prec
    • file 2: minT
    • file 3: maxT
    • file 4: wind

    Here prec, minT, maxT and wind are files that store values of prec, minT, maxT and wind for stations 1 through n.

    I want to store the data for each station like:

    for station 1: prec minT maxT wind 
        station 2: prec minT maxT wind
        .
        .
        .
        .
        station n: prec minT maxT wind
    

    EDIT #1

    My four data fies look like as follows:

    Prec

    1        2        3        4        5        6        7        8
    0        0        0        0        0        0        0        0
    0.254        0.254        0.254        0.254        0.254        0.254        0.254        0.254
    0        0        0        0        0        0        0        0
    

    Tmin

    1          2          3          4          5          6          7          8
    -23.349        -23.339        -23.327        -23.316        -23.303        -23.291        -23.278        -23.266
    -23.682        -23.683        -23.685        -23.687        -23.689        -23.692        -23.695        -23.698
    -24.302        -24.301        -24.3        -24.299        -24.299        -24.299        -24.3        -24.302
    

    Tmax

     1         2         3         4         5         6         7         8
    -17.087        -17.082        -17.077        -17.072        -17.066        -17.06        -17.053        -17.046
    -20.082        -20.095        -20.109        -20.124        -20.14        -20.157        -20.174        -20.191
    -20.48        -20.481        -20.483        -20.485        -20.486        -20.488        -20.489        -20.49
    

    wind

    1        2        3        4        5        6        7        8
    0        0        0        0        0        0        0        0
    1.778        1.778        1.778        1.778        1.778        1.778        1.778        1.778
    1.652        1.653        1.654        1.654        1.655        1.656        1.657        1.658
    

    I want to format the data file for each point it's named with (that is for points 1 to 8) as below:

    1

    0        -23.349        -17.087        0
    0.254        -23.682        -20.082        0
    0        -24.302        -20.48        1.778
    

    2

    0        -23.339        -17.082        0
    0.254      -23.683        -20.095        0
    0        -24.301        -20.481        1.778
    

    ...and so on... to n.

    Where the columns in each file would be: nprec, nTmin, nTmax, and nwind.

    • Jeff Hewitt
      Jeff Hewitt almost 11 years
      I think the question is not very clear. Please supply examples of the contents of each of the 4 files and what you want the final result to look like.
    • slm
      slm almost 11 years
      Can you add a sample row or 2 from each of those files? Do they include the date the telemetry data was acquired?
    • slm
      slm almost 11 years
      Please double check what I've tried to clean up in your question still makes sense.
  • AiB
    AiB almost 11 years
    Thank you all for helping me. Problem solved successfully.
  • slm
    slm almost 11 years
    @user45611 - wonderful. If you think this answer was helpful and/or solved your issue directly, please consider voting it up and accepting it.