一連の新しいファイルを生成するための2つのファイル間の算術(パート3)

一連の新しいファイルを生成するための2つのファイル間の算術(パート3)

次のような統合分析形式に変更したいタブ区切りのモデル入力ファイルがあります。

cat input.txt
/*      Precipitation   in      mm      */
10      30      15      20      22      11

###     Species description
***     sp_name LMA     wsg     a_h     Pmass   h_max
abies_lasiocarpa        2       0.5     1       0.001   20
abies_grandis   2.5     0.4     1       0.005   30
larix_occidentalis      1.5     0.3     1       0.003   18

次のように、ディストリビューションでランダムに選択されたタブで区切られた乗数の別のファイルが1行に3つあります。

cat multipliers.txt
2        1        3
4        3        2
3        2        3

現在、以前の質問に対する回答に示すように、1列の乗数ファイルに基づいてさまざまな1つの入力パラメータを処理するようにワークフローが設定されています(2つのファイル間の算術演算により、一連の新しいファイルが生成されます(パート2))。このメソッドはというスクリプトを使用tst.awkし、コマンドを使用して実行されますawk -f tst.awk input.txt multipliers.txt。複数の列で構成された乗数ファイルに基づいて複数の入力を変更するようにこのスクリプトを適用したいと思います。

cat tst.awk
BEGIN { FS=OFS="\t" }
NR==FNR {
    if ( tgtFldNr ) {
        lines[++numLines] = $0
    }
    else {
        hdr = hdr $0 ORS
        if ( /^\*\*\*/ ) {      # in case this line is not tab-separated
            split($0,f," ")
            for (i in f) {
                if ( f[i] == "wsg" ) {
                    tgtFldNr = i-1
                    break
                }
            }
        }
    }
    next
}
{
    mult = $1
    out = "file" FNR ".txt"
    printf "%s", hdr > out
    for (lineNr=1; lineNr<=numLines; lineNr++) {
        $0 = lines[lineNr]
        $tgtFldNr *= mult
        print > out
    }
    close(out)
}

したがって、現在の反復で「wsg」の代わりにmultipliers.txtの3列に基づいて入力「LMA」、「wsg」、および「Pmass」を変更すると仮定すると、出力は次のようになります。

cat file1.txt

(LMA*2、wsg*1、Pmass*3)

/*      Precipitation   in      mm      */
10      30      15      20      22      11

###     Species description
***     sp_name LMA     wsg     a_h     Pmass   h_max
abies_lasiocarpa        4       0.5     1       0.003   20
abies_grandis   5       0.4     1       0.015   30
larix_occidentalis      3       0.3     1       0.009   18
cat file2.txt

(LMA*4、wsg*3、Pmass*2)

/*      Precipitation   in      mm      */
10      30      15      20      22      11

###     Species description
***     sp_name LMA     wsg     a_h     Pmass   h_max
abies_lasiocarpa        8       1.5     1       0.002   20
abies_grandis   10      1.2     1       0.01    30
larix_occidentalis      6       0.9     1       0.006   18
cat file3.txt

(LMA*3、wsg*2、Pmass*3)

/*      Precipitation   in      mm      */
10      30      15      20      22      11

###     Species description
***     sp_name LMA     wsg     a_h     Pmass   h_max
abies_lasiocarpa        6       1       1       0.003   20
abies_grandis   7.5     0.8     1       0.015   30
larix_occidentalis      4.5     0.6     1       0.009   18

私はこれにどのように適応しますかtst.awk?私はelifステートメントをマージしようとしましたが、tst.awkそれがうまく機能するようにするために私がしていることを十分に知らないようです。

ベストアンサー1

awk 'BEGIN{ FS=OFS="\t" }
    NR==FNR         { muts[NR]=$0; c+=1; next }
    !hdr            { for(i=1; i<=c; i++) { close("file"i); print >>("file"i) } }
    /\*\*\*/ && !hdr{ hdr=1; next }
hdr {
      for (num in muts) {
          bak=$0; split(muts[num], tmp);
          $2*=tmp[1]; $3*=tmp[2]; $5*=tmp[3];
          close("file"num); print >>("file"num);
          $0=bak
      }
}' multipliers infile

おすすめ記事