LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Avoid carriage return until ^M is found (CentOS 6, bash 4.1) (https://www.linuxquestions.org/questions/linux-newbie-8/avoid-carriage-return-until-%5Em-is-found-centos-6-bash-4-1-a-4175482130/)

gacanepa 10-25-2013 08:10 AM

Avoid carriage return until ^M is found (CentOS 6, bash 4.1)
 
Hi everyone,
I have the following contents in a text file (as seen when viewed using vim):
Code:

one two three ^M
four five six ^M
seven
eight
nine ^M
ten eleven twelve ^M

(That is just a small portion of the file)
How can I obtain the following result?
Code:

one two three ^M
four five six ^M
seven eight nine ^M
ten eleven twelve ^M

If there are words in a certain line that does not end with ^M, they should be all in one line until a ^M appears.
Thanks in advance.

druuna 10-25-2013 11:05 AM

Give this a try:
Code:

sed -i ':loop;/^M/!{N;bloop};s/\n/ /g' infile
The ^M is special and can be created like this: type ctrl-v then ctrl-m

As a side effect the ^M's are not longer visible when opened in vi. This because the file is now a dos file and not a mixed dos/linux file.

Below a dump of the input file.

Before:
Code:

$ od -c infile
0000000  o  n  e      t  w  o      t  h  r  e  e      \r  \n
0000020  f  o  u  r      f  i  v  e      s  i  x      \r  \n
0000040  s  e  v  e  n  \n  e  i  g  h  t  \n  n  i  n  e
0000060      \r  \n  t  e  n      e  l  e  v  e  n      t  w
0000100  e  l  v  e      \r  \n
0000107

After:
Code:

0000000  o  n  e      t  w  o      t  h  r  e  e      \r  \n
0000020  f  o  u  r      f  i  v  e      s  i  x      \r  \n
0000040  s  e  v  e  n      e  i  g  h  t      n  i  n  e
0000060      \r  \n  t  e  n      e  l  e  v  e  n      t  w
0000100  e  l  v  e      \r  \n
0000107

The \r \n sequence represents the dos newline (the ^M). A single \n is a linux newline.

grail 10-25-2013 12:34 PM

You could also try awk:
Code:

awk -F"[ \n]" 'BEGIN{ORS=RS="\r\n"}$1=$1' file


All times are GMT -5. The time now is 05:41 PM.