getting line between 2 patterns

M

mike

hi

i have a file like this:

2004nddjjf99jdlkdf <---- 99 is at character position 11 to 12
2004abcdefghijklnmnopqrstuvwxyz
2004ldfhanvsduhkjgndfnspqiekfnv
2003nmvkdmcnfjfbndmdkvndnmvkdbn
2004nddgdf99dkgfjs

i wanted to get the text in between the 2 lines that have the number
"99" at the
11th and 12th position.

i looked at FAQ ,

perl -ne 'print if /START/ .. /END/' file1 ...

how can i define the START and END such that it must check for
character position 11 and 12 to look for 99 ??

thanks.
 
G

Gunnar Hjalmarsson

mike said:
i have a file like this:

2004nddjjf99jdlkdf <---- 99 is at character position 11 to 12
2004abcdefghijklnmnopqrstuvwxyz
2004ldfhanvsduhkjgndfnspqiekfnv
2003nmvkdmcnfjfbndmdkvndnmvkdbn
2004nddgdf99dkgfjs

i wanted to get the text in between the 2 lines that have the
number "99" at the 11th and 12th position.

i looked at FAQ ,

perl -ne 'print if /START/ .. /END/' file1 ...

how can i define the START and END such that it must check for
character position 11 and 12 to look for 99 ??

/^.{10}99/
 
J

John W. Krahn

mike said:
i have a file like this:

2004nddjjf99jdlkdf <---- 99 is at character position 11 to 12
2004abcdefghijklnmnopqrstuvwxyz
2004ldfhanvsduhkjgndfnspqiekfnv
2003nmvkdmcnfjfbndmdkvndnmvkdbn
2004nddgdf99dkgfjs

i wanted to get the text in between the 2 lines that have the number
"99" at the
11th and 12th position.

i looked at FAQ ,

perl -ne 'print if /START/ .. /END/' file1 ...

how can i define the START and END such that it must check for
character position 11 and 12 to look for 99 ??

perl -ne 'print if /^.{10}99/ ... /^.{10}99/' file1 ...



John
 
A

Anno Siegel

John W. Krahn said:
perl -ne 'print if /^.{10}99/ ... /^.{10}99/' file1 ...

That prints the delimitling lines (the ones that contain "99").
Depending on what the OP means by "in between", it might be
necessary to do

perl -ne 'print if /^.{10}99/ ... /^.{10}99/ and not /^.{10}99/' file1

Anno
 
M

mike

That prints the delimitling lines (the ones that contain "99").
Depending on what the OP means by "in between", it might be
necessary to do

perl -ne 'print if /^.{10}99/ ... /^.{10}99/ and not /^.{10}99/' file1

Anno


thanks for the advise.

I have another requirement

2004nddjjf99jdlkdf <---- first location that have '99' at char pos
11-12
2004abcdefghijklnmnopqrstuvwxyz
2004ldfhanvsduhkjgndfnspqiekfnv
2003nmvkdmcnfjfbndmdkvndnmvkdbn
2004nddgdf99dkgfjs <----------- (2)
2001lksdnvnvkxlxmncxkxlxknlxxcl
1999kldkdkfsksjdkjvkjslkjdflkjl
2002nodmen99ddklss <------------(3)

I wanted to get the text between the first location ( where '99' is at
char pos 11-12 ) and the second location (2), then put the text into a
file.
Then get the text between (2) and (3) and put to another file.
I have no idea how to keep track of the lines that have '99' at pos
11-12.
Any advise? A "psuedocode" would be a great help, cos i want to try to
code the rest of it myself...
thanks...
 
A

Anno Siegel

mike said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in message



thanks for the advise.

I have another requirement

2004nddjjf99jdlkdf <---- first location that have '99' at char pos
11-12
2004abcdefghijklnmnopqrstuvwxyz
2004ldfhanvsduhkjgndfnspqiekfnv
2003nmvkdmcnfjfbndmdkvndnmvkdbn
2004nddgdf99dkgfjs <----------- (2)
2001lksdnvnvkxlxmncxkxlxknlxxcl
1999kldkdkfsksjdkjvkjslkjdflkjl
2002nodmen99ddklss <------------(3)

I wanted to get the text between the first location ( where '99' is at
char pos 11-12 ) and the second location (2), then put the text into a
file.
Then get the text between (2) and (3) and put to another file.
I have no idea how to keep track of the lines that have '99' at pos
11-12.
Any advise? A "psuedocode" would be a great help, cos i want to try to
code the rest of it myself...
thanks...

Your "additional requirement" makes this another problem altogether.

Apparently, you want to switch output files (and ignore the line)
whenever "99" appears in a certain position.

So let's say you want to number the output files "out_01" .. "out_99".
Untested:

my $file = 'out_01';
open my $out, '>', $file or die "Can't create $file: $!";
while ( <$in> ) {
if ( substr( $_, 11, 2) eq '99' ) {
$file ++; # magic string increment
open $out, '>', $file or die "Can't create $file: $!";
} else {
print $out, $_;
}
}

Anno
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top