Reg:parsing a file

S

sivga

hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file with following lines


exact 5
seasad 3
switch text 1.2
new data 20
cd data 10
phase 30
extra 40
same 200
different 500


wat i need do is i neeed to read the file and if the file has (matches)



switch text i need to write switch text to a new file say k2 and the
next four lines new data ,cd data,extra and phase to k2 .other wise no
need to write to the file k2. ,I cannot form the string say new data cd



data and write to a file coz the numbers at the end will differ .How do



i do it ?


while (<AG>) {


if ($_ =~ /exact +([0-9]+)/ ) {
print k1 " $_";


} elsif ( $_ =~ /switch text /) {
print k2 "$_";




} elsif ($_ =~ /same/) {
print k1 "$_";
}


Thanks for the help
 
K

Klaus

sivga said:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file
[snip]

wat i need do is i neeed to read the file
[snip]

i need to write switch text to a new file say k2

[snip]

It is recommended that you begin your perl program with the following
two lines:

use strict;
use warnings;

Secondly, you need to open all your input-file and output-files.
Have a look at "perldoc perlopen" on how to open files.

Personally, I would say:

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
while (<AG>) {
while ( said:
print k1 " $_";
print {$k1} ' ', $_;
print k2 "$_";
print {$k2} $_;
print k1 "$_";
print {$k1} $_;
 
S

sivga

Klaus said:
sivga said:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file
[snip]

wat i need do is i neeed to read the file
[snip]

i need to write switch text to a new file say k2

[snip]

It is recommended that you begin your perl program with the following
two lines:

use strict;
use warnings;

Secondly, you need to open all your input-file and output-files.
Have a look at "perldoc perlopen" on how to open files.

Personally, I would say:

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
while (<AG>) {
while ( said:
print k1 " $_";
print {$k1} ' ', $_;
print k2 "$_";
print {$k2} $_;
print k1 "$_";
print {$k1} $_;

klaus i have already done all those ..i have pasted jus the snippet of
the code . The problem is there a way to specify like if the condition
matches then get next four lines .how do i specify this

thanks for the help
 
S

sivga

sivga said:
Klaus said:
sivga said:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file
[snip]

wat i need do is i neeed to read the file
[snip]

i need to write switch text to a new file say k2

[snip]

It is recommended that you begin your perl program with the following
two lines:

use strict;
use warnings;

Secondly, you need to open all your input-file and output-files.
Have a look at "perldoc perlopen" on how to open files.

Personally, I would say:

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";
while (<AG>) {
while ( said:
print k1 " $_";
print {$k1} ' ', $_;
print k2 "$_";
print {$k2} $_;
print k1 "$_";
print {$k1} $_;

klaus i have already done all those ..i have pasted jus the snippet of
the code . The problem is there a way to specify like if the condition
matches then get next four lines .how do i specify this

thanks for the help

wat i need do is if th eline matches then i need to get the next four
lines an place it in a array ..how do i do it in perl ?
 
K

Klaus

sivga said:
sivga said:
Klaus said:
sivga wrote:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file

[snip]

wat i need do is i neeed to read the file

[snip]

i need to write switch text to a new file say k2

[snip]

It is recommended that you begin your perl program with the following
two lines:

use strict;
use warnings;

Secondly, you need to open all your input-file and output-files.
Have a look at "perldoc perlopen" on how to open files.

Personally, I would say:

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";

while (<AG>) {
while (<$AG>) {

print k1 " $_";
print {$k1} ' ', $_;

print k2 "$_";
print {$k2} $_;

print k1 "$_";
print {$k1} $_;

klaus i have already done all those ..i have pasted jus the snippet of
the code .

then please post some code that you have already done, not something
else which is different to what you have already done.
wat i need do is if th eline matches then i need to get the next four
lines an place it in a array ..how do i do it in perl ?

I will give an example to match the string "abc" (but be aware that you
will get "Use of uninitialized value" warnings if, after a successful
match /abc/, there are less than 4 lines in the file left):

use strict;
use warnings;

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";

my @array;
while (<$AG>) { chomp;
if (/abc/) { do{ my $line = <$AG>; push @array, $line } for (1..4)
}
}

{ local $, = "\n"; print @array, '' }
 
S

sivga

Klaus said:
sivga said:
sivga said:
Klaus wrote:
sivga wrote:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file

[snip]

wat i need do is i neeed to read the file

[snip]

i need to write switch text to a new file say k2

[snip]

It is recommended that you begin your perl program with the following
two lines:

use strict;
use warnings;

Secondly, you need to open all your input-file and output-files.
Have a look at "perldoc perlopen" on how to open files.

Personally, I would say:

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";

while (<AG>) {
while (<$AG>) {

print k1 " $_";
print {$k1} ' ', $_;

print k2 "$_";
print {$k2} $_;

print k1 "$_";
print {$k1} $_;

klaus i have already done all those ..i have pasted jus the snippet of
the code .

then please post some code that you have already done, not something
else which is different to what you have already done.
wat i need do is if th eline matches then i need to get the next four
lines an place it in a array ..how do i do it in perl ?

I will give an example to match the string "abc" (but be aware that you
will get "Use of uninitialized value" warnings if, after a successful
match /abc/, there are less than 4 lines in the file left):

use strict;
use warnings;

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";

my @array;
while (<$AG>) { chomp;
if (/abc/) { do{ my $line = <$AG>; push @array, $line } for (1..4)
}
}

{ local $, = "\n"; print @array, '' }


thanks klaus for the help actually i was trying different ways to
handle this .thanks for the help
 
S

sivga

sivga said:
Klaus said:
sivga said:
sivga wrote:
Klaus wrote:
sivga wrote:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file

[snip]

wat i need do is i neeed to read the file

[snip]

i need to write switch text to a new file say k2

[snip]

It is recommended that you begin your perl program with the following
two lines:

use strict;
use warnings;

Secondly, you need to open all your input-file and output-files.
Have a look at "perldoc perlopen" on how to open files.

Personally, I would say:

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";
open my $k1, '>', 'k1.txt' or die "Error >k1.txt ($!)";
open my $k2, '>', 'k2.txt' or die "Error >k2.txt ($!)";

while (<AG>) {
while (<$AG>) {

print k1 " $_";
print {$k1} ' ', $_;

print k2 "$_";
print {$k2} $_;

print k1 "$_";
print {$k1} $_;

klaus i have already done all those ..i have pasted jus the snippet of
the code .

then please post some code that you have already done, not something
else which is different to what you have already done.
wat i need do is if th eline matches then i need to get the next four
lines an place it in a array ..how do i do it in perl ?

I will give an example to match the string "abc" (but be aware that you
will get "Use of uninitialized value" warnings if, after a successful
match /abc/, there are less than 4 lines in the file left):

use strict;
use warnings;

open my $AG, '<', 'ag.txt' or die "Error <ag.txt ($!)";

my @array;
while (<$AG>) { chomp;
if (/abc/) { do{ my $line = <$AG>; push @array, $line } for (1..4)
}
}

{ local $, = "\n"; print @array, '' }


thanks klaus for the help actually i was trying different ways to
handle this .thanks for the help


klaus thanks for the help but can help in undersatnding how does the
for loop work here when u said for (1..4) Can you pls help here
..thanks
 
T

Tad McClellan

sivga said:
hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file with following lines


exact 5
seasad 3
switch text 1.2
new data 20
cd data 10
phase 30
extra 40
same 200
different 500


wat i need do is i neeed to read the file and if the file has (matches)



switch text i need to write switch text to a new file say k2 and the
next four lines new data ,cd data,extra and phase to k2 .other wise no
need to write to the file k2. ,I cannot form the string say new data cd



data and write to a file coz the numbers at the end will differ .How do



i do it ?


while (<AG>) {


if ($_ =~ /exact +([0-9]+)/ ) {
print k1 " $_";


} elsif ( $_ =~ /switch text /) {
print k2 "$_";


print k2 scalar(<AG>); # print the next 4 lines
print k2 scalar(<AG>);
print k2 scalar(<AG>);
 
T

Tad McClellan

[ Please trim quoted text that is not needed to establish the
context for your comments.
]

how does the
for loop work here when u said for (1..4)


See the "Foreach Loops" section in:

perldoc perlsyn
 
M

Mumia W.

hello experts,

Iam new to perl .Can you pls suggest how to do this this is wat iam
trying to do i have a text file with following lines


exact 5
seasad 3
switch text 1.2
new data 20
cd data 10
phase 30
extra 40
same 200
different 500


wat i need do is i neeed to read the file and if the file has (matches)



switch text i need to write switch text to a new file say k2 and the
next four lines new data ,cd data,extra and phase to k2 .other wise no
need to write to the file k2. ,I cannot form the string say new data cd



data and write to a file coz the numbers at the end will differ .How do



i do it ?


while (<AG>) {


if ($_ =~ /exact +([0-9]+)/ ) {
print k1 " $_";


} elsif ( $_ =~ /switch text /) {
print k2 "$_";




} elsif ($_ =~ /same/) {
print k1 "$_";
}


Thanks for the help

It was incredibly difficult to understand you, but I think I
know what you're getting at (thanks to the code you posted).

It is probably going to be easier for you to use Tie::File to
make the file you are reading look like an array. Then reading
the next four lines should be easy:

The following is NOT an example of such because I decided to
put the data directly in my program:

use strict;
use warnings;
my (@AR, @K1, @K2);
@AR = q{
exact 5
seasad 3
switch text 1.2
new data 20
cd data 10
phase 30
extra 40
same 200
different 500
} =~ m/^ *(\w.*)$/mg;

local $\ = "\n";
for my $nx (0..$#AR) {
$_ = $AR[$nx];
if (/switch text/) {
push @K2, @AR[$nx..$nx+4];
} elsif (/exact/) {
push @K1, $_;
} elsif (/same/) {
push @K1, $_;
}
}

print join "\n", @K1;
print '----------------------------';
print join "\n", @K2;

__END__

Read 'perldoc perltie' and 'perldoc Tie::File' to see how to
use tied file arrays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top