bash:AWKコードの一部を含む関数

bash:AWKコードの一部を含む関数

私のbashコードには、入力ファイルと対話し、結果を別のtxtファイルに追加するsed + AWKコードの一部があります。両方の塗りつぶしは同じ bash スクリプトによって生成され、ジョブは異なる変数として保存されます。

#sed removing some lines in input fille "${file}".xvg, defined in the begining of bash script
sed -i '' -e '/^[#@]/d' "${file}".xvg
# AWK measure XMAX and YMAX in the input file
# adding these outputs as two lines in another batch.bfile, which is going to be used for something
awk '
  NR==1{
    max1=$1
    max2=$2
  }
  $1>max1{max1=$1}
  $2>max2{max2=$2}
  END{printf "WORLD XMAX %s\nWORLD YMAX %s\n",max1+0.5,max2+0.5'} "${file}".xvg >> "${tmp}"/batch.bfile

これら2つの(sed + awk)操作をいくつかの関数(bashスクリプトの先頭に定義されている)にまとめてから、スクリプトで1行のコマンドとして使用できますか(もっと複雑な場合はFORに適用されます)。

私のバージョンの例は次のとおりです。

#!/bin/bash


#folder with batch file
home=$PWD
tmp="${home}"/tmp


## define some functions for file processing
bar_xvg_proc () {
##AWK procession of XVG file: only for bar plot;
sed -i '' -e '/^[#@]/d' ${file}
# check XMAX and YMAX for each XVG
awk '
  NR==1{
    max1=$1
    max2=$2
  }
  $1>max1{max1=$1}
  $2>max2{max2=$2}
  END{printf "WORLD XMAX %s\nWORLD YMAX %s\n",max1+0.5,max2+0.5'} ${file} >> "${tmp}"/grace2.bfile

}
###

bar_xvg_proc "${home}"/test.xvg

sedエラーです

sed: -i may not be used with stdin

ただし、スクリプトで関数を呼び出す前に、 test.xvg を新しい変数 $file="${home}"/test.xvg として定義すると正常に動作します。ファイルに特定の変数を割り当てずに入力ファイルに直接この関数を使用するにはどうすればよいですか?

これは私のxvgファイルです。

# Created by:
#                     :-) GROMACS - gmx cluster, 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 cluster is part of G R O M A C S:
#
# Good gRace! Old Maple Actually Chews Slate
#
@    title "Cluster Sizes"
@    xaxis  label "Cluster #"
@    yaxis  label "# Structures"
@TYPE xy
@g0 type bar
       1       94
       2       31
       3       24
       4       24
       5       15
       6        6
       7        6
       8        5
       9        4
      10        4
      11        3
      12        3
      13        3
      14        3
      15        2
      16        2
      17        2
      18        2
      19        1
      20        1
      21        1
      22        1
      23        1
      24        1
      25        1

ベストアンサー1

関数内で$ {file}を "$ 1"に変更すると、必要に応じて機能します。

次に、これを変更することを検討してください。

bar_xvg_proc () {
##AWK procession of XVG file: only for bar plot;
sed -i '' -e '/^[#@]/d' "$1"
# check XMAX and YMAX for each XVG
awk '
  NR==1{
    max1=$1
    max2=$2
  }
  $1>max1{max1=$1}
  $2>max2{max2=$2}
  END{printf "WORLD XMAX %s\nWORLD YMAX %s\n",max1+0.5,max2+0.5'} "$1" >> "${tmp}"/grace2.bfile

}

これに関して:

bar_xvg_proc () {
    ##AWK procession of XVG file: only for bar plot;
    # check XMAX and YMAX for each XVG
    awk '
      /^[#@]/ { next }
      (++nr)==1{
        max1=$1
        max2=$2
      }
      $1>max1{max1=$1}
      $2>max2{max2=$2}
      END{printf "WORLD XMAX %s\nWORLD YMAX %s\n",max1+0.5,max2+0.5'} "${@:--}" >> "${tmp}"/grace2.bfile
}

awkを使用するとsedは必要なく、"${@:--}"この方法では複数のファイル名を渡したり、ストリームをパイプで連結したりする機能を持つことができます。ファイルが存在しない場合は、awkがstdinを使用していることを知らせるからです。

>>最後に代わりに使うべきかどうかはわかりません>。関数の外部で出力リダイレクトを実行したい場合があります。

おすすめ記事