Using C shell, the following command-line
set pf = "`awk -v var=$pd '{if($1<0) print var, $2, $3}' test.txt`"
returns an error in awk:
awk: {if( <0) print var, , } syntax error.
This is especially puzzling as the command itself works without any problem:
awk -v var=$pd '{if($1<0) print var, $2, $3}' test.txt
Is there a way that we can store all output of the single Awk command line into a single variable? What is the reason the above is failing?
After some tinkering, I can only come to the conclusion that it is one of those C-Shell quirks. C-shell (
csh
ortcsh
) is apparently notoriously known for its peculiarities, and I believe that this is exactly what is going on here. Here are some examples, based on the OP's enigma.unquoted:
quoted:
So, in the double-quoted examples, it looks like
$1
is replaced by an empty string. This explains why the first case prints the full linea_b c
and the third just the number2
. The second fails as the Awk statementprint ,2
is invalid while the first works asprint
is equivalent toprint $0
in Awk.If you play a bit more, you actually notice that C-shell tries to do variable substitution. You actually do not need to use
set
in all the above, just a simple double-quoted command substitution. The following example shows completely how bananas this is:So from this, you see that C-Shell is performing the variable substitutions and
$0
is being replaced with the stringcsh
. But only in the double-quoted version!So, why is this the case? The reason is the double-quotes. A double-quoted string allows variable substitution, disregarding the usage of nested quotes within the double-quoted string. So, even though, the Awk line is single-quoted in a back-wards quoted string, the double-quotes still will do the variable substitution on
$n
. This is in contrast to Bash:So, how can this be solved? While C-shell is completely non-intuitive and mentally a quoting nightmare, it is possible to fix the problem by terminating the quoting early and change from double to single-quotes for a short time.
Solution: So after all this, I think we can conclude that
could potentially solve the problem.