#! /usr/bin/env bash # Convert csv to simple csv, i.e., no commas (inside strings) that do not separate fields by changing some commas to dashes. # Actually default for field separator is comma but can be changed with second argument. # Usage: csv2scsv [filename] [separator] # If filename is omitted, input comes from standard input. if [[ $# -lt 2 ]]; then separator=',' else separator=$2 fi cat $1 | \ awk 'BEGIN {instring=0} {for (i=1;i<=length($0);i++) {nextchar=substr($0,i,1); if (nextchar=="\"") instring=1-instring; else if (nextchar=="'$separator'" && instring) printf "-"; else printf nextchar;}} {print ""}'