Need shell script to append double quotes for each column in a file

Hi Experts,

I am beginner to the shell scripting, My requirement is to append double quotes for each column in a file if double quotes does not exist.

Example:

"abc"|123|"gh-ch"|23.067
awk '{for (i=1; i<=NF; i++) {sub("^\"", "", $i); sub("\"$", "", $i); gsub("\"", "\\\"", $i); $i="\"" $i "\""} ; print}' FS="|" OFS="|" infile

Try:

sed 's/"*\([^|"]*\)"*/"\1"/g' file

or

awk '{for(i=1; i<=NF; i++) gsub(/^"*|"*$/,"\"",$i)}1' FS=\| OFS=\| file

Thanks for the answers!!

But i am having problem with the first column, my file looks like below example.

"G|||ABCD|||1234"|"ABC"|123.05|"xyz"

except first column i have to apply the above code( as in first column data itself contains pipe(|) in the data)

so when i use above commands the first column pipes are replacing with the double quotes

"G"|"|"|"ABCD"|"|"|"1234"|"ABC 

So i need output like

"G|||ABCD|||1234"|"ABC"|"123.05"|"XYZ"

Thanks everyone

In that case see if this works:

awk '{for(i=1; i<=NF; i+=2) gsub(/[^|]+/,"\"&\"",$i)}1' FS=\" OFS=\" file

Hi,
I think this one with sed is ok.

sed 's/^/"/;s/\([^|]\)\([|]\)\([^|]\)/\1"\2"\3/g;s/$/"/;s/""/"/g' infile

I don't understand this one from Scrutinizer in #3

sed 's/"*\([^|"]*\)"*/"\1"/g' infile

Someone can explain ?
Thanks.

That replaces any substring NOT containing | nor " , be it enclosed in double quotes or not, with itself enclosed in double quotes. In the requestor's sample line that occurs 4 times.

Hi,
Thanks to reply but sorry, my question was too short.
So, I try to explain my non-understanding.

If infile contain a line like that

abc|123"|gh-ch"|23.067"

with this command line

sed 's/"*\([^|"]*\)"*/"\1"/g' infile

sed with g at the end operate 4 times.
so,

time 1 :

"* = ''
\([^|"]*\) = abc
"* = ''
pattern = "abc"|123"|gh-ch"|23.067"
               ^

sed stop on the | for the second pass

time 2 :

"* = ''
\([^|"]*\) = ''
"* = ''
pattern = "abc"|123"|gh-ch"|23.067"
               ^

sed stop on the | for the third pass

etc ...
and sed never stop.

It's my understanding.
It's false but I can't figure out how this work.
That's why I ask.

To obtain the same result
my command line is

sed 's/"*\([^|"]*\)"*\(|*\)/"\1"\2/g' infile

Thanks.

The regex

"*\([^|"]*\)"*

applied to

abc|123"|gh-ch"|23.067"

will yield the first parenthesized subexpression to be used by the back reference

abc
123
gh-ch
23.067

Any existing double quotes will be excluded from the subexpression and thus the back reference. Does this example help you understand:

sed ' s/"*\([^|"]*\)"*/X\1X/1; p; s/"*\([^|"]*\)"*/X\1X/2; p; s/"*\([^|"]*\)"*/X\1X/3; p; s/"*\([^|"]*\)"*/X\1X/4' file
XabcX|123"|gh-ch"|23.067"
XabcX|X123X|gh-ch"|23.067"
XabcX|X123X|Xgh-chX|23.067"
XabcX|X123X|Xgh-chX|X23.067X
1 Like

Many tanks.
I don't know this type of back reference too

s/"*\([^|"]*\)"*/X\1X/3
                      ^

That's not a back reference, but one on the s command's flags following the replacement. info sed :