pattern matching and grabing value sub

J

J M

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!
 
T

Tad McClellan

J M said:
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 ) {
 
G

gnari

J M said:
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
 
G

gibbering poster

J M said:
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;
 
J

J M

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

Walter Roberson

: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
 
J

J M

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.

=======================================================================
 
T

Tad McClellan

[ Please do not top-post. ]


J M said:
#!/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 ]
 
W

Walter Roberson

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

gnari

Walter Roberson said:
: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
 
A

Anno Siegel

Robin said:
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
 
G

G Klinedinst

J M said:
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
 
J

Joe Smith

J said:
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
 
J

Joe Smith

Joe said:
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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top