ファイルの数を使用して計算

ファイルの数を使用して計算

私のデータは、2つの列を持つfile1と1つの列を持つfile2で構成されています。この計算を適用する必要があります

for (i = 1; i == NR)

{x = ($1-T1)/Fi; print (x-int(x))} 

$ 1はファイル1の最初の列、T1はファイル1の最初の列の最初の行、Fiはファイル2のi番目の行です。

ファイル1

5     2
56    3
566   2
54    2

ファイル2

1
2
6
8

計算は

{x = ($1(file1)-5)/1; print (x-int(x))}
--> output1
{x = ($1(file1)-5)/2; print (x-int(x))}
--> output2
{x = ($1(file1)-5)/6; print (x-int(x))}
--> output3
{x = ($1(file1)-5)/8; print (x-int(x))}
--> output4

必要な結果は、各列に4つの数字を持つ4つのファイルです。私の言葉は、$ 1だけが計算中に変更される数値であり、他の変数は固定されることを意味します。

ベストアンサー1

awk 'FNR == NR { F[++n] = $1; next } FNR == 1 { T1 = $1 } { for (i = 1; i <= n; ++i) { x = ($1 - T1)/F[i]; print x - int(x) >"output" FNR} }' file2 file1

file2最初に(単一の列を含むコマンドラインに提供されている最初のファイル)の内容を読み取り、配列に保存しますF

次に、file1配列内の値だけ各行の数字を読み、計算しますF。このように計算された数字の各行のfile1名前outputの後にfile1

結果:

$ ls
file1   file2   output1 output2 output3 output4
$ cat output1
0
0
0
0
$ cat output2
0
0.5
0.5
0.375
$ cat output3
0
0.5
0.5
0.125
$ cat output4
0
0.5
0.166667
0.125
$ cat output5

コメント付きのスクリプトawk:

FNR == NR {
    # This is the first file.
    # Read its data into F, and then continue.

    F[++n] = $1
    next
} 

# We are now only processing the second file.

FNR == 1 {
    # Save T1 from the first line of the second file.
    T1 = $1
} 

{
    # Loop through F and compute x for each.
    for (i = 1; i <= n; ++i) {
        x = ($1 - T1)/F[i]

        # Print to the file given by the current line number.
        print x - int(x) >"output" FNR
    }
}

おすすめ記事