Skip to content

Instantly share code, notes, and snippets.

@aduzsardi
Last active October 9, 2017 07:16
Show Gist options
  • Save aduzsardi/5380da3b6599ddd0bfde7f1feb5d3074 to your computer and use it in GitHub Desktop.
Save aduzsardi/5380da3b6599ddd0bfde7f1feb5d3074 to your computer and use it in GitHub Desktop.
bash tab separated values to comma separated values
#!/bin/bash
# Alex Duzsardi @aduzsardi
# made it more portable by using 'tr' instead of 'dos2unix'
if [[ $# -ne 1 ]]; then
echo "Usage: ./$0 tab-delimited-file.txt"
else
tr -d '\r' < "$1" | sed -r -e 's/\t/","/g' -e 's/^/"/g' -e 's/\s+(")/\1/g' -e 's/(\s+)?$/"/g' > "$1".csv
fi
@aduzsardi
Copy link
Author

another way ...the 'tr' part converts CRLF to LF , 'sed' part removes extra spaces from between the fields if you have for example

Field1\s\s\s\tField2\s\tField 3\tField 4

tr -d '\r' < tsv-file.txt | awk -F'\t' '{print "\""$1"\",\""$2"\",\""$3"\""}' | sed -r -e 's/\s+(","|")/\1/g'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment