2

I can convert LF to CRLF using

sed "s/$/`echo -e \\\r`/"

I can also remove all line endings with

perl -pe 's/\r|\n//g'

I created a modified version which I thought would remove only CR from CRLF however it doesn't seem to work, any ideas?

perl -pe 's/\r//g'

Update: The perl script works just not with Apple Mac OS X TextMate editor as it does not display or read \r.

Jason
  • 607
  • 3
  • 9
  • 25
  • What do you mean by *“doesn't seem to work”*? That should do exactly as you ask. What platform are you working on? If you are on Windows then Perl will put back the CR on output, but it would also have been removed on input so there is nothing to remove within the program. – Borodin Mar 10 '15 at 12:32
  • I'm using a Mac editor called TextMate. I suspect that once I've converted the line endings when I save the file it is reverting them back. The strange thing is that it doesn't revert back when I convert the line endings from LF to CRLF. – Jason Mar 10 '15 at 12:47
  • I think I may have cracked it, TextMate does not display or read \r. I searched and replaced with perl -pe 's/\015/,/g' but no commas appeared. It looks like I'll have to make do with File > Save As - Line Endings: LF – Jason Mar 10 '15 at 13:15

2 Answers2

1

On a non-Windows box, the code you provided does work.

$ echo -ne "foo\r\nbar\r\n" | od -c
0000000   f   o   o  \r  \n   b   a   r  \r  \n
0000012

$ echo -ne "foo\r\nbar\r\n" | perl -pe's/\r//g' | od -c
0000000   f   o   o  \n   b   a   r  \n
0000010

On a Windows machine (which defaults to translating CRLF⇒LF on read and LF⇒CRLF on write), you can use

perl -pe"BEGIN { binmode STDOUT }"

There are portable solutions, but they are longer.

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

You can use cereja Python package

pip install cereja

import cereja cereja.lf_to_crlf(dir_or_file_path)

if is dir path will be recursive

or

cereja.lf_to_crlf(dir_or_file_path, ext_in=[“.py”,”.csv”])

You can substitute for any standard. See the filetools module

Joab Leite
  • 84
  • 3