LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-07-2011, 06:28 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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!

Last edited by cristalp; 11-07-2011 at 06:36 AM.
 
Old 11-07-2011, 07:36 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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?
 
Old 11-07-2011, 07:50 AM   #3
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.

Last edited by cristalp; 11-07-2011 at 08:01 AM.
 
Old 11-07-2011, 07:57 AM   #4
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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?

Last edited by cristalp; 11-07-2011 at 07:58 AM.
 
Old 11-07-2011, 08:39 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print nth line after the line which matches the string cristalp Programming 7 10-27-2011 02:53 PM
[SOLVED] Copy and replacing specific line from file1 to file2 line by line vjramana Programming 10 03-28-2011 08:49 AM
Editing a specific group of lines by line number? SirTristan Linux - Newbie 5 09-29-2009 08:30 PM
how to print a specific line number with cat new_2_unix Linux - Newbie 2 10-14-2007 11:47 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration