Operating System - OpenVMS
1827707 Members
2594 Online
109967 Solutions
New Discussion

Re: How to extract string from command output?

 
SOLVED
Go to solution
Hein van den Heuvel
Honored Contributor

Re: How to extract string from command output?

Andrew,

Maybe it is time to review the task you are trying to accomplish and perhaps provide a sample input file, appended as .TXT to a reply.

The VMS SEARCH tool would be good to select a few lines to a new /intermediate output file.
But it looks like you needs data from several lines, and interpret each line.
In that case I would just read teh whole file with DCL and pick and choose as you go.

Then again, I would not really do that in DCL but in PERL instead.

Based on the text line presented earlier i would write a perl program like:

--- freespace.pl ------
while (<>) {
$main = $1 if /MAIN Memory.*\s(\d+)/;
$VSM = $1 if /Memory free.*\s(\d+)/;
$int = $1 if /Program fil.*\s(\d+)/;
$pat = $1 if /Pattern fil.*\s(\d+)/;
}
$problem .= " Main" if ($main < $int);
$problem .= " Pattern" if ($VSM < $pat);
$ENV{freespace} = $problem;
--------------------

And I would use it in a DCL procedure like:

$PERL freespace.pl your-file.txt
$IF F$TRNLNM("freespace").NES.""
$THEN
$ WRITE SYS$OUTPUT "Insufficient tester memory space! (", F$TRNLNM("freespace"), ")"
$ GOTO ERROR34
$ ELSE write temp "Free space check OK."
$ ENDIF


fwiw,
Hein.
Robert_Boyd
Respected Contributor

Re: How to extract string from command output?

Here is another way to do this with PIPE and SEARCH:

This example shows how to get the begin and end dates for an errorlog file into a logical name and then into a symbol:

$ SUMMARIZE = "DIAGNOSE/TRANSLATE/SUMMARY"
$ pipe summarize 'errlog_file' | search sys$input 'date_string' | -
( read sys$pipe d1 ; read sys$pipe d2 ; -
ds=""""+f$ele(dtelem," ",f$edi(d1,"TRIM,COMPRESS"))+","+f$ele(dtelem," ",f$edi(d2,"TRIM,COMPRESS"))+"""" ; -
define/job errdates &ds )
$SYMBOL_ASSIGN:
$ errdates = F$trnlnm("ERRDATES")
$ deassign/job ERRDATES
$ 'start_symbol' == f$element(0,",",errdates)
$ 'end_symbol' == f$element(1,",",errdates)

Master you were right about 1 thing -- the negotiations were SHORT!
Andrew Yip
Advisor

Re: How to extract string from command output?

Hello Jan & John,

Thanks a million!
Your suggestions solves my problem.
It's a pretty good and fast idea.

But, by the way, out of curiousity, does the $status: "%X00000001" & "%X08D78053" mean anything specifically besides 'found' & 'not found'?
=)

Andrew
Andrew Yip
Advisor

Re: How to extract string from command output?

Hello Hein,

Thanks for your help & advice rendered earlier in this post, and thanks for promoting Perl too.

As much as i would like to pick up Perl, but i have limited time and resources to do so now.

Very much for now, i'm pretty stuck with using OVMS to deal with some modifications on my existing scripts. =(

But anyway, you guys are real great! Couldn't have done it without all of you!
You guys made my day!

Andrew
Andrew Yip
Advisor

Re: How to extract string from command output?

Hmm..with regards to my earlier question on how to extract a line from 1 file and replace it on the same line of another line, i'm still having some doubts.

Though Kalle had earlier suggested to use the EXTRACT utility, but i'm sorry i hadn't really tried it out yet.

But anyway, i stumbled upon the open/read/write command which claims to be able to open a file and write records at the beginning of the file, as compared to EOF for the open/append command.

However, when i tried to use this, i was given an error which i believe i must have done it wrongly. Stupid me!
What's the correct way to use the open/read/write command?
Jan van den Ende
Honored Contributor

Re: How to extract string from command output?

Andrew,

$STATUS = %X1 (or zero-filled: %X00000001 ) is the "normal"status for any command, it means "successfully finished without any comments"

ANY other odd numeric status is qualified success ( in case of search: the SEARCH did not encounter any error, it reached the end of file(s) to search, BUT did not find any hits )
Bits 0-3 code the $severity of the message:
0 = Warning
1 = Success
2 = Error
3 = Qualified Success
4 = Fatal

There are potentially 2**32-1 different statusses, so pretty much any condition can have its own status.
Those numbers are segmentised:
0 - 2**16 -1 (16 bits) are "reserved to Digital.
Then some (4 I think) code for the Facility.
All others are available, and can be licensed, by 3rd party.

In sys$message there are several modules that "know the accompanying text and how to present it"
Any 3rd party reserving a range also adds a module.

And it is the responsibility of the (OS-) coders that ANY condition gets its own status, AND those statusses are represented by the MESSAGE servers.

This way VMS does not have the VERY helpful "General Failure" or "An error has occurred" messages. GREAT in tracing trouble!

hth

Proost.

Have one on me.

jpe



Don't rust yours pelled jacker to fine doll missed aches.
Hein van den Heuvel
Honored Contributor

Re: How to extract string from command output?

Jan wrote>>
>> Those numbers are segmentised:
0 - 2**16 -1 (16 bits) are "reserved to Digital.
>> Then some (4 I think) code for the Facility.

Bits 3 - 15 are the 'ident'
Facility starts at bit 16 (RMS is numero uno :-).

DCL already sets symbol $severity to bit 0-2

The other components have to be obtained by masking (.AND.) and shifting (/), or with bit level access.

With OpenVMS 8.3 DCL will also set the symbols $FACILITY and $IDENT to the corresponding bit-vield values.

Andrew >> What's the correct way to use the open/read/write command?

For a simple sequential file which your data appears to be the writing to anything except the end of the file is restricted to UPDATE while maintaining record length.

To replace a first line you may need to do something like:

$OPEN/READ/READ file filename
$READ file record
$len = F$LEN(record)
$space_fill = F$FAO("!#* ",len)
$new_record = F$EXT(0,len,new+space_fill)
$WRITE/UPDATE/SYMB file new_record

hth,
Hein.


Jan van den Ende
Honored Contributor

Re: How to extract string from command output?

Hein,
thank for the corrections.

I also have one on your bit of DCL: :-)

OPEN/READ/READ should of course be

OPEN/READ/WRITE

hth

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Andrew Yip
Advisor

Re: How to extract string from command output?

Hi Hein,

Thanks for your advice.
Can you please kindly explain the following lines? I don't quite understand it. Sorry.

$space_fill = F$FAO("!#* ",len)
$new_record = F$EXT(0,len,new+space_fill)

Andrew
Hein van den Heuvel
Honored Contributor

Re: How to extract string from command output?

[Right, I meant to write open/read/write. ]

Those funny lines are one of several ways to create a new record which has the same length as the original record.

As I indicated, the RMS SYS$UPDATE function which is executed behind the $WRITE/UPDATE requires the record length not to change for a simple sequential file

$space_fill = F$FAO("!#* ",len)

First, check $HELP LEXI F$FAO, and notably the 'directives' section. The documentation is really in the system service manual under SYS$FAO.
Any !xx in the first argument text is a 'directive' more often than not acting upon further arguments. Here the ! says 'something special coming up', the # is for 'count in the next argument', the * repeat the next character (or directive) 'count'- times and then a 'space'.
The result is a string of spaces as long as the original record.

Next comes:

$new_record = F$EXT(0,len,new+space_fill)

Here we append that string of spaces to the new data to be put (in record 1) making is plenty big. Then we just extract the first len bytes making is equal sized.

A less brute-force approach is to calculate how many fill bytes are needed, if the new record is shorter:

$space_filler = F$FAO("!#* ",len - f$len(new))
$new_record = new + space_filler

Best is really to just use F$FAO to create the fixed length output field, as it would also truncate any new data which is too long. So the better solution is:

$new_record = F$FAO("!#AS",F$LEN(record),new)

Try it with tests like:
$ write sys$output "=", F$FAO("!#AS",20,"test"), "="

Actually my preferred inline update is often the left-side-sub-string replace:

symbol-name[offset,size] :=[=] replacement-string

Again, read interactive $HELP for details...

Note, a single line "$HELP := " will not work of course !

More than you wanted to know huh?!

Cheers,
Hein.

Andrew Yip
Advisor

Re: How to extract string from command output?

Hi Hein,

You're the MAN!
Way to go! I believe they don't put you up the list for nothing huh?! =)

Hmm..that's means by hook or by crook, the string to be replaced must be of the same character length with the new string.

This would be a limitation then. What about say, if i need to replace a string of 7 chars with a string of 10 chars, or vice versa? No way? Any alternative to accomplish that?

Andrew
Hein van den Heuvel
Honored Contributor

Re: How to extract string from command output?


Then you would need the old fashion read'n write loop, copying the old data you like while merging in the new.

EDITORS (TPU, SLP) and languages like PERL and AWK are really good at that and DCL loops work fine also.

Hein.