sed:選択した列からデータを抽出する

sed:選択した列からデータを抽出する

次の形式で構成されたログファイルがあります。

# 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

このデータに基づいて、コメント(#と@で始まる)を含むすべての行を無視し、下の複数列から2番目の列のみを取得し、最後に値に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で作成)を省略できますか?

ベストアンサー1

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

おすすめ記事