majordomo
majordomo

Reputation: 1240

Convert csv to CRLF line endings in bash script

I have a csv that I'm converting to JSON using csv2json. I've encountered a problem after changing my script and discovered its because of my line endings.

When I try to convert my csv (original.csv) to json, I get an error because of the line endings. So I opened up the csv in Coda (the text editor I'm using), and chose "Convert to CRLF", saved the file, then re-ran csv2json. It worked.

My question is, how can I convert original.csv to CRLF in a bash script?

I tried using sed with no luck:

sed 's/$'"/`echo \\\r`/" original.csv > new.csv

Thank you

Upvotes: 2

Views: 2787

Answers (2)

majordomo
majordomo

Reputation: 1240

Ended up using perl:

perl -pe 's/\r\n|\n|\r/\r\n/g' original.csv > new.csv

Upvotes: 2

speakr
speakr

Reputation: 4209

No bash or sed but another quite simple solution:

On Debian and derivates like Ubuntu you can use the unix2dos command to easily convert LF to CRLF in a file:

unix2dos -n original.csv new.csv

You can install it with the dos2unix package:

apt-get install dos2unix

To again convert CRLF to LF use the command dos2unix.

Upvotes: 1

Related Questions