-
-
Save arc279/195a8a9f1a261bf48ef7bc3ad3746de7 to your computer and use it in GitHub Desktop.
pivot と unpivot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function data() { | |
printf "%s\n" $(echo {1..10}{a..c}) | while read x; do | |
NUM=${x:0: -1} | |
CATE=${x: -1} | |
printf "%s\t%s\t%s\n" $NUM $CATE value_$NUM$CATE | |
done | |
} | |
function pivot() { | |
datamash crosstab 1,2 collapse 3 | |
} | |
function unpivot() { | |
read header | |
sort -k1 -n | while read x; do | |
{ | |
echo "$header" | |
echo "$x" | cut -f2- | |
} | datamash transpose | while read y; do | |
printf "%s\t%s\n" $(echo "$x" | cut -f1) "$y" | |
done | |
done | |
} | |
data | |
echo ----- | |
data | pivot | |
echo ----- | |
data | pivot | unpivot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 a value_1a | |
1 b value_1b | |
1 c value_1c | |
2 a value_2a | |
2 b value_2b | |
2 c value_2c | |
3 a value_3a | |
3 b value_3b | |
3 c value_3c | |
4 a value_4a | |
4 b value_4b | |
4 c value_4c | |
5 a value_5a | |
5 b value_5b | |
5 c value_5c | |
6 a value_6a | |
6 b value_6b | |
6 c value_6c | |
7 a value_7a | |
7 b value_7b | |
7 c value_7c | |
8 a value_8a | |
8 b value_8b | |
8 c value_8c | |
9 a value_9a | |
9 b value_9b | |
9 c value_9c | |
10 a value_10a | |
10 b value_10b | |
10 c value_10c | |
----- | |
a b c | |
1 value_1a value_1b value_1c | |
10 value_10a value_10b value_10c | |
2 value_2a value_2b value_2c | |
3 value_3a value_3b value_3c | |
4 value_4a value_4b value_4c | |
5 value_5a value_5b value_5c | |
6 value_6a value_6b value_6c | |
7 value_7a value_7b value_7c | |
8 value_8a value_8b value_8c | |
9 value_9a value_9b value_9c | |
----- | |
1 a value_1a | |
1 b value_1b | |
1 c value_1c | |
2 a value_2a | |
2 b value_2b | |
2 c value_2c | |
3 a value_3a | |
3 b value_3b | |
3 c value_3c | |
4 a value_4a | |
4 b value_4b | |
4 c value_4c | |
5 a value_5a | |
5 b value_5b | |
5 c value_5c | |
6 a value_6a | |
6 b value_6b | |
6 c value_6c | |
7 a value_7a | |
7 b value_7b | |
7 c value_7c | |
8 a value_8a | |
8 b value_8b | |
8 c value_8c | |
9 a value_9a | |
9 b value_9b | |
9 c value_9c | |
10 a value_10a | |
10 b value_10b | |
10 c value_10c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment