From 77ec331c7dacad6b5028783288eb705bdb9ad22e Mon Sep 17 00:00:00 2001 From: equa Date: Thu, 27 Jul 2023 16:08:28 -0400 Subject: initial commit --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 README.md (limited to 'README.md') diff --git a/README.md b/README.md new file mode 100644 index 0000000..806cb1d --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# csv.sh + +parse CSV files with pure POSIX shell! + +## the short way + +(see `csv-min.sh`) + +to convert from csv to tab-separated strings: + +``` +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' +``` + +tabs, newlines, and backslashes are escaped into `\t`, `\n`, and `\\`, respectively. + +to convert back 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;' +``` + +(this program doesn't output CR LFs, but you can modify it to! `sed 's/$/'"$(printf "\r")"'/'`) + +## what? + +see `csv.sh`. + +## disclaimer + +you shouldn't trust yourself to verify a CSV parser, let alone trust me to write one! + +CSV is an amalgamation of formats, loosely described by [RFC 4180](https://www.rfc-editor.org/rfc/rfc4180). i try to be slightly more lenient than RFC 4180, and i tried my parser on output from a variety of programs, but i don't guarantee correctness for weird files. + +## license + +made by [Natalia Posting](https://equa.space/) in 2023. + +Permission to use, copy, modify, and/or distribute this software for +any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL +WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE +FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY +DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN +AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- cgit 1.3.0-6-gf8a5