about summary refs log tree commit diff
path: root/csv-min.sh
diff options
context:
space:
mode:
authorequa <equaa@protonmail.com>2023-07-27 16:08:28 -0400
committerequa <equaa@protonmail.com>2023-07-27 16:16:37 -0400
commit77ec331c7dacad6b5028783288eb705bdb9ad22e (patch)
treeda634e1e17752a34e0898592f7735fb1679066e7 /csv-min.sh
initial commit
Diffstat (limited to 'csv-min.sh')
-rwxr-xr-xcsv-min.sh23
1 files changed, 23 insertions, 0 deletions
diff --git a/csv-min.sh b/csv-min.sh
new file mode 100755
index 0000000..3a12dcd
--- /dev/null
+++ b/csv-min.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+# Parsing CSV files with sed
+
+# we convert csv files into a simplified interchange format:
+# text characters separated by tabs! we escape tabs, newlines, and backslashes
+# into \t, \n, and \\.
+
+# these functions process on standard input and standard output.
+# we allow CR LFs in input but we don't output them by default.
+
+# see csv.sh for an unminified version.
+
+# made by Natalia Posting in 2023
+
+from_csv () {
+	LC_ALL=C sed -n 's/'"$(printf "\r")"'$//;s/\\/\\\\/g;s/'"$(printf "\t")"'/\\t/g;H;x;h;s/^\n//;s/\n/\\n/g;s/,/,,/g;s/$/,/;s/^/,/;s/,\([^",]*\("[^"]*\(""[^"]*\)*"[^",]*\)*\),/\1'"$(printf "\t")"'/g;/,$/d;s/.$//;s/,,/,/g;s/"\([^"]*\(""[^"]*\)*\)"/\1/g;s/""/"/g;p;s/.*//;h'
+}
+
+
+to_csv () {
+	LC_ALL=C sed 's/"/""/g;s/'"$(printf "\t")"'/","/g;s/^/"/;s/$/"/;s/\\\\/& /g;s/\\n/\n/g;s/\\t/'"$(printf "\t")"'/g;s/\\\\ /\\/g;'
+}