You have a few errors in your awk
command.
The following will import $arg
as the awk
variable arg
:
awk -F , -v OFS=, -v arg="$arg" '{ $2 = arg; print }' >"$outfile"
$2
can impossibly be interpreted as the second positional parameter here or in your code since it occurs in single quotes, and the shell does not expand single quoted variables.
OFS
and arg
(both are awk
variables) are set with -v
on the command line. You don't use -v
to set OFS
and you use -v
in the wrong way in front of the actual awk
code. This would have given you an error (which you don't mention). In fact -OFS,
with GNU awk
means "turn on optimisation and set the input field separator to the string S,
".
Note that if $arg
(the shell variable) contains backslashes that you need to preserve, then it's better to pass the value through the environment instead since these would otherwise be interpreted by awk
:
arg="$arg" awk -F , -v OFS=, '{ $2 = ENVIRON["arg"]; print }' >"$outfile"
The output file is presumably supposed to be $outfile
, not outfile
.
Related:
arg
. – Ed Morton Oct 09 '19 at 17:53