chipoglesby chipoglesby - 5 months ago 18
Bash Question

Removing carriage return within double quotes

I have a simple text file that I'm trying in bash with a shell script, but I keep running into a few problematic lines.

There are a given number of fields and one of the fields is a free form input.

The free form field has carriage returns within the double quotes that I would like to remove.

The format looks similar to this:

"0001","Barker","Bob","Free Form Text Here"
"0002","Barker","Jane", "Free Form
Text Here"


I've tried the following three solutions with no luck:


  1. tr '\r\n' ''
    // removes all carriage returns

  2. sed –e 's/\".*\n"//g'
    // doesn't work

  3. awk -v RS='"[^"]*"' -v ORS= '{gsub(/\n/, " ", RT); print $0 RT}'
    // only returns commas.



Any ideas what I could be doing wrong? I'm fine with using
sed
or
awk
as along as the solution works.

Answer

It is easier using perl:

perl -pe 's/(,\h*"[^\n"]*)\n/$1 /g' file

"0001","Barker","Bob","Free Form Text Here"
"0002","Barker","Jane", "Free Form Text Here"

To save changes inline use:

perl -i -pe 's/(,\h*"[^\n"]*)\n/$1 /g' file