replacing ^M characters with linefeed 
Author Message
 replacing ^M characters with linefeed

I'm trying to replace all occurences of the ^M character with line feeds
instead (the file was created on an MSDOS machine).  I tried:

sed 's/^M/\n/g' file.txt
sed 's/^M/\012/g' file.txt

(where ^M is typed as Control-V Control-M)

and a few variants, including similar stuff in the vim editor, but alas
nothing seems to work.  When I cat or edit the file, it looks like one
huge line with ^M's where the line feeds should go.

I'm probably missing something obvious...



Sun, 09 May 2004 08:10:56 GMT  
 replacing ^M characters with linefeed

Quote:

>I'm trying to replace all occurences of the ^M character with line feeds
>instead (the file was created on an MSDOS machine).  I tried:

>sed 's/^M/\n/g' file.txt
>sed 's/^M/\012/g' file.txt

>(where ^M is typed as Control-V Control-M)

>and a few variants, including similar stuff in the vim editor, but alas
>nothing seems to work.  When I cat or edit the file, it looks like one
>huge line with ^M's where the line feeds should go.

>I'm probably missing something obvious...

man tr

Also read the comp.unix.questions FAQ

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 09 May 2004 12:38:10 GMT  
 replacing ^M characters with linefeed
why dont you try with dos2ux and/or dos2ux ???



Quote:
> I'm trying to replace all occurences of the ^M character with line feeds
> instead (the file was created on an MSDOS machine).  I tried:

> sed 's/^M/\n/g' file.txt
> sed 's/^M/\012/g' file.txt

> (where ^M is typed as Control-V Control-M)

> and a few variants, including similar stuff in the vim editor, but alas
> nothing seems to work.  When I cat or edit the file, it looks like one
> huge line with ^M's where the line feeds should go.

> I'm probably missing something obvious...



Sun, 09 May 2004 23:37:22 GMT  
 replacing ^M characters with linefeed

Quote:

>I'm trying to replace all occurences of the ^M character with line feeds
>instead (the file was created on an MSDOS machine).  I tried:

>sed 's/^M/\n/g' file.txt
>sed 's/^M/\012/g' file.txt

You can't use \n in sed. You have to use \ and a real newline.
(see miscellaneous notes in manpage)

Quote:
>(where ^M is typed as Control-V Control-M)

>and a few variants, including similar stuff in the vim editor, but alas
>nothing seems to work.  When I cat or edit the file, it looks like one
>huge line with ^M's where the line feeds should go.

>I'm probably missing something obvious...

Yes. ^M is a representation of the return character, not the
newline, and you want to remove the return, not replace it. In
vim you can do:

    :help DOS

and you will find that you can set the fileformat with:
    :e file_in_dos_format
    :set fileformat=unix
    :w file_in_unix_format

in vi's without recoding:

    :%s/^M//g

the same works in sed:

    sed -ne 's/^M//p' file_in_dos_format > file_in_unix_format

Maybe you want to use teco (but the version I have gets confused
by removing the \r s):

    <s^M$-c$d$>     (this will stop with a search failure after EOF)

Ob-awk:

    awk '{gsub("^M","");print}' file_in_dos_format > file_in_unix_format

should do the trick.

        Heiko



Fri, 14 May 2004 06:00:03 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Using string map to replace Special Characters (MS Word)

2. Issues with Non-COBOL characters and REPLACE / REPLACING

3. string.replace() can't replace newline characters???

4. How do I parse with respect to a Linefeed character

5. Linefeed Character

6. '"""' and linefeed characters

7. Replacing 2 end characters

8. Replace a sequence of characters

9. How to replace characters in a text file

10. Replace character if not followed by?

11. Replace character by awk

12. replacing characters

 

 
Powered by phpBB® Forum Software