|
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.
| |
Share:
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.
| | |
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
| | 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: -
open(F,"z.txt") or die "error:$!";
-
while(<F>) {
-
next until /start1/;
-
@rem=<F>;
-
}
-
foreach(@rem){
-
last if(/end1/);
-
print $_;
-
}
-
- Nithin
| | |
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.
| | 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 | | 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
| | |
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.. - open(TEXT, "test.txt");
-
@contents = <TEXT>;
-
-
foreach (@contents){
-
-
if(/$start/ .. /end/) {
-
-
push @bet,$_;
-
-
}
-
}
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. - open(TEXT, "test.txt");
-
@contents = <TEXT>;
-
-
foreach (@contents){
-
-
if(/$start/ .. /end/) {
-
-
push @bet,$_;
-
-
} else {
-
-
push @other,$_;
-
-
}
-
}
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
| | Expert 2GB |
You are a bad boy, a very very bad boy. You have to go to the principles office you bad boy.
| | 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: -
open(TEXT, "output.txt");
-
@contents = <TEXT>;
-
-
foreach (@contents){
-
-
if(/start/ .. /end/) {
-
next if((/start/ )|| (/end/) ||(/begin/));
-
push @bet,$_;
-
-
} else {
-
next if((/start/ )|| (/end/) ||(/begin/));
-
push @other,$_;
-
-
}
-
}
-
print "@other";
-
| | 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
| | |
Hi Kevin,
As your wish..
Regards,
Dheena.
| | |
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
| | 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, -
open(TEXT, "in.txt");
-
open(NEW, ">>out.txt");
-
my @data=<TEXT>;
-
-
foreach(@data) {
-
print NEW unless($seen{$_}++); ## append unless the data exists already
-
}
-
- Nithin
| | Expert 2GB |
You can avoid duplicate lines while appending/writing to a file by making use of a hash. For example, -
open(TEXT, "in.txt");
-
open(NEW, ">>out.txt");
-
my @data=<TEXT>;
-
-
foreach(@data) {
-
print NEW unless($seen{$_}++); ## append unless the data exists already
-
}
-
- 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
| | |
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.
| | |
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 - foreach $lines (@lines){
-
if($lines =~ m/start1/){
-
until ($lines =~ m/end1/){
-
print OUTPUT "$lines";
-
}
-
}
-
}
| | |
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.
| | 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,
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by Jesse Noller |
last post: by
|
22 posts
views
Thread by Ling Lee |
last post: by
|
1 post
views
Thread by j |
last post: by
|
6 posts
views
Thread by ivan.perak@gmail.com |
last post: by
|
7 posts
views
Thread by peraklo@gmail.com |
last post: by
| |
7 posts
views
Thread by Gustaf |
last post: by
|
3 posts
views
Thread by Robert |
last post: by
| | | | | | | | | | | |