LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   print lines form nth line to mth line which fulfill specific condition (https://www.linuxquestions.org/questions/programming-9/print-lines-form-nth-line-to-mth-line-which-fulfill-specific-condition-912304/)

cristalp 11-07-2011 05:28 AM

print lines form nth line to mth line which fulfill specific condition
 
Dear all,

I have a file like:
Code:

bala-bala
bala-bala
bala-bala
...
bala-bala
OP1  C B 116    -47.050
OP2  U B 116    -44.778
O5'  C B 099    -46.477
O4'  C B 116    -47.664         
C3'  X B 120    -46.440
...
C5'  N B 328    -47.727
C4'  C B 007    -47.671
bala-bala
bala-bala
bala-bala
...
bala-bala

The parts with bala-bala means that the formats of these parts are not regular. The content of these parts are bit mess.

The part in middle with
Code:


OP1  C B 116    -47.050
OP2  U B 116    -44.778
...
C4'  C B 007    -47.671

is the regular part. The 3rd filed is fixed which is always B. The other fields are always changing from line to line.

What I want to do is to print the irregular part as its orignal format and print the middle regular part from the 107th to 211th line of this part.

Please notice that the 107th line is not the 107th line of the whole file but the 107th line just of the middle regular part. The same for 211th line. In this case, the line of "OP1 C B 116 -47.050" would be the first line, then "OP2 U B 116 -44.778" is the second line, and so on.

The output file should look like:
Code:

bala-bala           
bala-bala
...                      <-everything same as input file
bala-bala
OX1  T B 180    -40.190 <- the 107th line of the middle part
K7'  T B 005    -43.602 
...
XC1  M B 104    -40.989 <- the 211th line of the middle part
bala-bala
bala-bala
...                      <-everything same as input file
bala-bala

I am searching some AWK solutions, but it seems not so straightforward for me. Please let me know if you have any ideas. Any reply would be appreciated and I would thank you for your kind help!

grail 11-07-2011 06:36 AM

So the 107 and 211 part made no sense to me :( Also you need to explain how you converted the lines as they are quite radically different from the input file?

cristalp 11-07-2011 06:50 AM

Quote:

Originally Posted by grail (Post 4517942)
So the 107 and 211 part made no sense to me :( Also you need to explain how you converted the lines as they are quite radically different from the input file?

The middle part of the input file is:
Code:

OP1  C B 116    -47.050 
OP2  U B 116    -44.778
O5'  C B 099    -46.477
O4'  C B 116    -47.664         
C3'  X B 120    -46.440
...
C5'  N B 328    -47.727
C4'  C B 007    -47.671

The 107 and 211 are the numbers of two lines in this part. The line "OP1 C B 116 -47.050" is the No.1 line, "OP2 U B 116 -44.778" is the No.2 line, "O5' C B 099 -46.477" is the No.3 line, "O4' C B 116 -47.664" is the No.4 line and so on. There are more than hundreds lines of such format.

I suppose the No.107 line in the middle part would be
Code:

OX1  T B 180    -40.190
and No. 211 line in the middle part would be
Code:

XC1  M B 104    -40.989
So, what I want is print only lines in between of No.107 line and No.211 line. I did not convert the line, I just assume the line at No.107 and No.211 would look like that. Is that clear now? Let me know if I need to do any future explanations.

cristalp 11-07-2011 06:57 AM

Quote:

Originally Posted by grail (Post 4517942)
So the 107 and 211 part made no sense to me :( Also you need to explain how you converted the lines as they are quite radically different from the input file?

OK to make my question easier and clear. I change it a little bit.
I have a input file:
Code:

bala-bala
bala-bala
bala-bala
...
bala-bala
OP1  C B 116    -47.050
OP2  U B 116    -44.778
O5'  C B 099    -46.477
O4'  C B 116    -47.664         
C3'  X B 120    -46.440
...
C5'  N B 328    -47.727
C4'  C B 007    -47.671
bala-bala
bala-bala
bala-bala
...
bala-bala

I want to print irregular part "bala-bala" and parts of the middle regular part from line 2 to line 4. Line No.2 is
Code:

OP2  U B 116    -44.778
line No.4 is
Code:

O4'  C B 116    -47.664
I want to print only lines between these two lines for the middle part.

The output file should be:
Code:

bala-bala
bala-bala
bala-bala
...
bala-bala
OP2  U B 116    -44.778
O5'  C B 099    -46.477
O4'  C B 116    -47.664         
bala-bala
bala-bala
bala-bala
...
bala-bala

Is that clear now?

grail 11-07-2011 07:39 AM

Yes I think I follow now. So print as normal until you find start of middle and then increment a counter till you get to 107 and then print until you get to 211 then wait till
you find the end of middle portion and start printing the rest.


All times are GMT -5. The time now is 03:15 AM.