Skip to main content
further simplification
Source Link
Freddy
  • 26.3k
  • 1
  • 27
  • 64

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. If the string is empty, append the opening ", else ; before each field. InIn the END section, print the string plus the opening and closing ".

awk -v FPAT='.=[^;"]+'=[^;"]+(; )?' '{
    for (i=1;i<=NF;i++) s=s (s=="" ? "\"" : "; ") $i
}
END {
    print "\"" s "\""
}
 ' file

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. If the string is empty, append the opening ", else ; before each field. In the END section, print the string plus the closing ".

awk -v FPAT='.=[^;"]+' '{
    for (i=1;i<=NF;i++) s=s (s=="" ? "\"" : "; ") $i
}
END {
    print s "\""
}
 ' file

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. In the END section, print the string plus the opening and closing ".

awk -v FPAT='.=[^;"]+(; )?' '{
    for (i=1;i<=NF;i++) s=s $i
}
END {
    print "\"" s "\""
}' file
simplified regex
Source Link
Freddy
  • 26.3k
  • 1
  • 27
  • 64

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. If the string is empty, append the opening ", else ; before each field. In the END section, print the string plus the closing ".

awk -v FPAT='[a-z]=[^;"]+'FPAT='.=[^;"]+' '{
    for (i=1;i<=NF;i++) s=s (s=="" ? "\"" : "; ") $i
}
END {
    print s "\""
}
' file

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. If the string is empty, append the opening ", else ; before each field. In the END section, print the string plus the closing ".

awk -v FPAT='[a-z]=[^;"]+' '{
    for (i=1;i<=NF;i++) s=s (s=="" ? "\"" : "; ") $i
}
END {
    print s "\""
}
' file

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. If the string is empty, append the opening ", else ; before each field. In the END section, print the string plus the closing ".

awk -v FPAT='.=[^;"]+' '{
    for (i=1;i<=NF;i++) s=s (s=="" ? "\"" : "; ") $i
}
END {
    print s "\""
}
' file
Source Link
Freddy
  • 26.3k
  • 1
  • 27
  • 64

Using GNU awk, you could use FPAT to split each record into the key-value pairs, loop over the fields and append them to a string. If the string is empty, append the opening ", else ; before each field. In the END section, print the string plus the closing ".

awk -v FPAT='[a-z]=[^;"]+' '{
    for (i=1;i<=NF;i++) s=s (s=="" ? "\"" : "; ") $i
}
END {
    print s "\""
}
' file