В одну сторону, используя sed
:
Содержимое script.sed
:
## Substitute '\"' with '\n'.
s/\\\"/\n/g
## If there is an odd number of '\"' or the string doesn't end with '\"' I
## will append some at the end. There is no danger, but it will be used to
## avoid an infinite loop.
## 1.- Save content to 'hold space'.
## 2.- Remove all characters except '\n'.
## 3.- Remove one of them because next command will add another one.
## 4.- Put content in 'pattern space' to begin working with it.
## So, if in original string there were 3 '\"', now there will be 6. ¡Fine!
h
s/[^\n]//g
s/\n//
H
g
## Label 'a'.
:a
## Save content to 'hold space'.
h
## Remove from first '\n' until end of line.
s/\(\n\).*$/\1/
## Substitute all commas with pipes.
s/,/|/g
## Delete first newline.
s/\n//
## Append content to print as final output to 'hold space'.
H
## Recover rest of line from 'hold space'.
g
## Remove content modified just before.
s/[^\n]*//
## Save content to 'hold space'.
h
## Get first content between '\n'.
s/\(\n[^\n]*\n\).*$/\1/
s/\n\{2,\}//
## Susbtitute '\n' with original '\"'.
s/\n/\\"/g
## Append content to print as final output to 'hold space'.
H
## Recover rest of line from 'hold space'.
g
## Remove content printed just before.
s/\n[^\n]*\n//
/^\n/ {
s/\n//g
p
b
}
ba
Содержимое infile
:
\"One,Two, Three\" Four, Five, Six
One \"Two\", Three, Four, Five
One \"Two, Three, Four, Five\"
One \"Two\" Three, Four \"Five, Six\"
Запустите как:
sed -nf script.sed infile
Со следующим результатом:
\"One,Two, Three\" Four| Five| Six
One \"Two\"| Three| Four| Five
One \"Two, Three, Four, Five\"
One \"Two\" Three| Four \"Five, Six\"