summaryrefslogtreecommitdiff
path: root/csvtommd.rb
diff options
context:
space:
mode:
Diffstat (limited to 'csvtommd.rb')
-rwxr-xr-xcsvtommd.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/csvtommd.rb b/csvtommd.rb
new file mode 100755
index 0000000..355b0f9
--- /dev/null
+++ b/csvtommd.rb
@@ -0,0 +1,18 @@
+#!/usr/bin/ruby
+
+input = STDIN.read
+
+# find quoted cells and replace commas inside quotes with placeholder
+input.gsub!(/"([^,].*?)"/m) { |quoted|
+ quoted.gsub(/[\n\r]*/,'').gsub(/,/,'zXzX')
+}
+# replace remaining commas with table divider (pipe)
+input.gsub!(/,/,"| ")
+# remove quotes from quoted cells
+input.gsub!(/(\| |^)"(.*?)"/,"\\1\\2")
+# replace placeholders with commas
+input.gsub!(/zXzX/,",")
+
+input.each { |l|
+ puts "| #{l.strip} |"
+}