On Sun, Jan 12, 2025, at 10:19 AM, MacBeth wrote: > On Sun, Jan 12, 2025 at 8:58 AM MacBeth <macbeth.112...@gmail.com> wrote: > >> And here is a version you would have to use without `sed` to get the line >> first, before you can append the field terminator to it: >> >> delim=, >> { >> while IFS= read -r row; do >> IFS="$delim" read -r k values < <(echo "$row$delim") >> values="${values%$delim}" >> printf "%-20s %s\n" "row='$row'" "k='$k' v='$values'" >> done >> } <<EOT >> a,b,c,d,e >> a,b,c,d, >> a,b,c,d >> a,b,c, >> a,b,c >> a,b, >> a,b >> a, >> a >> EOT >> >> > FYI... > > * The non-sed version works by setting a blank IFS, preventing 'word > splitting', which deactivates bash from possibly mangling the input, so we > can then process it however we want to. > > * Be careful with $delim, it is mainly to show by example, but is not safe > for any value... '/' in the `sed` version, for example.
The commands echo "$row$delim" and values="${values%$delim}" aren't safe either. The former is subject to the usual vagaries of "echo", and the latter uses the expansion of $delim as a pattern. Fixing these is not hard but is left as an exercise for the reader. -- vq