getting line between 2 patterns

Discussion in 'Perl Misc' started by mike, Aug 4, 2004.

  1. mike

    mike Guest

    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.
     
    mike, Aug 4, 2004
    #1
    1. Advertising

  2. mike wrote:
    > 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/

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 4, 2004
    #2
    1. Advertising

  3. mike wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Aug 4, 2004
    #3
  4. mike

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > mike wrote:
    > >
    > > 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 ...


    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
     
    Anno Siegel, Aug 4, 2004
    #4
  5. mike

    mike Guest

    -berlin.de (Anno Siegel) wrote in message news:<ceqb79$iqg$-Berlin.DE>...
    > John W. Krahn <> wrote in comp.lang.perl.misc:
    > > mike wrote:
    > > >
    > > > 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 ...

    >
    > 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...
     
    mike, Aug 4, 2004
    #5
  6. mike

    Anno Siegel Guest

    mike <> wrote in comp.lang.perl.misc:
    > -berlin.de (Anno Siegel) wrote in message
    > news:<ceqb79$iqg$-Berlin.DE>...
    > > John W. Krahn <> wrote in comp.lang.perl.misc:
    > > > mike wrote:
    > > > >
    > > > > 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 ...

    > >
    > > 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...


    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
     
    Anno Siegel, Aug 4, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crichmon
    Replies:
    4
    Views:
    486
    Mabden
    Jul 7, 2004
  2. Replies:
    14
    Views:
    556
    John Machin
    Jun 5, 2006
  3. Amit Rawal
    Replies:
    0
    Views:
    134
    Amit Rawal
    Jun 23, 2009
  4. Marek
    Replies:
    10
    Views:
    231
    Tim Greer
    Sep 19, 2008
  5. ccc31807

    capturing multiple patterns per line

    ccc31807, Feb 5, 2010, in forum: Perl Misc
    Replies:
    15
    Views:
    182
    Peter J. Holzer
    Feb 13, 2010
Loading...

Share This Page