Search code examples
concatenationgnuplotmultiple-columns

Reorder/concatenate columns in gnuplot


How to rearrange multiple columns in gnuplot? The [name]_* columns should be concatenated.

The data table looks like this:

agfc_01 dfr_01  ssft_01 agfc_02 dfr_02  ssft_02
1514    1514    8045    1445    1445    9701
NaN NaN 10823   1555    1587    8625
1825    1826    9028    1517    1554    7385
1660    1663    10384   1684    1684    11819
1163    918 10179   1515    1515    8046
1619    1620    8769    NaN NaN 10824
1011    1139    8141    1826    1827    9029
NaN NaN 10272   1661    1664    10385
NaN NaN 8557    1164    919 10180
1402    1402    13443   1620    1621    8770
511 492 11012   1012    1140    8142
NaN NaN 10204   NaN NaN 10273
NaN NaN 8867    NaN NaN 8558
811 811 7805    1403    1403    13444
1376    1383    10813   2710    2711    9154

What I need is the following:

agfc    dfr ssft
1514    1514    8045
NaN NaN 10823
1825    1826    9028
1660    1663    10384
1163    918 10179
1619    1620    8769
1011    1139    8141
NaN NaN 10272
NaN NaN 8557
1402    1402    13443
511 492 11012
NaN NaN 10204
NaN NaN 8867
811 811 7805
1376    1383    10813
1445    1445    9701
1555    1587    8625
1517    1554    7385
1684    1684    11819
1515    1515    8046
NaN NaN 10824
1826    1827    9029
1661    1664    10385
1164    919 10180
1620    1621    8770
1012    1140    8142
NaN NaN 10273
NaN NaN 8558
1403    1403    13444
2710    2711    9154

Or can the plot command use multiple columns as a single input?


Solution

  • gnuplot isn't great at (actually isn't made for) reordering/sorting/reversing/concatenating data. But your task can easily be done with gnuplot only.

    Plot your file into a datablock (check help with table). From gnuplot 5.4.0 on, you can use the (experimental) feature if ... which allows to define a condition whether the values should be written or not. Here it is used to suppress the second header.

    Data: SO76199322.dat

    agfc_01 dfr_01  ssft_01 agfc_02 dfr_02  ssft_02
    1514    1514    8045    1445    1445    9701
    NaN NaN 10823   1555    1587    8625
    1825    1826    9028    1517    1554    7385
    1660    1663    10384   1684    1684    11819
    1163    918 10179   1515    1515    8046
    1619    1620    8769    NaN NaN 10824
    1011    1139    8141    1826    1827    9029
    NaN NaN 10272   1661    1664    10385
    NaN NaN 8557    1164    919 10180
    1402    1402    13443   1620    1621    8770
    511 492 11012   1012    1140    8142
    NaN NaN 10204   NaN NaN 10273
    NaN NaN 8867    NaN NaN 8558
    811 811 7805    1403    1403    13444
    1376    1383    10813   2710    2711    9154
    

    Script: (works for gnuplot>=5.4.0)

    ### concatenate other columns
    reset session
    
    FILE = "SO76199322.dat"
    
    set table $Data
        plot for [i=0:1] FILE u (strcol(i*3+1)):(strcol(i*3+2)):(strcol(i*3+3)) w table if ($0!=0 || i!=1)
    unset table
    
    print $Data
    ### end of script
    

    Result:

    agfc_01         dfr_01  ssft_01
    1514    1514    8045
    NaN     NaN     10823
    1825    1826    9028
    1660    1663    10384
    1163    918     10179
    1619    1620    8769
    1011    1139    8141
    NaN     NaN     10272
    NaN     NaN     8557
    1402    1402    13443
    511     492     11012
    NaN     NaN     10204
    NaN     NaN     8867
    811     811     7805
    1376    1383    10813
    1445    1445    9701
    1555    1587    8625
    1517    1554    7385
    1684    1684    11819
    1515    1515    8046
    NaN     NaN     10824
    1826    1827    9029
    1661    1664    10385
    1164    919     10180
    1620    1621    8770
    1012    1140    8142
    NaN     NaN     10273
    NaN     NaN     8558
    1403    1403    13444
    2710    2711    9154