-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspose
More file actions
executable file
·49 lines (45 loc) · 878 Bytes
/
transpose
File metadata and controls
executable file
·49 lines (45 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#/bin/bash
# Transpose shell output (delimited by spaces)
# source: http://stackoverflow.com/a/1729980/46871
#
# Example:
# $ cat file.txt
# foo bar baz
# 31 12 AOK
# 33 15 BOH
# 29 14 BOH
# $ transpose file.txt
# foo 31 33 29
# bar 12 15 14
# baz AOK BOH BOH
#
filename=$1
if [[ $filename == "" ]]; then
echo "Usage: "
echo " $ cat file.txt"
echo " foo bar baz"
echo " 31 12 AOK"
echo " 33 15 BOH"
echo " 29 14 BOH"
echo " $ transpose file.txt"
echo " foo 31 33 29"
echo " bar 12 15 14"
echo " baz AOK BOH BOH"
exit 1
fi
awk '
{
for (i=1; i<=NF; i++) {
a[NR,i] = $i
}
}
NF>p { p = NF }
END {
for(j=1; j<=p; j++) {
str=a[1,j]
for(i=2; i<=NR; i++){
str=str" "a[i,j];
}
print str
}
}' $filename