4

I have a lot of files that end in the classical ^M, an artifact from my Windows times. As this is all source code, git actually thinks those files changed, so I want to remove those nasty lines once and for all.

Here is what I created:

sed -i 's/^M//g' file

But that does not work. Of course I did not type a literal ^M but rather ^V^M (ctrl V, ctrl M). In vim it works (:%s/s/^M//g) and if I modify it like this:

sed -i 's/^M/a/g' file

It also works, i.e. it ends every line with an 'a'. It also works to do this:

sed -i 's/random_string//g' file

Where random_string exists in the file. So I can replace ^M by any character and I can remove lines but I cannot remove ^M. Why?

Note: It is important that it is just removed, no replacing by another invisible char or something. I would also like to avoid double execution and adding an arbitrary string and removing it afterwards. I want to understand why this fails (but it does not report an error).

javex
  • 7,198
  • 7
  • 41
  • 60
  • 1
    Is the character at the end of the line? If so this might help: http://stackoverflow.com/questions/64749/m-character-at-end-of-lines – squiguy Aug 28 '12 at 20:41
  • 2
    Is there any particular reason you want to use `sed` for this? It seems easier to just run `dos2unix`. (That said -- your `sed -i 's/^M//g' file`, using `^V^M` to insert the `^M`, works fine for me.) – ruakh Aug 28 '12 at 20:42
  • Oh, I did not know about this tool. It may be easier, I will check it, thanks! – javex Aug 28 '12 at 20:47

2 Answers2

4

That character is matched with \r by sed. Use:

sed -e "s/\r//g" input-file
Birei
  • 35,723
  • 2
  • 77
  • 82
1

For my case, I had to do

sed -e "s/\r/\n/g" filename.csv

After that wc -l filename Showed correct output instead of 0 lines.

iqbalnaved
  • 85
  • 1
  • 8