How to remove Carriage Return (CRLF) within double quotes

July 22, 2015 at 08:32:55
Specs: Ubuntu 14.04.2 LTS, 512
How to remove Carriage Return (CRLF) within double quotes in a file. There are multiple CRLFs within double quotes. We are on Ubuntu 14.04.2 LTS. Can someone advice with awk or sed script.
Note: command should only remove Carriage Return within Double Quotes

message edited by covina

See More: How to remove Carriage Return (CRLF) within double quotes

Report •

July 22, 2015 at 22:56:27
If I am understanding correctly, you want to find a CRLF within double quotes and then remove just the CR. This perl script does that:

#!/usr/bin/perl -w

# this script finds a CRLF between double quotes and only removes the CR
open FILE, "./data.txt" or die "Error message here: $!";

while (<FILE>) {
   my $newline = $_;
   $newline =~ s/"^M^L"/"^L"/g;
   print "$newline\n";
close (FILE);

# be advised that in the script above ^M and ^L are actual control-M and control-L characters. To create them in vi press the control-v key combination and press control-m. Repeat the same process for control-L

Or if you just want a perl one-liner to do the job:

perl -wpl -e 's/"^M^L"/"^L"/g;' data.txt > newdata.txt

Let me know if you have any questions.

message edited by nails

Report •

July 23, 2015 at 12:07:25
Mr Nails it does not work. The file that we are importing is a csv file from unix to windows and the file was formatted to unix2dos. Therefore all lines in the file all have CRLF at the end. However, there is a comment field where we see multiple CRLFs within this single field and the contents of the field is enclosed with double quotes. So, when we open the file in notepad it looks like a single line is broken into several lines. If the file is opened in the excel it comes out OK.

My requirement is to replace all CRLFs within double quotes should be replaced by a single space.


message edited by covina

Report •

July 23, 2015 at 13:57:38
Then change CRLF to a single space as below:

perl -wpl -e 's/"^M^L"/" "/g;' data.txt > newdata.txt

Report •

Related Solutions

July 24, 2015 at 07:27:31
It won't work, I tried the same command in AIX 7.1. Can you suggest other alternatives like sed.

Report •

July 24, 2015 at 10:35:30
Saying, "It won't work does not help".

First, are you getting an error message? If so what?

Second, did you create the CR and LF as I instructed? It won't work otherwise.

Finally, post an example of your data.

Report •

Ask Question