Aquí hay una posible solución alternativa a este problema. Funciona al encontrar los campos que comienzan o terminan con comillas, y luego unirlos. Al final, actualiza los campos y NF, por lo que si coloca más patrones después del que realiza la fusión, puede procesar los campos (nuevos) utilizando todas las características normales de awk.
Creo que esto utiliza solo las funciones de POSIX awk y no depende de las extensiones de gawk, pero no estoy del todo seguro.
# 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;
}
}
En un archivo de entrada como:
thing "more things" "thing" "more things and stuff"
que produce:
Field 1: >>>thing<<<
Field 2: >>>"more things"<<<
Field 3: >>>"thing"<<<
Field 4: >>>"more things and stuff"<<<
muestra el formato de archivo de entrada ... y el resultado deseado! – ghostdog74