0

I have two files, the first file contain only 1 field, the second file has more lines than first file and also it has different field in each line.

File 1:

5
15
20

File 2:

3
5
O  1.25  2.30  3.75 
H  1.55  2.45  3.80
H  1.65  2.50  3.90
3
10
O  1.49  2.90  3.52
H  1.89  2.08  3.05
H  1.90  2.18  3.28
3
15
O  1.09  2.29  3.10
H  1.30  2.49  3.69
H  1.54  2.05  3.01
3
20
O  1.91  2.31  3.98 
H  1.64  2.96  3.04
H  1.07  2.49  3.49

in file 2, there is periodic loop, that the first line is number 3 and then second line is shows the number like 5,10,15,20, ... and the 3 lines that contain 4 field.

I want to compare two files and save the data when they have the same number, for example in file 1, first field is 5, I need to print the data from file 2 only!

correspond to the file 1 as an input and file 2 that I need to extract data, I would like to have the output like this:

3
5
O  1.25  2.30  3.75
H  1.55  2.45  3.80
H  1.65  2.50  3.90
3
15
O  1.09  2.29  3.10
H  1.30  2.49  3.69
H  1.54  2.05  3.01
3
20
O  1.91  2.31  3.98 
H  1.64  2.96  3.04
H  1.07  2.49  3.49

How I can have the output like this? when FNR==NR, print the previous field and also FNR==NR and then the next three fields. because they are relating to each other and I would like to have them. Thanks very much, Leila

  • What have you done so far? – techraf Mar 23 '16 at 13:58
  • I removed the blank lines from your example. Please check and make sure that your input and output is shown correctly. – terdon Mar 23 '16 at 16:31
0

You can do something like this with awk:

awk 'FNR==NR{
     a[$1];next
}
(FNR%10==3 && $1 in a){
     nr=FNR+9
}
(FNR<=nr || nr==""){
     print
}' file1 file2

Here I assumed there is one blank line after each line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.