0
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?

3

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.

1

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.