-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollapseCSV
More file actions
executable file
·47 lines (40 loc) · 1.26 KB
/
collapseCSV
File metadata and controls
executable file
·47 lines (40 loc) · 1.26 KB
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
#!/bin/bash
#
# Tommaso Leonardi, 2011
# This program reads a 2 fields tab separated files and using
# the first field as index it collapses the file putting the
# second field values all on the same line separated by commas.
# If the -s option is provided it sums up all the second field
# values having the same index. The input file must be sorted by the index.
#
#
#
#
INPUT="NONE"
while getopts "sh" opt
do
case $opt in
(s) SUM=1;;
(h) HELP=1;;
esac
done
if [ $HELP ] ; then
echo -e "This program reads a 2 fields tab separated files and using the first field as index it collapses the file putting the second field values all on the same line separated by commas.\n\n If the -s option is provided it sums up all the second field values having the same index. The input file must be sorted by the index."
exit
fi
shift $(($OPTIND - 1))
INPUT=$1
if [ "$INPUT" = "" ] ; then
TMP1=`mktemp`
while read line
do
echo "$line" >> ${TMP1}
done
INPUT="$TMP1"
fi
if [ $SUM ] ; then
cat $INPUT|awk 'BEGIN{OFS="\t"}{if($1==ID){VAL+=$2} else{if(ID!=""){print ID,VAL;}ID=$1;VAL=$2;} }END{print ID,VAL}'
else
cat $INPUT|awk 'BEGIN{OFS="\t"}{if($1==ID){VAL=VAL","$2} else{if(ID!=""){print ID,VAL;}ID=$1;VAL=$2;} }END{print ID,VAL}'
fi
rm -f ${TMP1}