Sunday, August 01, 2010

» Shell trick: CRLF to LF

I do this again and again in RPM .spec files, so just in case: a little trick to remove the CR from CRLF ;) (Windows uses two bytes for end-of-line markers, namely CR and LF, while Linux and Unix only use one: LF) You might of course use dos2unix or recode, but this one works in-place, which means that you don't need to make a copy, work on the copy and then copy (heh) that file back onto the original -- here is how to do it with dos2unix:
cp file file.orig && dos2unix file
And here the shorter version, using sed and its in-place editing feature:
sed -i 's/\r$//' file
Might as well do it with Perl, which also supports in-place editing:
perl -pi 's/\r$//' file
Update: as _Marcus_ just told me, recode actually does in-place editing by default, my bad :)

Labels: , , , ,

4 Comments:

Blogger DimiG said...

Thank you for your tips!

14:02  
Blogger Sagi. said...

btw: dos2unix works in-place as well.

06:43  
Blogger Dave said...

I often need to identify which files in a directory contain CRLFs that need changing to LF. I use the file command to do this:

$ file *.cxx
one.cxx: ASCII C program text
two.cxx: ASCII C program text, with CRLF line terminators

13:45  
Blogger Peter C. Ndikuwera said...

dos2unix also does inplace editing.

15:02  

Post a Comment

<< Home