By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

How to get the inbetween lines in a file?

P: 13
Hi Every One,

I am having a file like this..

start
bla bla bla1
start1
bla bla bla2
bla bla bla3
end1
bla bla bla4
bla bla bla5
start2
bla bla bla6
bla bla bla7
end2
bla bla bla8
end
from this i want to get the lines inbetween start1 and end1 i.e what i want is as follows

bla bla bla2
bla bla bla3

like this the file has n number of sets..

can any guide me regarding this?

Thanks and Regards,
Dheena.
Oct 31 '08 #1
Share this Question
Share on Google+
18 Replies


nithinpes
Expert 100+
P: 410
Hi Every One,

I am having a file like this..

start
bla bla bla1
start1
bla bla bla2
bla bla bla3
end1
bla bla bla4
bla bla bla5
start2
bla bla bla6
bla bla bla7
end2
bla bla bla8
end
from this i want to get the lines inbetween start1 and end1 i.e what i want is as follows

bla bla bla2
bla bla bla3

like this the file has n number of sets..

can any guide me regarding this?

Thanks and Regards,
Dheena.
What have you tried so far? You can make use of regex to skip the lines until a criteria is met and to print lines until other criteria is met.
Oct 31 '08 #2

P: 13
Hi Nithinpes,

yes i know the regular expressions..

But the regular expressions will work with a single string..

what i need is, i want to get the lines in between those lines(start1 and end1), like that its having n number..

I want to get those strings in a array of string (i.e) inbetween those two lines..

Thanks and Regards,
Dheenaalex
Oct 31 '08 #3

nithinpes
Expert 100+
P: 410
Hi Nithinpes,

yes i know the regular expressions..

But the regular expressions will work with a single string..

what i need is, i want to get the lines in between those lines(start1 and end1), like that its having n number..

I want to get those strings in a array of string (i.e) inbetween those two lines..

Thanks and Regards,
Dheenaalex
To get lines between start1 and end1(e.g), try the following:
Expand|Select|Wrap|Line Numbers
  1. open(F,"z.txt") or die "error:$!";
  2. while(<F>) {
  3. next until /start1/;
  4. @rem=<F>;
  5. }
  6. foreach(@rem){
  7. last if(/end1/);
  8. print $_;
  9. }
  10.  
- Nithin
Oct 31 '08 #4

P: 13
Hi Nithin,

This will work only for the start 1 and end1... actually i want between start2 and end2 like this n number of blocks..

Let me give a detailed explation, abt what i need..

My file is as follows

begin
bla bla bla1
bla bla bla2
start
bla bla bla3
bla bla bla4
end
bla bla bla5
start
bla bla bla6
bla bla bla7
bla bla bla8
end
bla bla bla9
end

I want the output as follows..

In the first string array i want bla bla bla3, bla bla bla4
In the second string array i want bla bla bla6, bla bla bla7 and bla bla bla8.

Like this it may have n number of start and end block.

Thanks and Regards,
Dheena.
Oct 31 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Why is this person allowed to continue posting?

He shows no effort.

He has never answered if this is school work.

His question is answered in the perl FAQs that come with perl but can't even be bothered to read them? Probably doesn't even know they exist.

http://perldoc.perl.org/perlfaq6.html
Oct 31 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
dheenaalex,

To get help, you are going to show us that you are actually trying to write code for your issue. Granted, Nithinpes did provide you some code to give you a boost, but you need to please post your code here (above and beyond what you were given by Nithinpes) so that we can help you.

Unless we know what you are trying and that you are actually coding and not expecting the answer, then I don't know what we can do much more for you.

Regards,

Jeff
Oct 31 '08 #7

P: 13
Hi Nithin,

Now I got that how to get the lines inbetween two lines.. now i want other than that lines..
.

The code for inbetween lines are as follows..

Expand|Select|Wrap|Line Numbers
  1.  open(TEXT, "test.txt");
  2.     @contents = <TEXT>;
  3.  
  4.         foreach (@contents){
  5.  
  6.             if(/$start/ .. /end/) {
  7.  
  8.                 push @bet,$_;
  9.  
  10.             }                         
  11.         }   

So i am trying to get the lines other than that inbetween lines, for that i am using the following code, but its not working.. can you guide me regarding this.

Expand|Select|Wrap|Line Numbers
  1.  open(TEXT, "test.txt");
  2.     @contents = <TEXT>;
  3.  
  4.         foreach (@contents){
  5.  
  6.             if(/$start/ .. /end/) {
  7.  
  8.                 push @bet,$_;
  9.  
  10.             }    else {
  11.  
  12.                     push @other,$_;
  13.  
  14.             }                     
  15.         }   
This was the file what i am having...

begin
bla bla bla1
bla bla bla2
start
bla bla bla3
bla bla bla4
end
bla bla bla5
start
bla bla bla6
bla bla bla7
bla bla bla8
end
bla bla bla9
end

Now what i want is other than inbetween lines, is as follows..

bla bla bla1
bla bla bla2

bla bla bla5

bla bla bla9


Then one thing Nithin, one guy was always scolding me.. Posting points against me..Pls tel him to stop posting like this..., if i am telling means he wont..

Regards,
Dheena
Nov 9 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
You are a bad boy, a very very bad boy. You have to go to the principles office you bad boy.
Nov 9 '08 #9

nithinpes
Expert 100+
P: 410
Hi Nithin,

Now I got that how to get the lines inbetween two lines.. now i want other than that lines..

Regards,
Dheena
The code you posted works fine for me, expect for the fact that includes 'begin' and 'end' lines. If you want to skip those, use a next statement:

Expand|Select|Wrap|Line Numbers
  1. open(TEXT, "output.txt"); 
  2.     @contents = <TEXT>; 
  3.  
  4.         foreach (@contents){ 
  5.  
  6.             if(/start/ .. /end/) { 
  7.                 next if((/start/ )|| (/end/) ||(/begin/));
  8.                 push @bet,$_; 
  9.  
  10.             }    else { 
  11.                     next if((/start/ )|| (/end/) ||(/begin/));
  12.                     push @other,$_; 
  13.  
  14.             }                      
  15.         }    
  16. print "@other";
  17.  
Nov 10 '08 #10

nithinpes
Expert 100+
P: 410
Hi Nithin,

Then one thing Nithin, one guy was always scolding me.. Posting points against me..Pls tel him to stop posting like this..., if i am telling means he wont..

Regards,
Dheena
That is so childish....
You never replied to the question in your previous thread on whether it was your homework. Also, in this thread, you were reluctant to post your code.
Please remember in future,that you need to post your code to get replies/ suggestions.

Regards,
Nithin
Nov 10 '08 #11

P: 13
Hi Kevin,

As your wish..

Regards,
Dheena.
Nov 10 '08 #12

P: 13
HI Nithin,

ok.. Just for fun i posted like that.

Sorry for that..

Ok now what i want is i am appending a file ..

In the run time i want to check for the data..

Actually what i need is while appending data in to a file, i want to check weather the data is already or not..

Is it possible in perl?



Regards,
Dheena
Nov 10 '08 #13

nithinpes
Expert 100+
P: 410
HI Nithin,

ok.. Just for fun i posted like that.

Sorry for that..

Ok now what i want is i am appending a file ..

In the run time i want to check for the data..

Actually what i need is while appending data in to a file, i want to check weather the data is already or not..

Is it possible in perl?



Regards,
Dheena
You can avoid duplicate lines while appending/writing to a file by making use of a hash. For example,
Expand|Select|Wrap|Line Numbers
  1. open(TEXT, "in.txt"); 
  2. open(NEW, ">>out.txt");
  3. my @data=<TEXT>;
  4.  
  5. foreach(@data) {
  6.    print NEW unless($seen{$_}++); ## append unless the data exists already
  7. }
  8.  
- Nithin
Nov 10 '08 #14

KevinADC
Expert 2.5K+
P: 4,059
You can avoid duplicate lines while appending/writing to a file by making use of a hash. For example,
Expand|Select|Wrap|Line Numbers
  1. open(TEXT, "in.txt"); 
  2. open(NEW, ">>out.txt");
  3. my @data=<TEXT>;
  4.  
  5. foreach(@data) {
  6.    print NEW unless($seen{$_}++); ## append unless the data exists already
  7. }
  8.  
- Nithin
And that is also covered in the perl FAQs. Its obvious this guy is way lazy, but its not really my business if you wish to continue helping him.

Kevin
Nov 10 '08 #15

P: 13
Hi Nithin,

I need to check in the writing file not in reading file..

Actually i am spliting an input file and writing it in different files, at that time i want to avoid the duplication. So how can i check that writing file values?

Is that one is possible in Perl?

Thanks and regards,
Dheena.
Nov 10 '08 #16

P: 1
I need help with something similar, what if start1 and end1 happens more than 1 time?

start
bla bla bla1
start1
bla bla bla2
bla bla bla3
end1
bla bla bla4
bla bla bla5
start2
bla bla bla6
bla bla bla7
end2
bla bla bla8
start1
bla bla bla9
bla bla bla10
end1
end


how can I get those lines too? I tried this but I only get an eternal loop printing one line

Expand|Select|Wrap|Line Numbers
  1. foreach $lines (@lines){
  2.   if($lines =~ m/start1/){
  3.   until ($lines =~ m/end1/){ 
  4.   print OUTPUT "$lines";
  5. }
  6. }
  7. }
Nov 18 '08 #17

P: 13
Hi Deragor,

You can consider using a flag. Flag up when you read start1 (and skip to next line using "next") and flag down when you read end1 (and skip to next line using "next"). When the flag is up, print the line. This should work for your case and I know there are more experts out there who can do it more efficiently.
Nov 19 '08 #18

Icecrack
Expert 100+
P: 174
With Every one posting they need help with the same thing then it seems to be homework,

i don't think we should give them any more info,
Nov 19 '08 #19

Post your reply

Sign in to post your reply or Sign up for a free account.