Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

Using PIPE to Open and Read a File

 
Lin Park
Occasional Visitor

Using PIPE to Open and Read a File

I have a flat text file with a six digit date (yymmdd) imbedded inside a string of characters on the first line of the file. I want a single command to open the file, read the line, parse out the date, and compare it to what I expect it to be. I am trying to use this but not having any luck:

PIPE OPEN/READ/SHARE FILUN FNAME.TXT | READ FILUN FSTR | CLOSE FILUN | FILDAT=F$EXTRACT(73,6,FSTR) | IF "''FILDAT'" .NES. "060420" THEN EXIT 44

Please help - thank you!
9 REPLIES
David B Sneddon
Honored Contributor

Re: Using PIPE to Open and Read a File

How about

$ @COMPARE_STUFF

where compare_stuff.com contains the open/read/close
etc...?

Dave
Thomas Ritter
Respected Contributor

Re: Using PIPE to Open and Read a File

Lin, looks like you have a UNIX background. Remember when writing procedures, that's not just a dialog between you and the computer but involves the support staff or others who one day may have to examine your work for what ever reason.
John Gillings
Honored Contributor

Re: Using PIPE to Open and Read a File

Lin,

Yes, you can use PIPE to do this, but since this is really a sequence of commands to be executed sequentially, rather than a cascade of pipes, you need to enclose the sequence in parentheses and use the ";" separator, rather than "|"

So,

$ PIPE (OPEN/READ/SHARE FILUN FNAME.TXT ; -
READ FILUN FSTR ; CLOSE FILUN ; -
FILDAT=F$EXTRACT(73,6,FSTR) ; -
IF FILDAT.NES."060420" THEN EXIT 44)

Note you must have a space on either side of every ";" character so DCL knows it's not a file version number delimiter. I've also used continuation characters and explicit line feeds to make it more readable, but it could just be one long command.

Note that since it's just the first line you're interested in, using a "real" pipe you can shorten the sequence to:

$ PIPE TYPE FNAME.TXT | -
(READ SYS$PIPE FSTR ; -
FILDAT=F$EXTRACT(73,6,FSTR) ; -
IF FILDAT.NES."060420" THEN EXIT 44)

Although it's less characters, it involves an extra process creation, so may be slower.

As others have pointed out, this could be done using a command procedure, but then the pipe command could be stored in a single symbol, thus making it independent of a file.
A crucible of informative mistakes
Lin Park
Occasional Visitor

Re: Using PIPE to Open and Read a File

David - thanks for the suggestion but I am trying to do this without having a command procedure.

Thomas - thanks for that.

John - your response was quite helpful and the first one worked perfectly. Thank You VERY MUCH!!!!!
Hein van den Heuvel
Honored Contributor

Re: Using PIPE to Open and Read a File

Hmmm... I _love_ one liners. Honestly. But to create a subprocess just to be able to stick more than 1 command on a line it crazy, notbaly if that line is 'unmanageably' long.

Check out: $pipe ( show proc )

It would be kinda nice if DCL understood the parenthesis syntax without a pipe & subprocess:
$(command-1; command-2; ... )

Might I suggest:

$ perl -e "exit 44 if ('060420' ne substr <>,73,6)" fname.txt

hth,
Hein.






Jess Goodman
Esteemed Contributor

Re: Using PIPE to Open and Read a File

I agree that it's silly to create a subprocess for this, and that's what the parentheses will do (create a subshell). But are they needed? Why not just:

$ PIPE OPEN/READ/SHARE FILUN FNAME.TXT ; -
READ FILUN FSTR ; CLOSE FILUN ; -
FILDAT=F$EXTRACT(73,6,FSTR) ; -
IF FILDAT.NES."060420" THEN EXIT 44

You might want to use && in place of the first two ;s since if you can't open or read the file then the rest can't be done either.




I have one, but it's personal.
Jan van den Ende
Honored Contributor

Re: Using PIPE to Open and Read a File

Jess,

it is not the parentheses that give rise to the subproces, it is the PIPE command that does that.
So, your solution also creates a subprocess.

fwiw,

Proost.

Have one on me (perhaps in May in Nashua>)

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

Re: Using PIPE to Open and Read a File

Jan, did you try?

Check out:

$Help pipe description

or try:

$show proc
$ pipe open x x.tmp ; read x y ; show process ; show symb y

Jess,

Thanks for that entry. You are right. I only ever used pipe to pipe into a next process, but that's only one of its features. I learned something new today!

Cheers,
Hein.
Jan van den Ende
Honored Contributor

Re: Using PIPE to Open and Read a File

Well,

Hein, that makes 2 who learned! Until now I took "PIPE = create subprocess" for granted.

Thanks, Jess!

Proost.

Have one on me (maybe in May in Nashua?)

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