unix%:~/tmp$ cat tmp.txt
z=0.016728
NH=5.7E20
Center for spectra: 2:00:14.906, +31:25:45.826
Eu gostaria que o valor de z
fosse definido como uma variável chamada $redsh
, o valor de NH
to be $abun
e os valores do centro to be $xc
e $yc
respectivamente.
Como faço para fazer isso?
Responder1
Eu usaria sed
para substituir os valores do arquivo, adicionar set
e executar eval
tudo:
eval `sed 's/z=/set redsh=/;s/NH=/abun=/;s/.*: \(.*\), \(.*\)/xc=\1 yc=\2/' tmp.txt`
Exemplo de execução
% 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
Observe, no entanto, que:Você não deve usar o shell C. Use o shell Bourne.
Responder2
Leia man csh;man grep;man cut;man awk;man tr
e faça algo como
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 ' '`"