Last active
October 9, 2017 07:16
-
-
Save aduzsardi/5380da3b6599ddd0bfde7f1feb5d3074 to your computer and use it in GitHub Desktop.
bash tab separated values to comma separated values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another way ...the 'tr' part converts CRLF to LF , 'sed' part removes extra spaces from between the fields if you have for example
tr -d '\r' < tsv-file.txt | awk -F'\t' '{print "\""$1"\",\""$2"\",\""$3"\""}' | sed -r -e 's/\s+(","|")/\1/g'