replace \r\r\n with \r\n


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace \r\r\n with \r\n
# 1  
Old 03-14-2009
replace \r\r\n with \r\n

I am having a dificult time trying to replace 2 carriage returns and 1 newline with 1 carriage return and 1 newline in a file. The file may come as CR NL in which case I need to leave it as is. When it comes with CR CR NL then I need to remove one of the CR's.

i tried

sed 's/\r\r\n/\r\n\g' file

but it has no effect on the file

appreciate the help. (I did search and tried many of the examples)
# 2  
Old 03-14-2009
The syntax you posted is incorrect and would only cause an error message since the pattern is missing the last / .

Second, sed never matches \n, since it considers it to mark the end of the line. sed 's/\r\r$/\r/' should do the job.
# 3  
Old 03-14-2009
Quote:
Originally Posted by Corona688
The syntax you posted is incorrect and would only cause an error message since the pattern is missing the last / .

Second, sed never matches \n, since it considers it to mark the end of the line. sed 's/\r\r$/\r/' should do the job.
I tried this and it does not have any effect on the file

sed 's/\r\r$/\r/' filename > o.txt

I opened my filename in vi and I can see one ^M. when I open in ultraedit I see 0D 0D 0A at the end of each line. I want the file to be in DOS format but as I metnioned before I need to insure that if it is already in DOS format that the sed command does not change it in any way.
# 4  
Old 03-14-2009
I think I got it - please confirm my understanding. When I open the file in vi I only see 1 ^M. In ultraedit I see what looks like 2 carriage returns 0D 0D.

tr -d '\015' < filename > o.txt

the above removed the extra carriage return. Then I tried it on the output file and it left the file unchanged.
# 5  
Old 03-14-2009
I tested my solution as follows before posting it:
Code:
$ echo -en '\r\r\n' | hexdump -C
00000000  0d 0d 0a                                          |...|
00000003
$ echo -en '\r\r\n' | sed 's/\r\r$/\r/' | hexdump -C
00000000  0d 0a                                             |..|
00000002
$ $ echo -en '\r\r\n\r\r\n' | sed 's/\r\r$/\r/' | hexdump -C
00000000  0d 0a 0d 0a                                       |....|
00000004

I am using GNU sed, what version are you using? It may be available on your system as gsed.

Whatever sed you're using may behave better if you remove the $. The $ prevents carriage-returns from being removed in the middle of a line, so if you don't need to worry about that, ditch the $. Also, 's/\r\r/\r/g' might also be worth a try.

That trick with tr will remove all carriage returns, every last one, so it may be that your editor is displaying spurious carriage returns for whatever reason.

Lastly, it is possible you're using an odd OS that considers files to have text mode or binary mode.. This may cause different programs to display the file in different ways depending on the mode they open it in. Are you using cygwin?
# 6  
Old 03-14-2009
I am on SUNos so I use sed

The file is created by an external source. The external source is suppoed to create the file in DOS format but for some reason sometimes they are introduce an extra 0D.

I pick up the file from our server and move it to a folder foR my testing using binary mode SSH client. I open the file in a text editor and I can see 0D 0D 0A. I open it in vi and I can only see one ^M. I though ^M and 0D are the same thing - no?

When I run the tr against a DOS file with 0D 0A it does not remove the 0D -which is what I want.

I am not using cygwin.

One thing - when i view the file in the text editor and go to the end of the line the cursor is on the second 0D. So like this

0D 0D 0A (cursor appears on the bolded 0D)
# 7  
Old 03-14-2009

Code:
sed 's/\r\r/\r/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. UNIX for Dummies Questions & Answers

How can I replace the lines that start with a star and replace it with numbers start from 1?

I need to replace the (*) in the fist of a list with numbers using sed for example > this file contain a list * linux * computers * labs * questions to >>>> this file contain a list 1. linux 2. computers 3. labs 4. questions (7 Replies)
Discussion started by: aalbazie
7 Replies

3. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

4. Shell Programming and Scripting

How to replace

Here is my file content age=12 age=34 age=54 age=23 Hello world. This is the age result I am getting. To day date is 23-02-2010. From the above content I have to replace all the values after 'age=*' to age=24. How to do it. (1 Reply)
Discussion started by: brahma
1 Replies

5. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

6. Shell Programming and Scripting

Replace Help

Hi, djfksdjk)))) kmmm jfdfjk)))) I want to replace the first ')' after kmmm with #. The output would be djfksdjk)))) exists jfdfjk#))) Can anyone help on this? (3 Replies)
Discussion started by: javeed7
3 Replies

7. Shell Programming and Scripting

replace

hi i have input file in this format E102|0|1-23-1994|0|12-5-1994|E003|A|10450|charan,devupalli|5000 how to convert this into outfile E102,0,1-23-1994,0,12-5-1994,E003,A,10450,charan,devupalli,5000 i wann the output in excel sheet.........that is why i am converting into csv.. but i have... (6 Replies)
Discussion started by: charandevu
6 Replies

8. Shell Programming and Scripting

replace last / by |

Hi: I want to write a Kshell script which will replace last / by |. eg: /home/apps/test/document should be replaced as /home/apps/test|document. The length of the string is not constant. Thanks, Ash (6 Replies)
Discussion started by: naikaa
6 Replies

9. Shell Programming and Scripting

How to replace into Vi

Ravi . Pawan 19.23 sanjeeva . I want to replace '.' into 'NULL' not the dot between 19.23 .. only single dots to NULL how to do replacement after opening file in Vi (5 Replies)
Discussion started by: ravi.sadani19
5 Replies

10. UNIX for Dummies Questions & Answers

How to replace ??

Hi all, I have a file with the following data. E01011U, ,E11, , , ,0 E03012U, ,E14, , , ,0 E02013U, ,E25, , , ,0 I want to change it to ('E01-01-1-U','E11' ); ('E03-01-2-U','E14' ); ('E02-01-3-U','E25' ); Please let me know about it .Thanks in advance. (10 Replies)
Discussion started by: preethgideon
10 Replies
Login or Register to Ask a Question