As mentioned in the comments under your previous question, this has nothing to do with CSVs or your awk script, it's all about how you're saving the output of a command.
$ printf 'a\nb\n\n'
a
b
$ col=$(printf 'a\nb\n\n')
$ printf '%s' "$col"
a
b$
$ col=$(printf 'a\nb\n\n'; printf x)
$ printf '%s' "$col"
a
b
x$
$ col="${col%x}"
$ printf '%s' "$col"
a
b
$
Note that with the above you're getting the whole output of the command saved in the variable, including the final newline that command substitution would have stripped off. If you want to remove a final newline too then do a subsequent:
$ col="${col%$'\n'}"
$ echo "$col"
a
b
$ printf '%s' "$col"
a
b
$
The reason to remove the x
and the \n
in 2 steps rather than doing a single col="$(col%$'\n'x}"
is that that would fail if the command had produced no output or output that didn't end in a \n
because then \nx
wouldn't exist in col
:
Right:
$ col=$(printf 'a'; printf x)
$ col="${col%x}"
$ col="${col%$'\n'}"
$ printf '%s' "$col"
a$
Wrong:
$ col=$(printf 'a'; printf x)
$ col="${col%$'\n'x}"
$ printf '%s' "$col"
ax$
To learn more about the issue take a look at "Command Substitution" in:
- The POSIX standard's Shell Execution Environment section where it says:
The shell shall expand the command substitution by executing command
in a subshell environment (see Shell Execution Environment) and
replacing the command substitution (the text of command plus the
enclosing "$()" or backquotes) with the standard output of the
command, removing sequences of one or more characters at the
end of the substitution.
- https://mywiki.wooledge.org/CommandSubstitution where it discusses the issue further and provides the workaround I used above.
arr=("a,a" "b" "")
arr=(); while IFS= read -r line; do arr+=("$line"); done < <(gawk ...)
like we discussed in Putting string with newline separated words into array