Using a file to input variables in csh scripting

unix%:~/tmp$ cat tmp.txt
z=0.016728
NH=5.7E20
Center for spectra: 2:00:14.906, +31:25:45.826

I would like for the value of z to be set to a variable named $redsh, the value of NH to be $abun, and the values for the center to be $xc and $yc respectively.

How do I go about doing this?

2 Answers

I’d use sed to replace the values from the file, add set and run eval on the whole thing:

eval `sed 's/z=/set redsh=/;s/NH=/abun=/;s/.*: \(.*\), \(.*\)/xc=\1 yc=\2/' tmp.txt`

Example run

% unset redsh abun xc yc
% cat tmp.txt
z=0.016728
NH=5.7E20
Center for spectra: 2:00:14.906, +31:25:45.826
% eval `sed 's/z=/set redsh=/;s/NH=/abun=/;s/.*: \(.*\), \(.*\)/xc=\1 yc=\2/' tmp.txt`
% set
abun 5.7E20
…
redsh 0.016728
…
xc 2:00:14.906
yc +31:25:45.826

Note however that: You shouldn't use the C shell. Use the Bourne shell.

Read man csh;man grep;man cut;man awk;man tr and do something like

set redsh = "`grep -E '^z=' tmp.txt | cut -d= -f2`"
set abun = "`grep -E '^NH=' tmp.txt | cut -d= -f2`"
set xc = "`grep -E '^Center for spectra: ' tmp.txt | cut -d, -f1 | cut '-d ' -f4`"
set yc = "`grep -E '^Center for spectra: ' tmp.txt | cut -d, -f2 | tr -d ' '`"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like