![Usar un archivo para ingresar variables en secuencias de comandos csh](https://rvso.com/image/1157178/Usar%20un%20archivo%20para%20ingresar%20variables%20en%20secuencias%20de%20comandos%20csh.png)
unix%:~/tmp$ cat tmp.txt
z=0.016728
NH=5.7E20
Center for spectra: 2:00:14.906, +31:25:45.826
Me gustaría que el valor de z
se establezca en una variable denominada $redsh
, el valor de NH
to be $abun
y los valores del centro to be $xc
y $yc
respectivamente.
¿Cómo voy a hacer esto?
Respuesta1
Usaría sed
para reemplazar los valores del archivo, agregar set
y ejecutar eval
todo:
eval `sed 's/z=/set redsh=/;s/NH=/abun=/;s/.*: \(.*\), \(.*\)/xc=\1 yc=\2/' tmp.txt`
Ejecución de ejemplo
% 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
Sin embargo, tenga en cuenta que:No deberías usar el shell C. Utilice el shell Bourne.
Respuesta2
Lee man csh;man grep;man cut;man awk;man tr
y haz 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 ' '`"