From 77ec331c7dacad6b5028783288eb705bdb9ad22e Mon Sep 17 00:00:00 2001 From: equa Date: Thu, 27 Jul 2023 16:08:28 -0400 Subject: initial commit --- csv-min.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 csv-min.sh (limited to 'csv-min.sh') 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;' +} -- cgit 1.3.0-6-gf8a5