-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat-to-tsv-stdin.sh
More file actions
executable file
·51 lines (44 loc) · 976 Bytes
/
flat-to-tsv-stdin.sh
File metadata and controls
executable file
·51 lines (44 loc) · 976 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
49
50
51
#!/bin/bash
set -e
if [ -n "$1" ]
then
echo "Usage: $(basename "$0") < input-flat.txt" >&2
fi
SILENT='true'
SED_EXT=-r
case $(uname) in
Darwin*)
SED_EXT=-E
esac
export SED_EXT
FIRST_RECORD=''
FIRST_RECORD_LINE='x'
while [ -n "${FIRST_RECORD_LINE}" ]
do
IFS='' read -r 'FIRST_RECORD_LINE'
FIRST_RECORD="${FIRST_RECORD}${FIRST_RECORD_LINE}
"
done
"${SILENT}" || echo "FIRST_RECORD=[[
${FIRST_RECORD}]]" >&2
TAB="$(echo -ne '\011')"
CR="$(echo -ne '\015')"
echo "${FIRST_RECORD}" \
| sed "${SED_EXT}" \
-e "s/${CR}$//" \
-e "s/^\$/${TAB}/" \
-e 's/^[^:]*:/"/' \
-e 's/[]]:.*$/"/' \
| tr '\011\012' '\012\011' \
| sed \
-e "s/^${TAB}//" \
-e '/^$/d'
( echo "${FIRST_RECORD}" ; cat ) \
| sed "${SED_EXT}" \
-e "s/${CR}$//" \
-e "s/^\$/${TAB}/" \
-e 's/^([^:]|:[^:])*:: //' \
| tr '\011\012' '\012\011' \
| sed \
-e "s/^${TAB}//" \
-e '/^$/d'