Removing ^M and the newline that follows it.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing ^M and the newline that follows it.
# 1  
Old 04-01-2011
Removing ^M and the newline that follows it.

Hi Gurus,

Apologies as I feel like this must be answered already on here somewhere but I just can't find it. I find many people looking to remove all \n and \r (CR and LF) or one or the other but the only times I've found someone trying to remove them only when both are together they've found workarounds instead eg: https://www.unix.com/unix-advanced-ex...ited-file.html

So my issue is: I have data like
Code:
a,b,c,d,e
a,b,c,d,e
a,b,c,^M
d,e
a,b,c,d,e

Removing the ^M alone I can do with
Code:
tr -d '\r'

But this still leaves the broken line

I tried
Code:
tr -d '\r\n'

but of course that moves ALL linefeeds, not just the ones after a ^M

I can find the lines with ease using grep and <ctrlv><ctrlm> and fix them manually but I need this in an automated script, as simple to understand as possible please as I'm obviously no unix wiz.

Your help is greatly appreciated, as always!

Lee
# 2  
Old 04-01-2011
Hi, Lee:

Perhaps the following will do the trick for you:
Code:
sed '/^M$/{N; s/.\n//;}'

That solution will discard a final line ending with \r\n since there's nothing to merge it with. If that's undesirable, see the next offering.

The following, when it encounters a final line which ends in \r\n, will strip the \r but leave the \n. Nothing follows the line so it cannot be merged with another, but the final result is a text file with unix line endings as per your example:
Code:
sed '/^M$/{s///; $p; N; s/\n//;}'

Note that everything said above only applies to a posix-compliant sed implementation. GNU sed by default chooses to ignore the standard and writes the pattern space to stdout when N executes and there's no further text. The following (I think .. untested) are the equivalents for the GNU sed commonly found on linux systems:

Code:
sed '/^M$/{$d; N; s/.\n//;}'

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

Regards,
Alister

P.S. In case the rationale for GNU sed's N behavior is of interest to anyone, it's discussed @ http://www.gnu.org/software/sed/manu...ting-Bugs.html (I found that myself when looking to report the "bug" Smilie)

Last edited by alister; 04-01-2011 at 08:16 PM..
These 2 Users Gave Thanks to alister For This Post:
# 3  
Old 04-01-2011
I had this issue with files that were ftp-ed from windows to unix .. using the dos2unix command cleans it up pretty well. Try it. I used it in a script to clean up data files before loading into the database and it worked too.

Code:
dos2unix <inputfile> <outputfile>

# 4  
Old 04-01-2011
mvijayv:

I think you misunderstood the problem. dos2unix, to my knowledge, only converts the line ending; it does not merge lines by deleting one type of line ending while leaving others intact.

Regards,
Alister
# 5  
Old 04-01-2011
Code:
echo "a,b,c,d,e
a,b,c,d,e
a,b,c,^M
d,e
a,b,c,d,e" |sed 'N;s/^M\n//'
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e

# 6  
Old 04-01-2011
Thanks Alister. Missed that piece .. Yup dos2unix doesn't merge the lines.
# 7  
Old 04-01-2011
Quote:
Originally Posted by yinyuemi
Code:
sed 'N;s/^M\n//'

That solution will not work for even numbered lines ending with \r\n. In those cases, there will be no \r\n in the pattern space. The pattern space will end with \r alone.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing newline characters within DEL quotes.

Hi, Text file has DEL character(ASCII code 127) as quotes with comma as field delimiter. If any of the field contains new line character then I need to remove it. Please help me to achieve this. Thanks Vikram (4 Replies)
Discussion started by: Vikramhm
4 Replies

2. UNIX for Dummies Questions & Answers

Removing PATTERN from txt without removing lines and general text formatting

Hi Everybody! First post! Totally noobie. I'm using the terminal to read a poorly formatted book. The text file contains, in the middle of paragraphs, hyphenation to split words that are supposed to be on multiple pages. It looks ve -- ry much like this. I was hoping to use grep -v " -- "... (5 Replies)
Discussion started by: AxeHandle
5 Replies

3. Shell Programming and Scripting

Script for removing newline character from file

Hi below is my file. cat input.dat 101,abhilash,1000 102,prave en,2000 103,partha,4 000 10 4,naresh,5000 (its just a example file) and my output should be: 101,abhilash,1000 102,praveen,2000 103,partha,4000 104,naresh,5000 below is my code cat input.dat |tr -d '\n' >... (6 Replies)
Discussion started by: abhilash_nakka
6 Replies

4. Shell Programming and Scripting

echo without newline

I am trying to make a download progress meter with bash and I need to echo a percentage without making a newline and without concatenating to the last output line. The output should replace the last output line in the terminal. This is something you see when wget or curl downloads files.... (6 Replies)
Discussion started by: locoroco
6 Replies

5. Shell Programming and Scripting

sed newline

Hi everyone, I'd like to use the script validatehtml which returns either the given url is HTML strict or not, using http:// validator . w3 . org . sh validatehtml #!/bin/bash wget -q http:// validator . w3 .org / check?uri=$1 cat check\?uri\=$1 | sed -n '/h2/ p' | sed 's/ */ /g' | sed... (2 Replies)
Discussion started by: azertyazerty
2 Replies

6. Shell Programming and Scripting

using awk removing newline and specific position

Hello Friends, Input File looks as follows: >FASTA Header1 line1 line2 line3 linen >FASTA Header2 Line1 Line2 linen >FASTA Header3 and so on ....... Output: Want something as: >FASTA Header1 line1line2line3linen >FASTA Header2 (5 Replies)
Discussion started by: Deep9000
5 Replies

7. UNIX for Dummies Questions & Answers

newline in echo

Hi all, I have a scripts in csh and whenI do echo command I want to add newline. I used to do it like this: #! /bin/csh echo "\n\n WHAT AREA DO YOU WANT:\n\n" echo -n " YOUR CHOISE : " set area=$< but since weupgrade the oracle to 10g it doesn't do the newline (he print the\n as a... (5 Replies)
Discussion started by: rikyer
5 Replies

8. Shell Programming and Scripting

Removing Embedded Newline from Delimited File

Hey there - a bit of background on what I'm trying to accomplish, first off. I am trying to load the data from a pipe delimited file into a database. The loading tool that I use cannot handle embedded newline characters within a field, so I need to scrub them out. Solutions that I have tried... (7 Replies)
Discussion started by: bbetteridge
7 Replies

9. Forum Support Area for Unregistered Users & Account Problems

newline

I have an old file originally created in vi but read and saved by a word processor at some point. I have ^Ms and know how to substitute them for anything I wish but I still only have one long line when viewed in vi. So I suppose I need to substitute a newline for each ^M but I don't know the... (2 Replies)
Discussion started by: Gale Gorman
2 Replies

10. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies
Login or Register to Ask a Question