Deleting [CR][LF] using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting [CR][LF] using sed
# 1  
Old 02-27-2017
Deleting [CR][LF] using sed

Dear all,

I face a problem I can not solve: I have different lines in a file and some of them are ending with [CR][LF] ie "\r\n" and the others are ending with [LF] "\n".

My aim is to supress the string "\r\n" and concatenate that line with the following one.

For example:

Saying I have a file 4_test.csv with the 4 following lines:

Code:
[x004191a@xsnl11p370p tmp]$ cat -v 4_test.csv
123^M
456
789^M
0AB
[x004191a@xsnl11p370p tmp]$ od -c 4_test.csv
0000000   1   2   3  \r  \n   4   5   6  \n   7   8   9  \r  \n   0   A
0000020   B  \n
0000022
[x004191a@xsnl11p370p tmp]$

My aim is to get as a result a file with only the two following lines:

Code:
[x004191a@xsnl11p370p tmp]$ cat -v 4_test_target.csv
123456
7890AB
[x004191a@xsnl11p370p tmp]$ od -c 4_test_target.csv
0000000   1   2   3   4   5   6  \n   7   8   9   0   A   B  \n
0000016
[x004191a@xsnl11p370p tmp]$

I tried different syntax using sed but I was not able to suppress the two consecutive characters '\r\n' !

Any kind of idea would be greatly appreciated ... Thanks a lot,

Didier.
# 2  
Old 02-27-2017
Code:
gawk '!/\r/{ORS=RS}/\r/{ORS=X}{gsub(/\r/,X)}1' filename

This User Gave Thanks to Yoda For This Post:
# 3  
Old 02-28-2017
Code:
mute@zbox:~$ sed '/\r$/{N;s/\r\n//}' 4_test.csv
123456
7890AB

I'm usually an awk guy myself but this seemed worthy
This User Gave Thanks to neutronscott For This Post:
# 4  
Old 02-28-2017
Code:
awk '{ORS=sub(/\r$/,X)?X:RS}1' filename

The previous solution works with GNU(Linux) sed only. A Unix sed does not have \r.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 02-28-2017
Many Thanks to all of you for your help,

Regards,

Didier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed for deleting the first word of each line?

sed /'1-2'/&^/ filename suppose there is a file containing three lines , how do we do delete the word from each line? hyter efr frf rerfer efe ewd cdcf evrfgf erfv the output has to look like frf ewd erfv (2 Replies)
Discussion started by: Rajeev Nukala
2 Replies

2. UNIX for Dummies Questions & Answers

Deleting last 3 lines from a file via sed

Hi Anybody can help me to delete the last 3 lines from a text file via sed under SunOS 5.8? Thanks Aldar (4 Replies)
Discussion started by: aldar
4 Replies

3. UNIX for Dummies Questions & Answers

Deleting all but a regex using sed, tr, cut etc

Hi guys, this is my first post, though I've been looking around the forums for a while trying to find a solution to my problem. I want to be able to take a several lines of text (an out put from get-iplayer and ls to be precise) and only keep the crazy alphanumerical code in each line. For example:... (6 Replies)
Discussion started by: Pureferret
6 Replies

4. Shell Programming and Scripting

sed - deleting each line up to a word

Hi there, I'd like to delete the beginning of a line up until it finds a certain word or character string: in this case, I'd like to delete each line up to the word "mounting". Thanks ;) Susan (12 Replies)
Discussion started by: kitykity
12 Replies

5. UNIX for Dummies Questions & Answers

deleting text with sed

Hi There! I've got a tab delimited text file (output from a software) to which I would like to delete specific strings from one of the columns. I have tried several sed codes, but they do not seem to work for me. I can manage to delete a specific word, but this is of no use, as I what I want to... (3 Replies)
Discussion started by: alfredman
3 Replies

6. Shell Programming and Scripting

deleting text records with sed (sed paragraphs)

Hi all, First off, Thank you all for the knowledge I have gleaned from this site! Deleting Records from a text file... sed paragraphs The following code works nearly perfect, however each time it is run on the log file it adds a newline at the head of the file, run it 5 times, it'll have 5... (1 Reply)
Discussion started by: Festus Hagen
1 Replies

7. Shell Programming and Scripting

Deleting lines using Sed

Hi All, Please can anyone help me as am deleting a line in a file with the below script: sed '/"$value"/d' redirects.virgin-atlantic.com.conf > olist where $value is a variable where the pattern to be matched is stored. I am not getting any error also but the line containing the pattern... (2 Replies)
Discussion started by: Shazin
2 Replies

8. Shell Programming and Scripting

sed logic before deleting

FileA NAME STATE CITY ---- ---- ----- abc ca ca bcc ny ny def nj nj (3 rows affected) Q1) I want to delete the second row with is ---- ---- -----. Can delete 2nd row using following sed '2d' FileA >FileB but incase the second record is not ---- ---- -----. then data will... (3 Replies)
Discussion started by: pinnacle
3 Replies

9. Shell Programming and Scripting

Help deleting lines with SED.

I take the /etc/passwd file and print it out, but I only want the lines that end with sh. I have cat /etc/passwd | sed '/sh/!d' Which prints out all lines that have sh somewhere in it. So I added $, which I thought matches the ends on lines, but its not working, like for example I have have... (5 Replies)
Discussion started by: Bandit390
5 Replies

10. Shell Programming and Scripting

Deleting Multiple Lines with sed

I am trying to use sed to delete multiple lines in a file. The problem is that I need to search for a certain line and then once found delete it plus the next 4 lines. For instance if I had a file that consisted of the following lines: #Data1.start ( (Database= data1) (Name = IPC)... (1 Reply)
Discussion started by: rambo15
1 Replies
Login or Register to Ask a Question