Removing ^M characters from a file

Hi,

I want to removing ^M characters from a file and combine the line with the next line.

ex:

issue i have:
ABC^M^M
DEF

solution i need:
ABCDEF

I found that you by using the following command you can remove new line characters.

tr -d '\r' < infile.csv > outfile.csv

still it wont combine the two lines.

please help

perl -i -ne 's/^M//g; print;' filename

^M can be typed as Ctrl-V and Ctrl-M

dos2unix < inputfile | gawk -v ORS=" " '1' > output_file

:D:D:D:D

tried dos2unix < inputfile | gawk -v ORS=" " '1' > output_file
but the output im getting is

ABC^M DEF

---------- Post updated at 05:44 AM ---------- Previous update was at 05:43 AM ----------

Please note that there can be one or more new line characters

---------- Post updated at 06:15 AM ---------- Previous update was at 05:44 AM ----------

And its also removing all unix terminations also. not acceptable

---------- Post updated at 06:51 AM ---------- Previous update was at 06:15 AM ----------

Wish scottn was around :confused:

what is the output dos2unix < input_file ??

dos2unix file file

Try this:

tr -d '\r\n' < infile.csv > outfile.csv 

dos2inix removes windows newline characters. but still 1 character per run. even then the second line is not merged with the first.

tr -d '\r\n' < infile.csv > outfile.csv removes all terminations so it becomes a single line. (there are many records in the file and only some are having this issue)

It should be helpful if you provide a better axample of your input file and the desired output.
My solution works with your example:

ssue i have:
ABC^M^M
DEF

solution i need:
ABCDEF

Given a file like this:

$
$ cat -v f8
ABC^M^M^M
DEF
GHI^M
JKL
MNO^M^M
PQR
STU^M^M^M^M^M^M^M
VWX^M^M^M^M
YZ
$

I shall assume that you want to remove to:
(a) remove "^M"s
(b) join the lines
(c) and retain Unix end-of-line characters

So, if you expect the final output to look like this:

ABCDEF
GHIJKL
MNOPQR
STUVWXYZ

then, you can use Perl thusly:

$
$ perl -pi -e 's/\r+\n//g' f8
$
$ cat -v f8
ABCDEF
GHIJKL
MNOPQR
STUVWXYZ
$
$

tyler_durden

1 Like

You are correct durde. thats how my file looks. in your code how can i specify the input file and output file?

---------- Post updated at 09:50 AM ---------- Previous update was at 09:31 AM ----------

Thanks alot durden. It worked :D:D:D:D:D:D

1 Like