pattern matching and grabing value sub

Discussion in 'Perl Misc' started by J M, Jan 22, 2004.

  1. J M

    J M Guest

    I have a postscript file with follwing information

    more lines.....
    s1 w1 (Queue Number) w2 s2
    1954 7483 mt
    sapf1 sf
    (1234567890) s
    80 su
    1714 9694 mt
    sapf2 sf
    44 sw
    s1 w1 (Queue Date) w2 s2
    more lines.....

    How can I pattern match for "Queue Number" and "Queue Date" and grep
    1234567890. Both Queue Number and Queue Date are static values.

    The return value for sub I am trying to get is 1234567890 without
    brackets.

    TIA!
    J M, Jan 22, 2004
    #1
    1. Advertising

  2. J M <> wrote:
    > I have a postscript file with follwing information
    >
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines.....
    >
    > How can I pattern match for "Queue Number"


    /Queue Number/

    > and "Queue Date"


    /Queue Date/

    > and grep
    > 1234567890.


    my($num) = grep /1234567890/, @lines;


    > The return value for sub I am trying to get is 1234567890 without
    > brackets.



    Hmmm. I don't see how any of your earlier questions will help solve
    that problem.

    But _why_ is 1234567890 the thing that should be returned?

    What distinguishes it from the parts you don't want?


    If you can give a clear Requirements Specification, then someone
    can surely show you how to do it in Perl.


    use PSI::ESP;

    Q:
    How can I return the contents of the first parens that do not
    contain "Queue Number" or "Queue Date"?

    A:
    while ( $ps =~ /\(([^)]*)\)/g ) {
    next if $1 eq 'Queue Number' or $1 eq 'Queue Date';
    print "matched '$1'\n";
    }

    or, better:

    while ( $ps =~ / \( ( [^)]* ) \) /xg ) {


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 22, 2004
    #2
    1. Advertising

  3. J M

    gnari Guest

    "J M" <> wrote in message
    news:...
    > I have a postscript file with follwing information
    >

    [snipped a few lines of postscript that need to be matched]
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines.....
    >
    > How can I pattern match for "Queue Number" and "Queue Date" and grep
    > 1234567890. Both Queue Number and Queue Date are static values.
    >


    You omit to say what you have tried, and why it failed.

    as this is postscript, and likely to be large, I will not suggest
    slurping and using m//s

    you might just want to loop-read lines of the file
    until /Queue Number/, then read more lines until
    /(\d+)\s+s/

    gnari
    gnari, Jan 22, 2004
    #3
  4. "J M" <> wrote in message
    news:...
    > I have a postscript file with follwing information
    >
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines.....
    >
    > How can I pattern match for "Queue Number" and "Queue Date" and grep
    > 1234567890. Both Queue Number and Queue Date are static values.
    >
    > The return value for sub I am trying to get is 1234567890 without
    > brackets.
    >
    > TIA!


    Note sure what the real requirements of your search are, but something
    like this (untested) should get you by:

    print $1 if /Queue Number.*?\((\d+)\).*?Queue Date/s;
    gibbering poster, Jan 22, 2004
    #4
  5. J M

    Robin Guest

    use this /mathch|match2/, the pipe "|" is used as an "or" operator for
    matches.

    --
    Regards,
    Robin
    --

    --

    "J M" <> wrote in message
    news:...
    > I have a postscript file with follwing information
    >
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines.....
    >
    > How can I pattern match for "Queue Number" and "Queue Date" and grep
    > 1234567890. Both Queue Number and Queue Date are static values.
    >
    > The return value for sub I am trying to get is 1234567890 without
    > brackets.
    >
    > TIA!
    Robin, Jan 22, 2004
    #5
  6. J M

    J M Guest

    The 1234567890 is a random number.

    Thanks!

    "J M" <> wrote in message
    news:...
    > I have a postscript file with follwing information
    >
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines.....
    >
    > How can I pattern match for "Queue Number" and "Queue Date" and grep
    > 1234567890. Both Queue Number and Queue Date are static values.
    >
    > The return value for sub I am trying to get is 1234567890 without
    > brackets.
    >
    > TIA!
    J M, Jan 23, 2004
    #6
  7. J M

    J M Guest

    I am going to rephrase this request:

    How can I grab dynamic digit value (for in this example 1234567890) between
    "Queue Number" and "Queue Date" static keys? So I have to find
    "(NNNNNNNNN)" value that is in a line between (Queue Number) and (Queue
    Date).

    > I have a postscript file with follwing information
    >
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines.....
    >
    > How can I pattern match for "Queue Number" and "Queue Date" and grep
    > 1234567890. Both Queue Number and Queue Date are static values.
    >
    > The return value for sub I am trying to get a dynamic value (digits in

    brackets) 1234567890 without brackets.
    >
    > TIA!
    J M, Jan 23, 2004
    #7
  8. In article <2t0Qb.7722$>,
    J M <> wrote:
    :I am going to rephrase this request:

    :How can I grab dynamic digit value (for in this example 1234567890) between
    :"Queue Number" and "Queue Date" static keys? So I have to find
    :"(NNNNNNNNN)" value that is in a line between (Queue Number) and (Queue
    :Date).

    :> I have a postscript file with follwing information


    :> s1 w1 (Queue Number) w2 s2
    :> 1954 7483 mt
    :> sapf1 sf
    :> (1234567890) s
    :> 80 su
    :> 1714 9694 mt
    :> sapf2 sf
    :> 44 sw
    :> s1 w1 (Queue Date) w2 s2

    local $\; $_ = <>; #slurp the whole file

    @Qnos = m/Queue Number\.*\((\d+)\).*Queue Date/sg; #match what you want
    --
    Caution: A subset of the statements in this message may be
    tautologically true.
    Walter Roberson, Jan 23, 2004
    #8
  9. J M

    J M Guest

    Sounds like the special characters and spaces in PS file causing major
    problem for me:

    #!/usr/bin/perl

    $PSF='/my/test.ps';

    open(PSFI, "$PSF") || writeLog("$PSF open failed!");

    @pslines = <PSFI>;

    close(PSFI);

    @pslines =~ tr\-_/A-Za-z0-9##cd;

    @pslines = m/Queue\ Number\.*\((\d+)\).*Queue\ Date/sg;

    print "$pslines";



    =============================================================

    syntax error at get-queue-num.pl line 10, near "@pslines = m/Queue
    Number\.*\("

    Backslash found where operator expected at get-queue-num.pl line 10, near
    ")\"

    (Missing operator before \?)

    Bareword found where operator expected at get-queue-num.pl line 10, near
    "*Queue Date"

    (Missing operator before Date?)

    syntax error at get-queue-num.pl line 14, at EOF

    Execution of get-queue-num.pl aborted due to compilation errors.

    =======================================================================

    "J M" <> wrote in message
    news:xGZPb.7699$...
    > The 1234567890 is a random number.
    >
    > Thanks!
    >
    > "J M" <> wrote in message
    > news:...
    > > I have a postscript file with follwing information
    > >
    > > more lines.....
    > > s1 w1 (Queue Number) w2 s2
    > > 1954 7483 mt
    > > sapf1 sf
    > > (1234567890) s
    > > 80 su
    > > 1714 9694 mt
    > > sapf2 sf
    > > 44 sw
    > > s1 w1 (Queue Date) w2 s2
    > > more lines.....
    > >
    > > How can I pattern match for "Queue Number" and "Queue Date" and grep
    > > 1234567890. Both Queue Number and Queue Date are static values.
    > >
    > > The return value for sub I am trying to get is 1234567890 without
    > > brackets.
    > >
    > > TIA!

    >
    >
    J M, Jan 23, 2004
    #9
  10. [ Please do not top-post. ]


    J M <> wrote:

    > #!/usr/bin/perl


    You should ask for all of the help that you can get:

    use strict;
    use warnings;


    > $PSF='/my/test.ps';
    >
    > open(PSFI, "$PSF") || writeLog("$PSF open failed!");

    ^ ^
    ^ ^ a useless use of double quotes

    Let's fix that, and work the value of $! into the function call:

    open(PSFI, $PSF) || writeLog("$PSF open failed! $!");


    > @pslines = <PSFI>;
    >
    > close(PSFI);
    >
    > @pslines =~ tr\-_/A-Za-z0-9##cd;
    >
    > @pslines = m/Queue\ Number\.*\((\d+)\).*Queue\ Date/sg;
    >
    > print "$pslines";
    >
    >
    >
    >=============================================================
    >
    > syntax error at get-queue-num.pl line 10, near "@pslines = m/Queue



    Remove one line at a time from your program, and re-run it.

    If the problem is still there put that line back in, and
    remove the next line, run it again.

    When the problem goes away, look closely at the line you just
    deleted (you _are_ keeping backups, aren't you?).

    Fix the syntax error in that line. All better.



    [ snip upside-down quoted text ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 23, 2004
    #10
  11. In article <kW1Qb.10761$>,
    J M <> wrote:
    :Sounds like the special characters and spaces in PS file causing major
    :problem for me:

    :mad:pslines =~ tr\-_/A-Za-z0-9##cd;

    You are attempting to use backslash as the pattern delimeter for
    tr, but you don't match the backslash until a later line.

    You didn't use warnings; neh?


    :mad:pslines = m/Queue\ Number\.*\((\d+)\).*Queue\ Date/sg;

    @pslines = m/Queue Number.*\((\d+)\).*Queue Date/sg;

    There is no reason to backslash the space, and you definitely don't
    want to backslash the period.
    --
    "The human genome is powerless in the face of chocolate."
    -- Dr. Adam Drewnowski
    Walter Roberson, Jan 23, 2004
    #11
  12. J M

    gnari Guest

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:buq4ue$bjo$...
    > In article <2t0Qb.7722$>,
    > J M <> wrote:
    > :I am going to rephrase this request:


    [snipped request that i think most of us got the first time]

    > local $\; $_ = <>; #slurp the whole file


    I just hope your postscript file does get too large

    >
    > @Qnos = m/Queue Number\.*\((\d+)\).*Queue Date/sg; #match what you want


    apart from the fact that you have a quoted perion after 'Number', this will
    only
    work as long as there is only one such item in the file. consider:
    Queue Number (123) Queue Date (abc) Queue Number (124) Queue Date

    I suggest (untested), if many occurances to be found:
    @Qnos = m/Queue Number.*?\((\d+)\).*?Queue Date/sg; #match what you want

    or if only one to be found:
    ($num) = m/Queue Number.*?\((\d+)\).*?Queue Date/s; #match what you want

    gnari
    gnari, Jan 23, 2004
    #12
  13. J M

    Anno Siegel Guest

    Robin <> wrote in comp.lang.perl.misc:

    > use this /mathch|match2/, the pipe "|" is used as an "or" operator for
    > matches.


    Top-posting some sloppily spelled Perl-like drivel that has nothing to do
    with the question isn't going to win you points.

    [snip fullquote]

    Anno
    Anno Siegel, Jan 23, 2004
    #13
  14. J M

    G Klinedinst Guest

    "J M" <> wrote in message news:<kW1Qb.10761

    > Sounds like the special characters and spaces in PS file causing major


    [snip]

    > =============================================================
    >
    > syntax error at get-queue-num.pl line 10, near "@pslines = m/Queue
    > Number\.*\("
    >
    > Backslash found where operator expected at get-queue-num.pl line 10, near
    > ")\"
    >
    > (Missing operator before \?)
    >
    > Bareword found where operator expected at get-queue-num.pl line 10, near
    > "*Queue Date"
    >
    > (Missing operator before Date?)
    >
    > syntax error at get-queue-num.pl line 14, at EOF
    >
    > Execution of get-queue-num.pl aborted due to compilation errors.
    >
    > =======================================================================


    I am by no means an expert unlike many of the people here, who have
    already given you some great solutions. I will however recommend, as
    one student to another, that you use "perl -c" on your scripts before
    trying to run them, to check your syntax. Your comment indicates you
    think the problem is with the datafile, not the code, but you would
    know for sure that wasn't the case if you tried to check the syntax
    first. I use it on everything I write, including "-w" and "use strict"
    and all of those tools help immensely.

    BTW, are we to assume that there are no nested examples of what you
    are searching for? Just curious b/c I was going to write my own
    solution and post it up here to get sliced and diced, oops, I mean
    constructively criticized.

    -Greg
    G Klinedinst, Jan 23, 2004
    #14
  15. J M

    Joe Smith Guest

    J M wrote:

    > I am going to rephrase this request:
    >
    > How can I grab dynamic digit value (for in this example 1234567890) between
    > "Queue Number" and "Queue Date" static keys? So I have to find
    > "(NNNNNNNNN)" value that is in a line between (Queue Number) and (Queue
    > Date).


    Have you checked the ".." operator?

    Range Operators

    Binary ".." is the range operator, which is really two different
    operators depending on the context. In scalar context, ".." returns
    a boolean value. The operator is bistable, like a flip-flop, and
    emulates the line-range (comma) operator of sed, awk, and various
    editors.

    jms@mathras> cat temp
    more lines.....
    s1 w1 (Queue Number) w2 s2
    1954 7483 mt
    sapf1 sf
    (1234567890) s
    80 su
    1714 9694 mt
    sapf2 sf
    44 sw
    s1 w1 (Queue Date) w2 s2
    more lines...
    more lines...
    s1 w1 (Queue Number) w2 s2
    1954 7483 mt
    sapf1 sf
    (9876543210) s
    80 su
    1714 9694 mt
    sapf2 sf
    44 sw
    s1 w1 (Queue Date) w2 s2
    more lines...

    mathras> perl temp.pl temp
    Found number 1234567890 at line 5
    Found number 9876543210 at line 16

    --
    I love my TiVo - http://www.inwap.com/u/joe/tivo/
    Joe Smith, Jan 26, 2004
    #15
  16. J M

    Joe Smith Guest

    Joe Smith wrote:

    > J M wrote:
    >
    >> I am going to rephrase this request:
    >>
    >> How can I grab dynamic digit value (for in this example 1234567890)
    >> between
    >> "Queue Number" and "Queue Date" static keys? So I have to find
    >> "(NNNNNNNNN)" value that is in a line between (Queue Number) and (Queue
    >> Date).

    >
    >
    > Have you checked the ".." operator?
    >
    > Range Operators
    >
    > Binary ".." is the range operator, which is really two different
    > operators depending on the context. In scalar context, ".." returns
    > a boolean value. The operator is bistable, like a flip-flop, and
    > emulates the line-range (comma) operator of sed, awk, and various
    > editors.
    >
    > jms@mathras> cat temp
    > more lines.....
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (1234567890) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines...
    > more lines...
    > s1 w1 (Queue Number) w2 s2
    > 1954 7483 mt
    > sapf1 sf
    > (9876543210) s
    > 80 su
    > 1714 9694 mt
    > sapf2 sf
    > 44 sw
    > s1 w1 (Queue Date) w2 s2
    > more lines...
    >
    > mathras> perl temp.pl temp
    > Found number 1234567890 at line 5
    > Found number 9876543210 at line 16


    Oops. Forgot to include the code.

    mathras> cat temp.pl
    while(<>) {
    if (/\(Queue Number\)/ .. /\(Queue Date\)/) {
    print "Found number $1 at line $.\n" if /\((\d+)\)/;
    }
    }


    -Joe

    --
    I love my TiVo - http://www.inwap.com/u/joe/tivo/
    Joe Smith, Jan 26, 2004
    #16
    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. Ben
    Replies:
    2
    Views:
    886
  2. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,024
    Lawrence D'Oliveiro
    May 20, 2011
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    233
    Marc Bissonnette
    Jan 13, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    227
    Xicheng Jia
    May 3, 2007
  5. Kerberos

    Grabing content from server

    Kerberos, Jan 18, 2005, in forum: Javascript
    Replies:
    3
    Views:
    75
    Martin Honnen
    Jan 18, 2005
Loading...

Share This Page