I have a file called file1
on my Linux machine with these contents:
ID Time Energy
43 4.38665978376386365240533e-05 3.215e-02
43 4.38666558838421885677290e-05 4.788e-01
48 4.77674337753321466689890e-05 1.750e-01
48 4.77674676992522297732519e-05 3.360e-01
52 4.99184267458611271553633e-05 3.110e-01
52 4.99184755438830858337990e-05 2.000e-01
203 2.17111141925415134391192e-04 0.000e+00
203 2.17111189970080955017814e-04 1.685e-01
203 2.17111190317825032474949e-04 3.425e-01
206 2.17705422992319207490738e-04 3.197e-01
206 2.17705445825075834731174e-04 1.913e-01
245 2.54300470583329032894099e-04 2.130e-01
245 2.54300477743886715713273e-04 2.679e-02
245 2.54300499465897461830871e-04 4.887e-02
245 2.54300511632548666141052e-04 0.000e+00
245 2.54300542217183317417195e-04 0.000e+00
245 2.54300549393671508017351e-04 2.224e-01
and file2
:
ID Time Energy
43 4.38666558838421885677290e-05 4.788e-01
48 4.77674676992522297732519e-05 3.360e-01
52 4.99184267458611271553633e-05 3.110e-01
203 2.17111190317825032474949e-04 3.425e-01
206 2.17705422992319207490738e-04 3.197e-01
245 2.54300549393671508017351e-04 2.224e-01
I want to read file1
and, if the energy value of a row in file1
exists in file2
, I should print the previous line.
The desired output from the example above is :
43 4.38665978376386365240533e-05 3.215e-02
48 4.77674337753321466689890e-05 1.750e-01
52 4.99184267458611271553633e-05 3.110e-01
203 2.17111189970080955017814e-04 1.685e-01
206 2.17705422992319207490738e-04 3.197e-01
245 2.54300542217183317417195e-04 0.000e+00
The question is: In same ID number, when the energy value of a row in file1 exists in file2, print the row before this one. can anyone kown how to do this by awk? I have tried code :
gawk '
NR==FNR {a[NR]=$0; next }
{
split(a[FNR],flds,FS,seps)
if(flds[1]=$1 && flds[2] <=$2)
print flds}' file1 file2 > flie3
Then using sort | uniq to remove the repeated lines with same id.
Time value is incremental in all files, and once the line with same energy value as file2 is in the first line within same ID number, then only choose this line. like 52 4.99184267458611271553633e-05 3.110e-01
file2
or will there always be one, unique value per id?52 4.99184267458611271553633e-05 3.110e-01
in your desired output? Don't you want to print the previous line?