LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2013, 08:10 AM   #1
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 205

Rep: Reputation: 27
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.
 
Old 10-25-2013, 11:05 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 10-25-2013, 12:34 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You could also try awk:
Code:
awk -F"[ \n]" 'BEGIN{ORS=RS="\r\n"}$1=$1' file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script read command issue with carriage return itamarm Linux - General 3 06-17-2013 07:37 AM
[SOLVED] Unwanted Carriage Return hossf Programming 8 11-29-2011 08:09 AM
carriage return mike2008 Linux - Newbie 3 10-15-2009 12:03 AM
carriage return in emac lisp balloon Programming 1 11-23-2004 08:29 AM
pppd...carriage return?!? icyfire Linux - Software 1 02-14-2002 07:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration