-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransptxt.sh
executable file
·61 lines (47 loc) · 1.13 KB
/
transptxt.sh
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
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# Transpose table in textfile
# Written by Andreas Heckel
# University of Heidelberg
# heckelandreas@googlemail.com
# https://github.com/ahheckel
# 25/03/2014
trap 'echo "$0 : An ERROR has occured."' ERR
set -e
function testascii()
{
local file="$1"
if LC_ALL=C grep -q '[^[:print:][:space:]]' $file; then
echo "0"
else
echo "1"
fi
}
Usage() {
echo ""
echo "Usage: `basename $0` <input-txt> <output-txt>"
echo ""
exit 1
}
[ "$2" = "" ] && Usage
data="$1"
out="$2"
# check
if [ $(testascii $data) -eq 0 ] ; then
echo "`basename $0`: ERROR : cannot read inputfile '$data' - exiting." ; exit 1
fi
# count number of columns
n_cols=$(awk '{print NF}' $data | sort -nu | head -n 1)
# count number of rows
n_rows=$(cat $data | wc -l)
#echo -n "`basename $0`: $n_rows rows , $n_cols columns --> "
# extract column
rm -f $out
for i in `seq 1 $n_cols` ; do
v=$(cat $data | awk -v c=${i} '{print $c}')
echo $v >> $out
done
# count number of columns
n_cols=$(awk '{print NF}' $out | sort -nu | head -n 1)
# count number of rows
n_rows=$(cat $out | wc -l)
#echo "$n_rows rows , $n_cols columns."