比较两个文件的两列中的值 - awk

时间:2022-11-09 22:51:10

I want to insert the value of the 2nd column from file2.txt as an additional column in file1.txt DEPENDING, if the value in Ist of file1.txt is less than value in the Ist column of file2.txt (see desired output)

我想从file2.txt中插入第二列的值作为file1.txt DEPENDING中的附加列,如果file1.txt的Ist中的值小于file2.txt的Ist列中的值(请参阅所需的输出)

file1.txt

FILE1.TXT

time_stamp ...   values from 1 to n column
0.005000    1   -0.491038 0.251996 -0.681055 0.238940 . . . . . n 
0.010000    1   -0.489565 0.254198 -0.676638 0.244113 . . . . . n
0.015000    1   -0.486287 0.256085 -0.671614 0.249955 . . . . . n
0.020000    1   -0.481431 0.257644 -0.665849 0.256754 . . . . . n
.
.
2.090000    1   -0.198916 0.255882 -0.215078 0.377169 . . . . . n
2.095000    1   -0.201185 0.248889 -0.215151 0.371873 . . . . . n
2.100000    1   -0.202041 0.240778 -0.214463 0.365228 . . . . . n
2.105000    1   -0.200996 0.231328 -0.212667 0.357113 . . . . . n
.
.
.
.

file2.txt

FILE2.TXT

duration symbol
0.37800  # (from 0.00000 to 0.37800 >>> symbol #)
0.79800  l (from 0.37800 to 0.79800 >>> symbol l)
0.87800  i
.
.
1.04600  aI
1.10400  k
1.17200  t
.
.
.
2.04600  b
2.09400  i
2.10400  I
.
.
.  
3.96800  b
4.01600  I
.
.
.
4.35000  i
4.77200  #

Desired output:

期望的输出:

symbol time_stamp
#      0.005000    1   -0.491038 0.251996 -0.681055 0.238940 . . . . . n 
#      0.010000    1   -0.489565 0.254198 -0.676638 0.244113 . . . . . n
#      0.015000    1   -0.486287 0.256085 -0.671614 0.249955 . . . . . n
#      0.020000    1   -0.481431 0.257644 -0.665849 0.256754 . . . . . n
.
.
b      2.090000 1   -0.198916 0.255882 -0.215078 0.377169 . . . . . n
i      2.095000 1   -0.201185 0.248889 -0.215151 0.371873 . . . . . n
i      2.100000 1   -0.202041 0.240778 -0.214463 0.365228 . . . . . n
i      2.105000 1   -0.200996 0.231328 -0.212667 0.357113 . . . . . n
.
.
.
.

1 个解决方案

#1


2  

You need something like this:

你需要这样的东西:

awk '
NR==FNR { key2val[$1] = $2; next }
{
    pfx=""
    for (key in key2val) {
        if (key < $1) {
            pfx = key2val[key]
        }
    }
    print pfx, $0
}
' file2 file1

I'm sure the math in the loop is wrong but all those . lines cluttering and obfuscating your input and output files mean we don't have anything we can easily copy/paste to test with so I'm not going to put any effort into that part and you should be able to figure that detail out given this simple and obvious program structure anyway.

我确信循环中的数学是错误的,但所有这些。线条混乱和混淆你的输入和输出文件意味着我们没有任何东西,我们可以很容易地复制/粘贴测试,所以我不会在这部分做任何努力,你应该能够计算出这个细节给出这个无论如何,程序结构简单明了。

#1


2  

You need something like this:

你需要这样的东西:

awk '
NR==FNR { key2val[$1] = $2; next }
{
    pfx=""
    for (key in key2val) {
        if (key < $1) {
            pfx = key2val[key]
        }
    }
    print pfx, $0
}
' file2 file1

I'm sure the math in the loop is wrong but all those . lines cluttering and obfuscating your input and output files mean we don't have anything we can easily copy/paste to test with so I'm not going to put any effort into that part and you should be able to figure that detail out given this simple and obvious program structure anyway.

我确信循环中的数学是错误的,但所有这些。线条混乱和混淆你的输入和输出文件意味着我们没有任何东西,我们可以很容易地复制/粘贴测试,所以我不会在这部分做任何努力,你应该能够计算出这个细节给出这个无论如何,程序结构简单明了。