How to transpose x and y axis of table, using cut & paste shell commands
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I need to transpose x and y axis, of a file of 450.000 ÃÂ 15.000 tab separated fields, so I tried first with a small 5 ÃÂ 4 test file named A.txt:
x column1 column2 column3
row1 0 1 2
row2 3 4 5
row3 6 7 8
row4 9 10 11
I tried this:
for i in 1..4; do cut -f"$i" A.txt | paste -s; done > At.txt
but it does not work fine... Any help with this code?
The output is:
X row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3
2
5
8
11
cut paste
add a comment |Â
up vote
1
down vote
favorite
I need to transpose x and y axis, of a file of 450.000 ÃÂ 15.000 tab separated fields, so I tried first with a small 5 ÃÂ 4 test file named A.txt:
x column1 column2 column3
row1 0 1 2
row2 3 4 5
row3 6 7 8
row4 9 10 11
I tried this:
for i in 1..4; do cut -f"$i" A.txt | paste -s; done > At.txt
but it does not work fine... Any help with this code?
The output is:
X row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3
2
5
8
11
cut paste
Maybe helpful: unix.stackexchange.com/q/79642/117549
â Jeff Schaller
Aug 8 at 13:22
Thanks but I am worrking with Cygwin and commands like "transpose" do not work :-( I am not familiar with awk anyway...
â Jose
Aug 8 at 13:30
Can you show what you expected. (are you trying to transpose x and y axis?)
â ctrl-alt-delor
Aug 8 at 14:30
right...each line should be a column a 450K x 15K shall be relaced by an equivalent 15K x 450k
â Jose
Aug 8 at 14:33
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to transpose x and y axis, of a file of 450.000 ÃÂ 15.000 tab separated fields, so I tried first with a small 5 ÃÂ 4 test file named A.txt:
x column1 column2 column3
row1 0 1 2
row2 3 4 5
row3 6 7 8
row4 9 10 11
I tried this:
for i in 1..4; do cut -f"$i" A.txt | paste -s; done > At.txt
but it does not work fine... Any help with this code?
The output is:
X row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3
2
5
8
11
cut paste
I need to transpose x and y axis, of a file of 450.000 ÃÂ 15.000 tab separated fields, so I tried first with a small 5 ÃÂ 4 test file named A.txt:
x column1 column2 column3
row1 0 1 2
row2 3 4 5
row3 6 7 8
row4 9 10 11
I tried this:
for i in 1..4; do cut -f"$i" A.txt | paste -s; done > At.txt
but it does not work fine... Any help with this code?
The output is:
X row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3
2
5
8
11
cut paste
cut paste
edited Aug 8 at 15:05
ctrl-alt-delor
9,12931948
9,12931948
asked Aug 8 at 13:08
Jose
113
113
Maybe helpful: unix.stackexchange.com/q/79642/117549
â Jeff Schaller
Aug 8 at 13:22
Thanks but I am worrking with Cygwin and commands like "transpose" do not work :-( I am not familiar with awk anyway...
â Jose
Aug 8 at 13:30
Can you show what you expected. (are you trying to transpose x and y axis?)
â ctrl-alt-delor
Aug 8 at 14:30
right...each line should be a column a 450K x 15K shall be relaced by an equivalent 15K x 450k
â Jose
Aug 8 at 14:33
add a comment |Â
Maybe helpful: unix.stackexchange.com/q/79642/117549
â Jeff Schaller
Aug 8 at 13:22
Thanks but I am worrking with Cygwin and commands like "transpose" do not work :-( I am not familiar with awk anyway...
â Jose
Aug 8 at 13:30
Can you show what you expected. (are you trying to transpose x and y axis?)
â ctrl-alt-delor
Aug 8 at 14:30
right...each line should be a column a 450K x 15K shall be relaced by an equivalent 15K x 450k
â Jose
Aug 8 at 14:33
Maybe helpful: unix.stackexchange.com/q/79642/117549
â Jeff Schaller
Aug 8 at 13:22
Maybe helpful: unix.stackexchange.com/q/79642/117549
â Jeff Schaller
Aug 8 at 13:22
Thanks but I am worrking with Cygwin and commands like "transpose" do not work :-( I am not familiar with awk anyway...
â Jose
Aug 8 at 13:30
Thanks but I am worrking with Cygwin and commands like "transpose" do not work :-( I am not familiar with awk anyway...
â Jose
Aug 8 at 13:30
Can you show what you expected. (are you trying to transpose x and y axis?)
â ctrl-alt-delor
Aug 8 at 14:30
Can you show what you expected. (are you trying to transpose x and y axis?)
â ctrl-alt-delor
Aug 8 at 14:30
right...each line should be a column a 450K x 15K shall be relaced by an equivalent 15K x 450k
â Jose
Aug 8 at 14:33
right...each line should be a column a 450K x 15K shall be relaced by an equivalent 15K x 450k
â Jose
Aug 8 at 14:33
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
Your command works just fine assuming the input is a Unix text file with tab-delimited fields, and that GNU paste
is used. On non-GNU systems, you would have to use
$ for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3 2 5 8 11
Notice the -
argument to paste
which tells it to read standard input.
You most definitely do not want to run this on 450k columns though as that would require reading the file 450000 times. You'd be better off using some other solution for that.
See, for example, "Transposing rows and columns".
If the above command is run on a DOS text file, it would produce the following output in the terminal:
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
11
Redirecting the output to a new file and opening that file in the vim
editor would show
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3^M 2^M 5^M 8^M 11^M
where each ^M
is a carriage return character (the extra character at the end of a DOS text line). These carriage returns makes the cursor move back to the beginning of the line which is why the only thing that is visible on the last line in the terminal is a tab and 11
(which overwrites the other columns).
Make sure that your input file is a Unix text file by running dos2unix A.txt
.
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
 |Â
show 2 more comments
up vote
1
down vote
Cygwin is a Gnu environment. The problem is line endings, MS-Windows dose line endings different. Put the file through dos2unix, first (only do this to text files).
I have now reproduced. I pipe it into od -ta
, because my terminal renders it different to your dos cmd (cmd is changing a carrage return to a line feed).
#unix2dos A.txt
#for i in 1..4; do cut -f"$i" A.txt | paste -s; done | od -ta
0000000 x ht r o w 1 ht r o w 2 ht r o w 3
0000020 ht r o w 4 nl c o l u m n 1 ht 0 ht
0000040 3 ht 6 ht 9 nl c o l u m n 2 ht 1 ht
0000060 4 ht 7 ht 1 0 nl c o l u m n 3 cr ht
0000100 2 cr ht 5 cr ht 8 cr ht 1 1 cr nl
Explanation: cut
is seeing the carrage return as part of the last field. Newline is the record delimiter.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Your command works just fine assuming the input is a Unix text file with tab-delimited fields, and that GNU paste
is used. On non-GNU systems, you would have to use
$ for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3 2 5 8 11
Notice the -
argument to paste
which tells it to read standard input.
You most definitely do not want to run this on 450k columns though as that would require reading the file 450000 times. You'd be better off using some other solution for that.
See, for example, "Transposing rows and columns".
If the above command is run on a DOS text file, it would produce the following output in the terminal:
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
11
Redirecting the output to a new file and opening that file in the vim
editor would show
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3^M 2^M 5^M 8^M 11^M
where each ^M
is a carriage return character (the extra character at the end of a DOS text line). These carriage returns makes the cursor move back to the beginning of the line which is why the only thing that is visible on the last line in the terminal is a tab and 11
(which overwrites the other columns).
Make sure that your input file is a Unix text file by running dos2unix A.txt
.
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
 |Â
show 2 more comments
up vote
1
down vote
Your command works just fine assuming the input is a Unix text file with tab-delimited fields, and that GNU paste
is used. On non-GNU systems, you would have to use
$ for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3 2 5 8 11
Notice the -
argument to paste
which tells it to read standard input.
You most definitely do not want to run this on 450k columns though as that would require reading the file 450000 times. You'd be better off using some other solution for that.
See, for example, "Transposing rows and columns".
If the above command is run on a DOS text file, it would produce the following output in the terminal:
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
11
Redirecting the output to a new file and opening that file in the vim
editor would show
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3^M 2^M 5^M 8^M 11^M
where each ^M
is a carriage return character (the extra character at the end of a DOS text line). These carriage returns makes the cursor move back to the beginning of the line which is why the only thing that is visible on the last line in the terminal is a tab and 11
(which overwrites the other columns).
Make sure that your input file is a Unix text file by running dos2unix A.txt
.
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
 |Â
show 2 more comments
up vote
1
down vote
up vote
1
down vote
Your command works just fine assuming the input is a Unix text file with tab-delimited fields, and that GNU paste
is used. On non-GNU systems, you would have to use
$ for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3 2 5 8 11
Notice the -
argument to paste
which tells it to read standard input.
You most definitely do not want to run this on 450k columns though as that would require reading the file 450000 times. You'd be better off using some other solution for that.
See, for example, "Transposing rows and columns".
If the above command is run on a DOS text file, it would produce the following output in the terminal:
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
11
Redirecting the output to a new file and opening that file in the vim
editor would show
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3^M 2^M 5^M 8^M 11^M
where each ^M
is a carriage return character (the extra character at the end of a DOS text line). These carriage returns makes the cursor move back to the beginning of the line which is why the only thing that is visible on the last line in the terminal is a tab and 11
(which overwrites the other columns).
Make sure that your input file is a Unix text file by running dos2unix A.txt
.
Your command works just fine assuming the input is a Unix text file with tab-delimited fields, and that GNU paste
is used. On non-GNU systems, you would have to use
$ for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3 2 5 8 11
Notice the -
argument to paste
which tells it to read standard input.
You most definitely do not want to run this on 450k columns though as that would require reading the file 450000 times. You'd be better off using some other solution for that.
See, for example, "Transposing rows and columns".
If the above command is run on a DOS text file, it would produce the following output in the terminal:
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
11
Redirecting the output to a new file and opening that file in the vim
editor would show
x row1 row2 row3 row4
column1 0 3 6 9
column2 1 4 7 10
column3^M 2^M 5^M 8^M 11^M
where each ^M
is a carriage return character (the extra character at the end of a DOS text line). These carriage returns makes the cursor move back to the beginning of the line which is why the only thing that is visible on the last line in the terminal is a tab and 11
(which overwrites the other columns).
Make sure that your input file is a Unix text file by running dos2unix A.txt
.
edited Aug 8 at 14:46
answered Aug 8 at 13:27
Kusalananda
106k14209327
106k14209327
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
 |Â
show 2 more comments
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
I am working with Cygwin, so I presume it is a GNU environment... I typed for i in 1..4; do cut -f"$i" A.txt | paste -s - ; done > At.txt
â Jose
Aug 8 at 13:41
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
but it does not tranpose fine the file
â Jose
Aug 8 at 13:42
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
@Jose If you get some other output, then please post it in the text of your question. If the output that I get is wrong, then also post the expected output.
â Kusalananda
Aug 8 at 13:44
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
whem i open At.txt in notepad I see evething transposed but in a single line with no spaces nor enter between them ...
â Jose
Aug 8 at 14:06
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
@Jose Notepad is a Windows text editor that assumes DOS-formatted text files. You should not use Notepad to edit or view Unix text files.
â Kusalananda
Aug 8 at 14:07
 |Â
show 2 more comments
up vote
1
down vote
Cygwin is a Gnu environment. The problem is line endings, MS-Windows dose line endings different. Put the file through dos2unix, first (only do this to text files).
I have now reproduced. I pipe it into od -ta
, because my terminal renders it different to your dos cmd (cmd is changing a carrage return to a line feed).
#unix2dos A.txt
#for i in 1..4; do cut -f"$i" A.txt | paste -s; done | od -ta
0000000 x ht r o w 1 ht r o w 2 ht r o w 3
0000020 ht r o w 4 nl c o l u m n 1 ht 0 ht
0000040 3 ht 6 ht 9 nl c o l u m n 2 ht 1 ht
0000060 4 ht 7 ht 1 0 nl c o l u m n 3 cr ht
0000100 2 cr ht 5 cr ht 8 cr ht 1 1 cr nl
Explanation: cut
is seeing the carrage return as part of the last field. Newline is the record delimiter.
add a comment |Â
up vote
1
down vote
Cygwin is a Gnu environment. The problem is line endings, MS-Windows dose line endings different. Put the file through dos2unix, first (only do this to text files).
I have now reproduced. I pipe it into od -ta
, because my terminal renders it different to your dos cmd (cmd is changing a carrage return to a line feed).
#unix2dos A.txt
#for i in 1..4; do cut -f"$i" A.txt | paste -s; done | od -ta
0000000 x ht r o w 1 ht r o w 2 ht r o w 3
0000020 ht r o w 4 nl c o l u m n 1 ht 0 ht
0000040 3 ht 6 ht 9 nl c o l u m n 2 ht 1 ht
0000060 4 ht 7 ht 1 0 nl c o l u m n 3 cr ht
0000100 2 cr ht 5 cr ht 8 cr ht 1 1 cr nl
Explanation: cut
is seeing the carrage return as part of the last field. Newline is the record delimiter.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Cygwin is a Gnu environment. The problem is line endings, MS-Windows dose line endings different. Put the file through dos2unix, first (only do this to text files).
I have now reproduced. I pipe it into od -ta
, because my terminal renders it different to your dos cmd (cmd is changing a carrage return to a line feed).
#unix2dos A.txt
#for i in 1..4; do cut -f"$i" A.txt | paste -s; done | od -ta
0000000 x ht r o w 1 ht r o w 2 ht r o w 3
0000020 ht r o w 4 nl c o l u m n 1 ht 0 ht
0000040 3 ht 6 ht 9 nl c o l u m n 2 ht 1 ht
0000060 4 ht 7 ht 1 0 nl c o l u m n 3 cr ht
0000100 2 cr ht 5 cr ht 8 cr ht 1 1 cr nl
Explanation: cut
is seeing the carrage return as part of the last field. Newline is the record delimiter.
Cygwin is a Gnu environment. The problem is line endings, MS-Windows dose line endings different. Put the file through dos2unix, first (only do this to text files).
I have now reproduced. I pipe it into od -ta
, because my terminal renders it different to your dos cmd (cmd is changing a carrage return to a line feed).
#unix2dos A.txt
#for i in 1..4; do cut -f"$i" A.txt | paste -s; done | od -ta
0000000 x ht r o w 1 ht r o w 2 ht r o w 3
0000020 ht r o w 4 nl c o l u m n 1 ht 0 ht
0000040 3 ht 6 ht 9 nl c o l u m n 2 ht 1 ht
0000060 4 ht 7 ht 1 0 nl c o l u m n 3 cr ht
0000100 2 cr ht 5 cr ht 8 cr ht 1 1 cr nl
Explanation: cut
is seeing the carrage return as part of the last field. Newline is the record delimiter.
edited Aug 8 at 15:04
answered Aug 8 at 14:34
ctrl-alt-delor
9,12931948
9,12931948
add a comment |Â
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%2f461285%2fhow-to-transpose-x-and-y-axis-of-table-using-cut-paste-shell-commands%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
Maybe helpful: unix.stackexchange.com/q/79642/117549
â Jeff Schaller
Aug 8 at 13:22
Thanks but I am worrking with Cygwin and commands like "transpose" do not work :-( I am not familiar with awk anyway...
â Jose
Aug 8 at 13:30
Can you show what you expected. (are you trying to transpose x and y axis?)
â ctrl-alt-delor
Aug 8 at 14:30
right...each line should be a column a 450K x 15K shall be relaced by an equivalent 15K x 450k
â Jose
Aug 8 at 14:33