Sed: replacing a double quote in a field with quotation marks in a delayed entry

The specified optionally quoted file with channel separators with the following entries:

"foo"|"bar"|123|"9" Nails"|"2" "blah"|"blah"|456|"Guns "N" Roses"|"7" "brik"|"brak"|789|""BB" King"|"0" "yin"|"yang"|789|"John "Cougar" Mellencamp"|"5"

I want to replace any double quotes next to the delimiter.

I used the following and it almost works. With one exception.

sed "s/\([^|]\)\"\([^|]\)/\1'\2/g" a.txt

The result is as follows:

"foo"|"bar"|123|"9' Nails"|"2" "blah"|"blah"|456|"Guns 'N" Roses"|"7" "brik"|"brak"|789|"'BB' King"|"0" "yin"|"yang"|789|"John 'Cougar' Mellencamp"|"5"

It does not capture the second set of quotes if they are separated by a single character, as in Guns "N" Roses. Does anyone know why this is so and how it can be fixed? At the same time, I just pass the output to the second regular expression to handle the special case. I would prefer to do this in one go, as some files can be quite large.

Thanks in advance.

+4
source share
2 answers

sed:

sed -r "s/([^|])\"([^|])/\1'\2/g; s/([^|])\"([^|])/\1'\2/g" file
"foo"|"bar"|123|"9' Nails"|"2"
"blah"|"blah"|456|"Guns 'N' Roses"|"7"
"brik"|"brak"|789|"'BB' King"|"0"
"yin"|"yang"|789|"John 'Cougar' Mellencamp"|"5"
+1

sed while:

sed ':a; s/\([^|]\)"\([^|]\)/\1'\''\2/g; ta' file

t a, s/// -. , .

, perl , :

perl -pe 's/[^|]\K"(?!\||$)/'\''/g'

, :

perl -pe 's//'\''/g while /[^|]\K"(?!\||$)/' file

\x27 awkward '\'' . perl GNU sed.

+1

All Articles