473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to get the inbetween lines in a file?

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
18 2278
nithinpes
410 Expert 256MB
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
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
410 Expert 256MB
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
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
4,059 Expert 2GB
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
3,509 Expert Mod 2GB
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
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
4,059 Expert 2GB
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
410 Expert 256MB
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
410 Expert 256MB
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
Hi Kevin,

As your wish..

Regards,
Dheena.
Nov 10 '08 #12
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
410 Expert 256MB
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
4,059 Expert 2GB
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
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
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
ahgan
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
174 Expert 100+
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

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

Similar topics

2
by: Jesse Noller | last post by:
I am a relative newbie to python and I am having issues trying to iterate over the lines of a file. I have a text file - foo.bar inside of this file are lines of text: x-3411342 y-1324123...
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
1
Mague
by: Mague | last post by:
Hey, I was wondering if it is possible to get rid of html tags and everything in between it. I am 13 and 2 of my friends from school are making a website. It is going to host of my computer. Im...
7
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
3
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------...
3
anu401
by: anu401 | last post by:
System.out.println()---in this printing statement why we use (.)i.e dot inbetween system.out and out.println? please reply this question was asked in viva session
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.