I am trying to run a bash command in the following format:
declare "test${nb}"="$(cat file.txt | awk '{if($3>0.5 && $3 !~ "ddf") $2="NA"; print $1,$2}')" where $nb is an int (e.g. 2) and file.txt contains a table with various numeric and string values (I can provide more details if needed, but it should not be relevant here)
when running this, the shell substitutes !~ for the name of a file that I have (not sure why). I tried escaping this using the backslash like this:
declare "test${nb}"="$(cat file.txt | awk '{if($3>0.5 && $3 \!~ "ddf") $2="NA"; print $1,$2}')" but then I get this error:
awk: {if($3>0.5 && $3 \!~ "ddf") $2="NA"; print $1,$2} awk: ^ backslash not last character on line I also tried having the table contained in the variable "var" and writing it this way:
declare test[$nb]=$(echo "$var" | awk '{if($3>0.5 && $3 !~ "ddf") $2="NA"; print $1,$2}') Then there is no error, but the output is just the first field of first column of the table, which is not the case when I don't expand the variable name. For example, if I do this:
declare test2=$(echo "$var" | awk '{if($3>0.5 && $3 !~ "ddf") $2="NA"; print $1,$2}') then it works perfectly and test2 has the expected value. But I need to be able to use any number instead of 2 (something like test[$nb]).
any idea how I could fix this? Any help will be very appreciated!
thanks