Append n columns together
Clash 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.
awk cut
add a comment |Â
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.
awk cut
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
add a comment |Â
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.
awk cut
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.
awk cut
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
add a comment |Â
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
add a comment |Â
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 functiony
axis value (NR
- record number)x[k]=$1
- capture value for axisx
for (i=2; i<=NF; i++)
- iterating though fields starting from the 2ndt[i-1][k]=$i
- fill up the time axis arrayt
with functiony
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
...
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
 |Â
show 3 more comments
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
add a comment |Â
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
@L.Marsden, the answer is updated to omit header line/column
â RomanPerekhrest
Jan 31 at 10:00
add a comment |Â
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 functiony
axis value (NR
- record number)x[k]=$1
- capture value for axisx
for (i=2; i<=NF; i++)
- iterating though fields starting from the 2ndt[i-1][k]=$i
- fill up the time axis arrayt
with functiony
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
...
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
 |Â
show 3 more comments
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 functiony
axis value (NR
- record number)x[k]=$1
- capture value for axisx
for (i=2; i<=NF; i++)
- iterating though fields starting from the 2ndt[i-1][k]=$i
- fill up the time axis arrayt
with functiony
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
...
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
 |Â
show 3 more comments
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 functiony
axis value (NR
- record number)x[k]=$1
- capture value for axisx
for (i=2; i<=NF; i++)
- iterating though fields starting from the 2ndt[i-1][k]=$i
- fill up the time axis arrayt
with functiony
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
...
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 functiony
axis value (NR
- record number)x[k]=$1
- capture value for axisx
for (i=2; i<=NF; i++)
- iterating though fields starting from the 2ndt[i-1][k]=$i
- fill up the time axis arrayt
with functiony
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
...
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
 |Â
show 3 more comments
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
 |Â
show 3 more comments
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
add a comment |Â
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
add a comment |Â
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
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
answered Jan 30 at 17:13
Jeff Schaller
31.4k846105
31.4k846105
add a comment |Â
add a comment |Â
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
@L.Marsden, the answer is updated to omit header line/column
â RomanPerekhrest
Jan 31 at 10:00
add a comment |Â
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
@L.Marsden, the answer is updated to omit header line/column
â RomanPerekhrest
Jan 31 at 10:00
add a comment |Â
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
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
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
add a comment |Â
@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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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