Issue
I have below records into txt file.
000D3A|"RiFR Botnets" AD||83634C|dk
000D3|Ries Bidvest" AD||8364A3C|dhh
000D3A|"Ra Boots D"||83634C|gft
here I want to add double quotes for those records which having " into the line using AWK Unix command.
expected output which I want to write to file:
000D3A|""RiFR Botnets" AD"||83634C|dk
000D3|"Ries Bidvest" AD"||8364A3C|dhh
000D3A|""Ra Boots""||83634C|gft
I have tried using AWS command and AWK -F "|" but how do i search double quotes " here into the every line of file.
Solution
You can try:
awk -F"|" -v OFS="|" '{
for (i=1;i<=NF;i++) # for every field...
if (match($i,"\"")) # check for "
$i="\"" $i "\"" # add quotes to that field
}
1 # print' file
000D3A|""RiFR Botnets" AD"||83634C|dk
000D3|"Ries Bidvest" AD"||8364A3C|dhh
000D3A|""Ra Boots D""||83634C|gft
Or, you could use this sed
:
sed -E 's/\|([^|]*"[^|]*)\|/|"\1"|/g' file
# same output
(As noted in comments, the result is not valid csv. You requested this, but "Ries Bidvest" AD"
is not valid quoting and will break csv parsing...)
Answered By - dawg