I'm running into issues saving awk output to a variable and preserving the input. I have a tab-delimited file, ABC.txt, with the following format: Field 1 = A, Field 2 = A B <- 2 spaces between the letters A and B, Field 3 = C.
set text = `awk -F "\t" '{print $2}' ABC.txt`
echo $text
A B <- 1 space between the letters A and B,
echo "$text"
A B <- 1 space between the letters A and B,
I need a solution where the variable preserves multiple spaces (I've seen instances where there can be two or three consecutive spaces):
echo $text = A B <- 2 spaces between the letters A and B,
or
echo "$text" = A B <- 2 spaces between the letters A and B,
In CSH, for keeping the blanks you need to double-quote the variable-expansion and the backticks-expression; a simple case would be:
But unlike POSIX compliant shells, CSH doesn't start a new quoting-context for the command-substitutions (aka. backticks-expressions); once you double-quote it, the escaping rules becomes those of the surrounding double-quotes:
".$starts a variable-expansion.`starts a command-substitution.The way to include those characters as literals inside double-quotes is in fact to append them from outside: close the surrounding double-quotes, append the character (with backslash-escaping or inside single-quotes) and then reopen the double-quotes; the shell will take care of the concatenation for you.
Examples:
Both of those output:
What you need to do when double-quoting the command-substitution of
awk -F "\t" '{print $2}' ABC.txtis then:BTW, there's no point in using
awk -F "\t", it's easier withawk -F '\t'instead: