Operating System - OpenVMS
1748169 Members
4146 Online
108758 Solutions
New Discussion юеВ

Re: How to extract string from command output?

 
SOLVED
Go to solution
Andrew Yip
Advisor

Re: How to extract string from command output?

Hello Hein,

Thanks a million for your explanation. I now understand how to extract the strings.

Basically, i can get an output from my command and perform a search followed by a f$element function to extract the string.

However, is there any command or method to extract by line number?

Let's say for example, i would like to extract only line 1 of textfile A and line 2 onwards of textfile B. Finally join them into a new textfile.

The way i see f$element is extracting strings by a seperator character or element.

Please kindly advice. Thank you!

Andrew
____________________________________________
Andrew Yip
Advisor

Re: How to extract string from command output?

Hi Phil,

What are the workings beneath the 'tt'?
I can't run this in script mode. I'll have to execute it manually to obtain the output.

Andrew
____________________________________________
Karl Rohwedder
Honored Contributor

Re: How to extract string from command output?

The @ starts the commandfile TT, which is a logical name pointing to your terminal, but it allows for the /OUPUT qualifier.

There is a freeware utility called EXTRACT which allows to extract selectable record from files, see http://vms.process.com/scripts/fileserv/fileserv_search.exe?package=extract&description=&author=&system=Either&language=All&RD=&RM=&RY=

regards kalle
Ian Miller.
Honored Contributor

Re: How to extract string from command output?

TT is a logical name refering to your terminal.
$ @TT:

Is running a script from your terminal.

/OUTPUT redefines SYS$OUTPUT

The following will redirect output for one command then return to normal.

$ DEFINE/USER SYS$OUTPUT FILE.LIS
$ somecommandhere
Then open the file
$ OPEN HANDLE FILE.LIS
read the lines
$ READ HANDLE LINE
and process them.
____________________
Purely Personal Opinion
Andrew Yip
Advisor

Re: How to extract string from command output?

Hi Kalle,

Does the extract utility helps me in extracting particular lines from a textfile?

I'm unable to d/l the zip file.
Karl Rohwedder
Honored Contributor

Re: How to extract string from command output?

It allows specification of start/end records, start/end columns and can edit the extracted records (like F$EDIT, but more).

Here are the Examples from HELP:

2 examples
!Display the last 10 lines of Login.Com on the terminal
$ EXTRACT/TAIL=10 Login.Com /IDENTIFY

!Look at the first few lines of all Fortran source files
$ EXTRACT/HEAD=5 *.For

!Copy Test.Txt to Test.Dat, converting text into upper case
! and removing excess blanks.
$ EXTRACT/EDIT=(UPCASE,COMPRESS) Test.Txt/OUTPUT=Test.Dat

!Extract all but the first 10 and last 10 records of Test.Dat
$ EXTRACT Test.Dat/RECORDS=(START=11,END=-11) /OUTPUT=Test.Mid

!Get specific columns out of several files
$ EXTRACT/COLUMNS=(1:10,18:19,25,41:*) Test.*,[...]*.Tmp


I've added the ZIP file

regards Kalle
Hein van den Heuvel
Honored Contributor

Re: How to extract string from command output?

>> However, is there any command or method to extract by line number?
>> Let's say for example, i would like to extract only line 1 of textfile A and line 2 onwards of textfile B. Finally join them into a new textfile.

Sure, same loop through file principle, but keep a running count... in general. However, in the case of 1 or 2, I would just read it.
Untested example below.

Sounds like you'd better get that 'Programming in DCL book' and check out some examples, on your system or here in the forum.

However, since you are new to VMS/DCL I would suggest to learn PERL and/or AWK for file/line/text manipulation. You'll be able to use the skill on multiple platforms

$close/nolog file ! In case it was left open
$open file file-1
$n=6
$i=0
$loop:
$1=1+1
$read /end=not_found file record
$if i.lt.n then goto loop
$
$write sys$output "record ''n' = record"

Your question

$close/nolog file ! In case it was left open
$open file file-1
$read /end=not_found file record_1
$close file
$open file file-1
$read /end=not_found file record_2
$read /end=not_found file record_2
$close file
$open/write file file-3
$write file record_1 + record_2
$close file


to read record 123 in perl:

perl -ne "print if ($.== 123)"


Hein.
Jan van den Ende
Honored Contributor

Re: How to extract string from command output?

Andrew,

another wat to generate output from a command procedure, say ABCD.COM

$ @ABCD/OUTPUT= [p1] [p2] ...

Remember to put /out BEFORE any params.

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?

I had extracted the numbers (free space) from the command output and made some logical comparison between them.

============================================
$ IF (MAINfreebytes .LES. INTfilesize) .OR. (VSMfreebytes .LES. PATfilesize)
$ THEN GOTO ERROR34
$ ELSE write temp "Free space check OK."
$ ENDIF
============================================

MAIN Memory free space: 2790400 bytes
VSM Memory free space: 59935744 bytes
Program file size: 91136 bytes
Pattern files size: 14043136 bytes
Insufficient tester memory space!

But the result was incorrect! This is so strange! I even explicitly defined them to be of integer type.

E.g.
$ INTfilesize = f$integer(f$element(4," ",record))

Can anyone advice on what is wrong here?
Thanks!

Andrew
Andrew Yip
Advisor

Re: How to extract string from command output?

Sorry, i wasn't very clear in my previous post.

i was comparing the following:

$ IF (2790400 .LES. 91136) .AND. (59935744 .LES. 14043136)
$ THEN write sys$output "Insufficient tester memory space!"
$ ELSE write temp "Free space check OK."
$ ENDIF
============================================
Result: Insufficient tester memory space!

I was expecting to see "Free space check OK."