Reputation: 51
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)
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
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
.
.
.
.
Upvotes: 0
Views: 148
Reputation: 204731
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.
Upvotes: 2