1

I have some files with this structure:

2015-03-25 17:08:17
sysUpTimeInstance 93474;^M
1.ValueforState=2500

I want to replace the line break, and leave the third line with the second line, I mean, the output would be like this:

2015-03-25 17:08:17
sysUpTimeInstance 93474;1.ValueforState=2500

I tried with sed:

sed 's/^M$//' myfile.dat > mynewfile.dat

But it only removed the symbol ^M

Any suggestions?

3

2 thoughts:

  1. with sed, for any line that ends with a carriage return, join the next line

    sed '/\r$/ {N; s/\r\n//} ' file
    
  2. with awk, define the record separator for input and output:

    awk -v RS='\r\n' -v ORS='' 1 file
    
1

Assuming that what shows up as ^M in your post really is a carriage return character (\r), the following should do the job:

perl -pe 's/\r\n//g'

It will work no matter how many lines your input contains: all lines ending with \r\n will be joined with the line that follows.

1

A couple of things could be happening.

If the ^M is not actually the 2 charecters ^ and M, but is the way some editors can represent the Carriage Return (CR) character. eg. My Emacs editor shows it as such. This character is part of the end-of-line character pair in Windows file system: a Carriage Return (hex value 0x0D) + a New Line (hex value 0x0A). The New Line character is also known as a Line Feed. The Unix file system uses only the New Line character to end a line. To remove an unwanted Carriage Return at the end of a Unix-style line, using sed you can use the following regular expression: Note \x0D and \r both work in sed. Note: \r is a shorthand way to represent 0x0D.

sed '/\r$/{N; s/\r\n//}'  

If ^M is actually the two characters ^ and M, which can happen sometimes when a Windows file in incorrectly converted into a Unix-style file, then you must handle the ^ as a special regular-expression character; you must escape it with a backslash \: Use this sed command:

sed '/\^M$/{N; s/\^M\n//}'
0

^M looks for me like its a DOS file

If ^M is really ASCII 13 (0x09) and not ASCII 94+77 (0x5E+0x4D) try to use:

sed ':a;N;$!ba;s/^M\n//g' myfile.dat > mynewfile.dat
0

If it's really just three lines, and you always want to join the second and third line, you can use this:

sed -e '2N' -e 's/\r\n//'

the N command will add the next line (i.e. the third) to the second, and then the replacing will remove the linebreak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.