Ecco una possibile soluzione alternativa a questo problema. Funziona trovando i campi che iniziano o terminano con le virgolette, e quindi unendoli tra loro. Alla fine aggiorna i campi e NF, quindi se metti più pattern dopo quello che fa l'unione, puoi elaborare i (nuovi) campi usando tutte le normali funzioni di awk.
Penso che questo usi solo le funzioni di POSIX awk e non si basa sulle estensioni di gawk, ma non ne sono completamente sicuro.
# This function joins the fields $start to $stop together with FS, shifting
# subsequent fields down and updating NF.
#
function merge_fields(start, stop) {
#printf "Merge fields $%d to $%d\n", start, stop;
if (start >= stop)
return;
merged = "";
for (i = start; i <= stop; i++) {
if (merged)
merged = merged OFS $i;
else
merged = $i;
}
$start = merged;
offs = stop - start;
for (i = start + 1; i <= NF; i++) {
#printf "$%d = $%d\n", i, i+offs;
$i = $(i + offs);
}
NF -= offs;
}
# Merge quoted fields together.
{
start = stop = 0;
for (i = 1; i <= NF; i++) {
if (match($i, /^"/))
start = i;
if (match($i, /"$/))
stop = i;
if (start && stop && stop > start) {
merge_fields(start, stop);
# Start again from the beginning.
i = 0;
start = stop = 0;
}
}
}
# This rule executes after the one above. It sees the fields after merging.
{
for (i = 1; i <= NF; i++) {
printf "Field %d: >>>%s<<<\n", i, $i;
}
}
Su un file di input come:
thing "more things" "thing" "more things and stuff"
produce:
Field 1: >>>thing<<<
Field 2: >>>"more things"<<<
Field 3: >>>"thing"<<<
Field 4: >>>"more things and stuff"<<<
mostra il formato del file di input ... e l'output desiderato! – ghostdog74