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: , , , ,