Removing carriage return characters from file

Hello there,

I need to remove carriage return characters (\n and \r) from any input file specified. This is what I am doing right now:

  • dumping the file to octal format using the command 'od -c file_name
  • removing and \s and \n characters using sed commands

What I need to do now is convert the file_name from octal format back to a normal format. By normal I mean without octal numbers everywhere.

Please advise on how to undo a conversion done by using the 'od' command.

OR - if you have a better way of removing carriage return characters then please let me know.

Regards,

$vi nonewlines.c

#include <stdio.h> 
/*nonewlines.c - remove nl and cr */
main()
{
int ch;
      while((ch = getchar()) != EOF) if ((ch!='\n')&&(ch!='\r')) putchar(ch);
}

cc nonewlines.c -o nonewlines

./nonewlines < filetochange

Please use the search function before posting. We get this question several times a month. Here are a few threads...

there was a strange character(^M) been added automatically in UNIX

^m

getting rid of control characters

Porting of Windows written unix scripts to unix platform

Thanks for the input guys. I ended up using

cat filename | tr -d "\r \n" > filename2

Worked great.