0

I have a file called "random" that looks like this:

A B C D
A B C D
A B C D

How can I get this shell script to print up the second column?

#! /usr/bin/tcsh -f

set variable = "2" awk '{print "$""'"$variable"'" }' random

It gives:

$2
$2
$2
$2
$2

if I hardcode $2 like this:

#! /usr/bin/tcsh -f

awk '{print $2 }' random

I get

B
B
B

But I need to set the value differently each time so hardcoding isn't an option

ZakS
  • 305
  • tcsh and csh are terrible for shell scripting. There are many inconsistencies that cannot really be coded around. I would most strongly recommend that you write scripts in bash or sh rather than [t]csh. (By all means leave your interactive sessions with tcsh.) – Chris Davies Dec 17 '21 at 09:22

2 Answers2

2

Pass the variable to awk with awk's -v option. e.g. to set the awk variable n to the same value as the csh variable $variable:

% set variable=2
% awk -v n="$variable" '{print $n}' random
B
B
B

Alternatively, pass the variable assignment as an argument after the script (awk will interpret arguments like x=y as an assignment, not as a filename):

% set variable=4
% awk '{print $n}' n="$variable" random
D
D
D

In both cases, the awk script will interpret $n as "the field numbered n"

Alternatively, you can use environment variables. awk can access them through the ENVIRON array. e.g.

% setenv variable 2
% awk '{print $ENVIRON["variable"]}' random
B
B
B

BTW, as others have mentioned in comments, csh/tcsh is not a good choice for a scripting language - use a bourne-like shell for shell scripting. tcsh is still kind of OK-ish for an interactive shell, but doesn't offer anything that other shells don't already offer - csh was a superior interactive shell in the 1980s but other shells have long since surpassed it (and csh has the disadvantage of being different from a good scripting shell language, so you won't get experience with scripting just from interactive use of it). Try switching to bash, ksh, or zsh instead.

cas
  • 78,579
  • second option worked, thanks. regarding your third option, I get this error: "setenv: Variable name must contain alphanumeric characters." Not quite sure why – ZakS Dec 17 '21 at 12:44
  • all three options work. i tested them before posting. the code-formatted sections are copy-pasted from tcsh in my terminal. – cas Dec 17 '21 at 12:51
  • hey @cas I am 110% sure the error is on my end! I was just curious if you might know why it might be happening. But in any event your answer has helped me greatly - thank you. – ZakS Dec 17 '21 at 12:54
  • 1
    i copy-pasted the wrong setenv line from my terminal. should be setenv variable 2 (without an = symbol). I have updated the answer. – cas Dec 17 '21 at 12:57
1

Not that I remember all of (t)csh's quirks, but I think this works similarly to how it does in a POSIX-like shell. You have a quoted string '{print "$""', including the double-quotes; then the quoted variable expansion "$variable"; then another quoted string '" }'. What awk gets is {print "$""2" }, there's two back-to-back awk strings, which awk concatenates. But they're strings, not the $ operator.

awk '{print "$""'"$variable"'" }'
    <--quoted---><---var---><-q-> 

Lose the extra double-quotes:

tcsh> set variable = 2
tcsh> echo a b c | awk '{print $'"$variable"' }'
b

or in a POSIX-y shell:

$ variable=2
$ echo a b c | awk '{print $'"$variable"' }'
b
ilkkachu
  • 138,973