-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdupline
More file actions
executable file
·57 lines (45 loc) · 1.38 KB
/
dupline
File metadata and controls
executable file
·57 lines (45 loc) · 1.38 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
47
48
49
50
51
52
53
54
55
56
57
# duplicates lines based on splitting on [,;]. Expects a filename and case insensitive column header as arguments
infile="${1}"
fileroot=$(echo "${infile}" | sed 's/\.....\?$//')
outfile="${fileroot}_fixed.tsv"
if [[ -z $2 ]];then echo "You must indicate a split column";exit;fi
split_column="${2//[^A-Za-z0-9]/}"
rm -f "${outfile}"
read -r -d '' awkscript << "ENDOFAWK"
#!/usr/bin/awk -f
BEGIN {
gsub(/[^a-z0-9]/, "", COL)
}
{
if (NR == 1) {
for(i=1;i<=NF;i++) {
compareheader=tolower($i)
gsub(/[^a-z0-9]/, "", compareheader)
if (compareheader == COL ) { dupcolumn = i }
}
print $0 > OUTFILE
} else {
recordsprocessed++
numdups = patsplit($dupcolumn,col_array,/[^,;]+/)
for (i=1;i<=numdups;i++) {
recordscreated++
for(j=1;j<=NF;j++) {
if (j == NF) {delim = RS} else {delim = OFS}
if (j != dupcolumn ) {
printf($j""delim) >> OUTFILE
} else {
# printf("%s%s",$j,j==NF?RS:OFS) >> OUTFILE
printf(col_array[i]""delim) >> OUTFILE
}
}
}
printf "Records processed: %d :created: %d \\r", recordsprocessed, recordscreated
}
}
END { print "" }
ENDOFAWK
echo -e "${awkscript}" > tmp_colsplit
chmod 700 tmp_colsplit
awk -v COL="${split_column}" -v OUTFILE="${outfile}" -v FS="\t" -v OFS="\t" -f tmp_colsplit "${infile}"
rm tmp_colsplit
#echo "File has been rebuilt with multivalues in ${split_column} as individual rows and sent to ${outfile}"