removing special characters @ EOL

How to remove special chracters @ END OF EACH LINE in a file

file1.txt:

0003073413^M
0003073351^M
0003073379^M
0003282724^M
0003323334^M
0003217159^M
0003102760^M
0002228911^M

I used the below command but it is not working ?


 perl -pi -e 's/^M\/g' file1.txt

Even i tried the below command but it is showing o/p in command prompt. I want the changes in the same file

sed 's/\^.[^ ]*//g' file1.txt 

Is it really a literal caret and a literal uppercase letter m? Or is it a file that was created on Windows, and the ^M is your editors representation of the carriage return '\r'?

If it's the latter, use either dos2unix (if available) OR open the file in an editor that understands both formats and save it in UNIX format again OR run it trough tr (or similar) to remove the \r at the end.

And it's no wonder your Perl one-liner didn't do anything, it won't even run that way.

run the command

dos2unix filename

this uld automatically convert the extra characters that are viewed

in some unix version the format is

dos2unix filename1 filename2

if both the filename1 and filename2 are same it uld remove the extra characters and write in the same file filename1 else it converts and writes to filename2

The u can redirect the output a other file then copy the file

With tr:

tr -d '\r' < file > newfile

With sed the ^M must be typed as <CTRL-v> <Enter>:

sed 's/^M//' file > newfile

i dnt want to redirect the o/p in a new file. I need to make changes in the same file ?

Use the -i option with sed if your version supports it, otherwise you don't have any choice.