Remove pipe(|) symbol in except the ones which are enclosed in double quotes

I have file with are delimited by pipe(|) symbol, I wanted those to be removed except the ones which are enclosed in double quotes.

If your quote file is:

|Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|"

It should return:

Life is Beautiful"|"Indeed life is beautiful too"|"But unix is fun is not "|"

Is this a homework assignment?

What have you tried?

Are you just trying to keep pipe symbols in the three character sequence "|" or are you trying to keep any pipe symbol between paired double quotes?

I am trying to keep the pipe symbol between the quotes

I have tried these, still not getting the expected results.

sed -e ':a;s/^\(\(\(["]\)[^\3]*\3\|[^"]*\)*\)|/\1/;ta' data_file_name

awk '{  for (i = 0; ++i <= NF;) if (i != NF) {printf "%s",$i} else {printf "%s\n",$i}}' FPAT='("[^"]+")|([^|]+)' data_file_name 

Would that work?

sed -e 's/"|"/@@/g; s/|//g; s/@@/"|"/g' data_file_name 

Nope does not

$sed 's/|\([[:alnum:]]\)/\1/g;s/\([[:alnum:]]\)|/\1/g' a
Life is Beautiful"|"Indeed life is beautiful too"|"But unix is fun is not"|"
$cat a
|Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|"
$

Try:

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

or

awk 'NR%2{gsub(/\|/,x)}1' RS=\" ORS=\" file
1 Like

Just intrigued how it does fail for you. May I see the output at your end?
Mine is here:

$ cat pipes.file 
|Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|"
$ sed -e 's/"|"/@@/g; s/|//g; s/@@/"|"/g' pipes.file 
Life is Beautiful"|"Indeed life is beautiful too"|"But unix is fun is not"|"

Output in my system.

$ awk 'NR%2{sub(/\|/,x)}1' RS=\" ORS=\" pipes.file 
Life is |Beautiful"|"Indeed life is beautiful too|"|"But unix is fun is not"|"
$awk '{for(i=1; i<=NF; i+=2) sub(/\|/,x,$i)}1' FS=\" OFS=\" pipes.file 
Life is |Beautiful"|"Indeed life is beautiful too|"|"But unix is fun is not"|"
1 Like

Ah yes I used sub instead of gsub. Corrected it in my post.