Sed -e 's / ^ M / d' does not work

A very common problem, but I can not get around it with sed.

I have a script file (command package), say myfile.txt , which will be executed immediately to create a list. Now that I am performing a batch operation, my command line interface clearly shows that it cannot parse the command as a linear feed. ^M added at the end of each line.

I thought sed was the best way to do this. I tried:

  sed -e 's/^M/d' myfile.txt > myfile1.txt mv myfile1.txt myfile.txt 

This did not work. I also tried this and it did not work:

  sed -e 's/^M//g' myfile.txt > myfile1.txt mv myfile1.txt myfile.txt 

Then I thought sed takes it as an M character at the beginning of a line and therefore no result. So I tried:

  sed -e 's/\^M//g' myfile.txt > myfile1.txt mv myfile1.txt myfile.txt 

But no change. Is there a major mistake that I am making? Please advise, as I'm bad at sed .

I found the permission that was supposed to open the file in vi editor and in command mode execute this:

  :set fileformat=unix :w 

But I want him in sed .

+4
source share
3 answers

^M not literally ^M Replace ^M with \r . You can use the same representation for tr ; these two commands remove the carriage return:

 tr -d '\r' < input.txt > output.txt sed -e 's/\r//g' input.txt > output.txt 
+9
source
 sed -e 's/^M/d' myfile.txt 

It has the following meaning [the same for / \ ^ M /]: if the first letter of the string is M, then delete the string, otherwise print it and go to the next .. And you need to insert 2 delimiters / old / new / in s [command earch].

It can help you.

+1
source

Late, but here for posterity: sed Delete / Delete ^ M Carriage Return (Line Feed / CRLF) on Linux or Unix

The bottom line that answers the above question: get ^ M type CTRL + V, followed by CTRL + M ie, just don’t enter the carat symbol and capital M. This will not work

+1
source

Source: https://habr.com/ru/post/1447910/


All Articles