Append n columns together

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a large dataset, which shows how variable y changes as a function of both space (x) and time (t). There are n columns, each representing one time step. They are tab delimited.



note: there are no headers in the actual text file, they are just added here for explanation. Nor should there be headers in the output.



x y(0) y(1) y(2) y(3) ... y(n)
1 4 4.5 5 5.5 ... 100
2 5 5.5 6 6.5 ... 101
3 7 8 9 10 ... 102
4 10 12 14 16 ... 103


I need to reorganise my file so that I have only 3 columns; t, x, y, which should be sorted in that order, as below.



0 1 4
0 2 5
0 3 7
0 4 10
1 1 4.5
1 2 5.5
1 3 8
1 4 12
2 1 5
2 2 6
2 3 9
2 4 14
3 1 5.5
3 2 6.5
3 3 10
3 4 16
etc


Any help much appreciated. I feel like this should be possible using awk, but any solution is fine.







share|improve this question






















  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 4 at 16:35














up vote
0
down vote

favorite












I have a large dataset, which shows how variable y changes as a function of both space (x) and time (t). There are n columns, each representing one time step. They are tab delimited.



note: there are no headers in the actual text file, they are just added here for explanation. Nor should there be headers in the output.



x y(0) y(1) y(2) y(3) ... y(n)
1 4 4.5 5 5.5 ... 100
2 5 5.5 6 6.5 ... 101
3 7 8 9 10 ... 102
4 10 12 14 16 ... 103


I need to reorganise my file so that I have only 3 columns; t, x, y, which should be sorted in that order, as below.



0 1 4
0 2 5
0 3 7
0 4 10
1 1 4.5
1 2 5.5
1 3 8
1 4 12
2 1 5
2 2 6
2 3 9
2 4 14
3 1 5.5
3 2 6.5
3 3 10
3 4 16
etc


Any help much appreciated. I feel like this should be possible using awk, but any solution is fine.







share|improve this question






















  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 4 at 16:35












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a large dataset, which shows how variable y changes as a function of both space (x) and time (t). There are n columns, each representing one time step. They are tab delimited.



note: there are no headers in the actual text file, they are just added here for explanation. Nor should there be headers in the output.



x y(0) y(1) y(2) y(3) ... y(n)
1 4 4.5 5 5.5 ... 100
2 5 5.5 6 6.5 ... 101
3 7 8 9 10 ... 102
4 10 12 14 16 ... 103


I need to reorganise my file so that I have only 3 columns; t, x, y, which should be sorted in that order, as below.



0 1 4
0 2 5
0 3 7
0 4 10
1 1 4.5
1 2 5.5
1 3 8
1 4 12
2 1 5
2 2 6
2 3 9
2 4 14
3 1 5.5
3 2 6.5
3 3 10
3 4 16
etc


Any help much appreciated. I feel like this should be possible using awk, but any solution is fine.







share|improve this question














I have a large dataset, which shows how variable y changes as a function of both space (x) and time (t). There are n columns, each representing one time step. They are tab delimited.



note: there are no headers in the actual text file, they are just added here for explanation. Nor should there be headers in the output.



x y(0) y(1) y(2) y(3) ... y(n)
1 4 4.5 5 5.5 ... 100
2 5 5.5 6 6.5 ... 101
3 7 8 9 10 ... 102
4 10 12 14 16 ... 103


I need to reorganise my file so that I have only 3 columns; t, x, y, which should be sorted in that order, as below.



0 1 4
0 2 5
0 3 7
0 4 10
1 1 4.5
1 2 5.5
1 3 8
1 4 12
2 1 5
2 2 6
2 3 9
2 4 14
3 1 5.5
3 2 6.5
3 3 10
3 4 16
etc


Any help much appreciated. I feel like this should be possible using awk, but any solution is fine.









share|improve this question













share|improve this question




share|improve this question








edited Jan 31 at 11:42

























asked Jan 30 at 16:21









L. Marsden

276




276











  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 4 at 16:35
















  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 4 at 16:35















If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 4 at 16:35




If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 4 at 16:35










3 Answers
3






active

oldest

votes

















up vote
2
down vote













GNU awk solution:



awk '
k=NR; x[k]=$1;
for (i=2; i<=NF; i++)
t[i-1][k]=$i

END
for (i in t)
for (j in t[i])
print i-1, x[j], t[i][j]
' file



  • k=NR - crucial key reflecting function y axis value (NR - record number)


  • x[k]=$1 - capture value for axis x


  • for (i=2; i<=NF; i++) - iterating though fields starting from the 2nd


    • t[i-1][k]=$i - fill up the time axis array t with function y values



The output:



0 1 4
0 2 5
0 3 7
0 4 10
1 1 4.5
1 2 5.5
1 3 8
1 4 12
2 1 5
2 2 6
2 3 9
2 4 14
3 1 5.5
3 2 6.5
3 3 10
3 4 16
...





share|improve this answer






















  • A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
    – L. Marsden
    Jan 31 at 9:23











  • If you could update the answer how you recommend that would be very useful.
    – L. Marsden
    Jan 31 at 9:30










  • @L.Marsden, see my update
    – RomanPerekhrest
    Jan 31 at 9:53










  • I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
    – L. Marsden
    Jan 31 at 11:16











  • @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
    – RomanPerekhrest
    Jan 31 at 11:47

















up vote
0
down vote













If you don't mind looping over the input n times:



n=4 ### your N here
for((t=0; t <= n)); t++))
do
awk -F$'t' -v t=$t 'print t, $1, $(t+2)' < input
done > output





share|improve this answer



























    up vote
    0
    down vote













    Alternative GNU datamash + awk solution:



    datamash -W transpose <filename 
    | awk 'NR==1 for(i=1; i<=NF; i++) x[i]=$i
    NR > 1
    for (i=1; i<=NF; i++) print NR-2, x[i], $i
    '


    The output:



    0 1 4
    0 2 5
    0 3 7
    0 4 10
    1 1 4.5
    1 2 5.5
    1 3 8
    1 4 12
    2 1 5
    2 2 6
    2 3 9
    2 4 14
    3 1 5.5
    3 2 6.5
    3 3 10
    3 4 16





    share|improve this answer






















    • @L.Marsden, the answer is updated to omit header line/column
      – RomanPerekhrest
      Jan 31 at 10:00










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420713%2fappend-n-columns-together%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    GNU awk solution:



    awk '
    k=NR; x[k]=$1;
    for (i=2; i<=NF; i++)
    t[i-1][k]=$i

    END
    for (i in t)
    for (j in t[i])
    print i-1, x[j], t[i][j]
    ' file



    • k=NR - crucial key reflecting function y axis value (NR - record number)


    • x[k]=$1 - capture value for axis x


    • for (i=2; i<=NF; i++) - iterating though fields starting from the 2nd


      • t[i-1][k]=$i - fill up the time axis array t with function y values



    The output:



    0 1 4
    0 2 5
    0 3 7
    0 4 10
    1 1 4.5
    1 2 5.5
    1 3 8
    1 4 12
    2 1 5
    2 2 6
    2 3 9
    2 4 14
    3 1 5.5
    3 2 6.5
    3 3 10
    3 4 16
    ...





    share|improve this answer






















    • A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
      – L. Marsden
      Jan 31 at 9:23











    • If you could update the answer how you recommend that would be very useful.
      – L. Marsden
      Jan 31 at 9:30










    • @L.Marsden, see my update
      – RomanPerekhrest
      Jan 31 at 9:53










    • I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
      – L. Marsden
      Jan 31 at 11:16











    • @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
      – RomanPerekhrest
      Jan 31 at 11:47














    up vote
    2
    down vote













    GNU awk solution:



    awk '
    k=NR; x[k]=$1;
    for (i=2; i<=NF; i++)
    t[i-1][k]=$i

    END
    for (i in t)
    for (j in t[i])
    print i-1, x[j], t[i][j]
    ' file



    • k=NR - crucial key reflecting function y axis value (NR - record number)


    • x[k]=$1 - capture value for axis x


    • for (i=2; i<=NF; i++) - iterating though fields starting from the 2nd


      • t[i-1][k]=$i - fill up the time axis array t with function y values



    The output:



    0 1 4
    0 2 5
    0 3 7
    0 4 10
    1 1 4.5
    1 2 5.5
    1 3 8
    1 4 12
    2 1 5
    2 2 6
    2 3 9
    2 4 14
    3 1 5.5
    3 2 6.5
    3 3 10
    3 4 16
    ...





    share|improve this answer






















    • A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
      – L. Marsden
      Jan 31 at 9:23











    • If you could update the answer how you recommend that would be very useful.
      – L. Marsden
      Jan 31 at 9:30










    • @L.Marsden, see my update
      – RomanPerekhrest
      Jan 31 at 9:53










    • I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
      – L. Marsden
      Jan 31 at 11:16











    • @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
      – RomanPerekhrest
      Jan 31 at 11:47












    up vote
    2
    down vote










    up vote
    2
    down vote









    GNU awk solution:



    awk '
    k=NR; x[k]=$1;
    for (i=2; i<=NF; i++)
    t[i-1][k]=$i

    END
    for (i in t)
    for (j in t[i])
    print i-1, x[j], t[i][j]
    ' file



    • k=NR - crucial key reflecting function y axis value (NR - record number)


    • x[k]=$1 - capture value for axis x


    • for (i=2; i<=NF; i++) - iterating though fields starting from the 2nd


      • t[i-1][k]=$i - fill up the time axis array t with function y values



    The output:



    0 1 4
    0 2 5
    0 3 7
    0 4 10
    1 1 4.5
    1 2 5.5
    1 3 8
    1 4 12
    2 1 5
    2 2 6
    2 3 9
    2 4 14
    3 1 5.5
    3 2 6.5
    3 3 10
    3 4 16
    ...





    share|improve this answer














    GNU awk solution:



    awk '
    k=NR; x[k]=$1;
    for (i=2; i<=NF; i++)
    t[i-1][k]=$i

    END
    for (i in t)
    for (j in t[i])
    print i-1, x[j], t[i][j]
    ' file



    • k=NR - crucial key reflecting function y axis value (NR - record number)


    • x[k]=$1 - capture value for axis x


    • for (i=2; i<=NF; i++) - iterating though fields starting from the 2nd


      • t[i-1][k]=$i - fill up the time axis array t with function y values



    The output:



    0 1 4
    0 2 5
    0 3 7
    0 4 10
    1 1 4.5
    1 2 5.5
    1 3 8
    1 4 12
    2 1 5
    2 2 6
    2 3 9
    2 4 14
    3 1 5.5
    3 2 6.5
    3 3 10
    3 4 16
    ...






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 31 at 9:53

























    answered Jan 30 at 17:08









    RomanPerekhrest

    22.4k12144




    22.4k12144











    • A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
      – L. Marsden
      Jan 31 at 9:23











    • If you could update the answer how you recommend that would be very useful.
      – L. Marsden
      Jan 31 at 9:30










    • @L.Marsden, see my update
      – RomanPerekhrest
      Jan 31 at 9:53










    • I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
      – L. Marsden
      Jan 31 at 11:16











    • @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
      – RomanPerekhrest
      Jan 31 at 11:47
















    • A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
      – L. Marsden
      Jan 31 at 9:23











    • If you could update the answer how you recommend that would be very useful.
      – L. Marsden
      Jan 31 at 9:30










    • @L.Marsden, see my update
      – RomanPerekhrest
      Jan 31 at 9:53










    • I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
      – L. Marsden
      Jan 31 at 11:16











    • @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
      – RomanPerekhrest
      Jan 31 at 11:47















    A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
    – L. Marsden
    Jan 31 at 9:23





    A good solution, although I did mention that there are no headers in the actual file, and this answer assumes that there are. Perhaps I didn't make this clear enough, since every other answer assumed the same. Nor should there be headers in the output.
    – L. Marsden
    Jan 31 at 9:23













    If you could update the answer how you recommend that would be very useful.
    – L. Marsden
    Jan 31 at 9:30




    If you could update the answer how you recommend that would be very useful.
    – L. Marsden
    Jan 31 at 9:30












    @L.Marsden, see my update
    – RomanPerekhrest
    Jan 31 at 9:53




    @L.Marsden, see my update
    – RomanPerekhrest
    Jan 31 at 9:53












    I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
    – L. Marsden
    Jan 31 at 11:16





    I receive an error when I try to run this solution. Any idea what the problem is? I have tried to run this code on my actual dataset and also my example dataset. awk: cmd. line:3: t[i-1][k]=$i awk: cmd. line:3: ^ syntax error awk: cmd. line:7: for (j in t[i]) awk: cmd. line:7: ^ syntax error awk: cmd. line:8: print i-1, x[j], t[i][j] awk: cmd. line:8: ^ syntax error
    – L. Marsden
    Jan 31 at 11:16













    @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
    – RomanPerekhrest
    Jan 31 at 11:47




    @L.Marsden, its yours. The solution works fine and here's a screenshot ibb.co/fQZQeR. That's all
    – RomanPerekhrest
    Jan 31 at 11:47












    up vote
    0
    down vote













    If you don't mind looping over the input n times:



    n=4 ### your N here
    for((t=0; t <= n)); t++))
    do
    awk -F$'t' -v t=$t 'print t, $1, $(t+2)' < input
    done > output





    share|improve this answer
























      up vote
      0
      down vote













      If you don't mind looping over the input n times:



      n=4 ### your N here
      for((t=0; t <= n)); t++))
      do
      awk -F$'t' -v t=$t 'print t, $1, $(t+2)' < input
      done > output





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        If you don't mind looping over the input n times:



        n=4 ### your N here
        for((t=0; t <= n)); t++))
        do
        awk -F$'t' -v t=$t 'print t, $1, $(t+2)' < input
        done > output





        share|improve this answer












        If you don't mind looping over the input n times:



        n=4 ### your N here
        for((t=0; t <= n)); t++))
        do
        awk -F$'t' -v t=$t 'print t, $1, $(t+2)' < input
        done > output






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 30 at 17:13









        Jeff Schaller

        31.4k846105




        31.4k846105




















            up vote
            0
            down vote













            Alternative GNU datamash + awk solution:



            datamash -W transpose <filename 
            | awk 'NR==1 for(i=1; i<=NF; i++) x[i]=$i
            NR > 1
            for (i=1; i<=NF; i++) print NR-2, x[i], $i
            '


            The output:



            0 1 4
            0 2 5
            0 3 7
            0 4 10
            1 1 4.5
            1 2 5.5
            1 3 8
            1 4 12
            2 1 5
            2 2 6
            2 3 9
            2 4 14
            3 1 5.5
            3 2 6.5
            3 3 10
            3 4 16





            share|improve this answer






















            • @L.Marsden, the answer is updated to omit header line/column
              – RomanPerekhrest
              Jan 31 at 10:00














            up vote
            0
            down vote













            Alternative GNU datamash + awk solution:



            datamash -W transpose <filename 
            | awk 'NR==1 for(i=1; i<=NF; i++) x[i]=$i
            NR > 1
            for (i=1; i<=NF; i++) print NR-2, x[i], $i
            '


            The output:



            0 1 4
            0 2 5
            0 3 7
            0 4 10
            1 1 4.5
            1 2 5.5
            1 3 8
            1 4 12
            2 1 5
            2 2 6
            2 3 9
            2 4 14
            3 1 5.5
            3 2 6.5
            3 3 10
            3 4 16





            share|improve this answer






















            • @L.Marsden, the answer is updated to omit header line/column
              – RomanPerekhrest
              Jan 31 at 10:00












            up vote
            0
            down vote










            up vote
            0
            down vote









            Alternative GNU datamash + awk solution:



            datamash -W transpose <filename 
            | awk 'NR==1 for(i=1; i<=NF; i++) x[i]=$i
            NR > 1
            for (i=1; i<=NF; i++) print NR-2, x[i], $i
            '


            The output:



            0 1 4
            0 2 5
            0 3 7
            0 4 10
            1 1 4.5
            1 2 5.5
            1 3 8
            1 4 12
            2 1 5
            2 2 6
            2 3 9
            2 4 14
            3 1 5.5
            3 2 6.5
            3 3 10
            3 4 16





            share|improve this answer














            Alternative GNU datamash + awk solution:



            datamash -W transpose <filename 
            | awk 'NR==1 for(i=1; i<=NF; i++) x[i]=$i
            NR > 1
            for (i=1; i<=NF; i++) print NR-2, x[i], $i
            '


            The output:



            0 1 4
            0 2 5
            0 3 7
            0 4 10
            1 1 4.5
            1 2 5.5
            1 3 8
            1 4 12
            2 1 5
            2 2 6
            2 3 9
            2 4 14
            3 1 5.5
            3 2 6.5
            3 3 10
            3 4 16






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 31 at 9:59

























            answered Jan 30 at 17:51









            RomanPerekhrest

            22.4k12144




            22.4k12144











            • @L.Marsden, the answer is updated to omit header line/column
              – RomanPerekhrest
              Jan 31 at 10:00
















            • @L.Marsden, the answer is updated to omit header line/column
              – RomanPerekhrest
              Jan 31 at 10:00















            @L.Marsden, the answer is updated to omit header line/column
            – RomanPerekhrest
            Jan 31 at 10:00




            @L.Marsden, the answer is updated to omit header line/column
            – RomanPerekhrest
            Jan 31 at 10:00












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420713%2fappend-n-columns-together%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay