sed: extraer datos de la columna seleccionada
Tengo un archivo de registro organizado en el siguiente formato:
# This file was created Thu Dec 17 16:01:26 2020
# Created by:
# :-) GROMACS - gmx gyrate, 2019.3 (-:
#
# Executable: /usr/local/bin/../Cellar/gromacs/2019.3/bin/gmx
# Data prefix: /usr/local/bin/../Cellar/gromacs/2019.3
# Working dir: /Users/gleb/Desktop/DO/unity_or_separation
# Command line:
# gmx gyrate -f /Users/gleb/Desktop/DO/unity_or_separation/storage/7000_cne_lig177/1AllBoxes_7000_cne_lig177.xtc -s /Users/gleb/Desktop/DO/unity_or_separation/storage/7000_cne_lig177/lig_1AllBoxes_7000_cne_lig177.pdb -o /Users/gleb/Desktop/DO/unity_or_separation/storage/7000_cne_lig177/RG/RG_1AllBoxes_7000_cne_lig177.xvg
# gmx gyrate is part of G R O M A C S:
#
# God Rules Over Mankind, Animals, Cosmos and Such
#
@ title "Radius of gyration (total and around axes)"
@ xaxis label "Time (ps)"
@ yaxis label "Rg (nm)"
@TYPE xy
@ view 0.15, 0.15, 0.75, 0.85
@ legend on
@ legend box on
@ legend loctype view
@ legend 0.78, 0.8
@ legend length 2
@ s0 legend "Rg"
@ s1 legend "Rg\sX\N"
@ s2 legend "Rg\sY\N"
@ s3 legend "Rg\sZ\N"
1 0.535827 0.476343 0.375777 0.453993
2 0.509863 0.450424 0.333084 0.453975
3 0.51779 0.374447 0.44955 0.440349
4 0.535215 0.392331 0.442183 0.472716
5 0.542371 0.468222 0.383178 0.47146
6 0.49479 0.340223 0.42002 0.44437
7 0.495905 0.370873 0.445952 0.394239
8 0.518463 0.424257 0.400878 0.443746
A partir de estos datos, necesito omitir todas las líneas que contienen comentarios (comenzando desde # y @), tomando solo la segunda columna de la tabla de múltiples columnas en la parte inferior y finalmente multiplicando los valores por 10:
#this is a second column after conversion
5.4
5.1
5.2
5.4
5.4
4.9
5.0
5.2
Puedo hacerlo combinando sed + awk:
sed -i '' -e '/^[#@]/d' "${storage}"/"${experiment}"/RG/RG_${pdb_name}.xvg awk '-F ' '{ printf("%.1f\n", $2*10) }' "${storage}"/"${experiment}"/RG/RG_${pdb_name}.xvg > "${storage}"/"${experiment}"/RG/RG_${pdb_name}..xvg
¿Es posible realizar todos los pasos usando solo sed (el primer comando), omitiendo así la creación de un nuevo archivo (resultado de AWK)?
Respuestas
Sed no está hecho para aritmética. Podría intentar soluciones torpes, pero Awk es mejor en ese sentido:
awk '!/^[#@]/{printf("%.1f\n",$2*10)}' file
Con GNU Awk, agregue -i inplace
para editar el archivo en su lugar. Si no tiene GNU Awk, puede usarsponge
awk '!/^[#@]/{printf("%.1f\n",$2*10)}' file | sponge file
o use la buena y antigua sobreescritura (es lo que sucede debajo del capó de todos modos ...)
awk '!/^[#@]/{printf("%.1f\n",$2*10)}' file > newfile &&
mv newfile file