sed : 선택한 열에서 데이터 추출

Dec 17 2020

다음 형식으로 정렬 된 로그 파일이 있습니다.

# 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

이 데이터에서 주석이 포함 된 모든 줄 (# 및 @에서 시작)을 생략하고 하단의 다중 열 테이블에서 두 번째 열만 가져와 결국 값에 10을 곱해야합니다.

#this is a second column after conversion
5.4
5.1
5.2
5.4
5.4
4.9
5.0
5.2

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

sed (첫 번째 명령) 만 사용하여 모든 단계를 수행 할 수 있으므로 새 파일 (AWK 결과) 생성을 생략 할 수 있습니까?

답변

6 Quasímodo Dec 17 2020 at 22:55

Sed는 산술 용이 아닙니다. 서투른 해결 방법을 시도 할 수 있지만 Awk가 그 점에서 더 좋습니다.

awk '!/^[#@]/{printf("%.1f\n",$2*10)}' file

GNU Awk를 사용 -i inplace하여 파일을 제자리에 추가 합니다. GNU Awk가 없으면 다음을 사용할 수 있습니다.sponge

awk '!/^[#@]/{printf("%.1f\n",$2*10)}' file | sponge file

또는 오래된 덮어 쓰기를 사용하십시오 (어쨌든 후드 아래에서 일어나는 일입니다 ...)

awk '!/^[#@]/{printf("%.1f\n",$2*10)}' file > newfile &&
mv newfile file