Computing.Net > Forums > OpenVMS > Extract string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extract string

Reply to Message Icon

Name: William Polanco
Date: August 12, 2004 at 10:37:38 Pacific
OS: OpenVMS AXP Version V7.3-
CPU/Ram: 256
Comment:

Dears, I am new in OPENVMS, I like to obtain a specific field from a string line, similar like awk.

Furthermore, what command in OPENVMS is similar a head o tail in unix.

Thanks for your help.



Sponsored Link
Ads by Google

Response Number 1
Name: WillemGrooters
Date: August 13, 2004 at 05:45:48 Pacific
Reply:

Welcome to VMS world.

Your first line of knowlegde is HELP on the commandline ;-).

Other sources for help are www.openvms.org and the OpenVMS forum on www.hp.com (search for 'ITRC'. You need to register there but it's free)

To extract a specific field from a line:
It depends. If the fields are separated by a specific character, use F$ELEMENT:

Example (Type each line, without starting "$", at the prompt. ($ is the standard VMS prompt, usually used in typing when specifying code)

$var="1#one#st#first"
$nr=f$element(0,"#",var)
$nam=f$element(1,"#",var)
$ord=f$element(2,"#",var)
$onm=f$element(3,"#",var)
$! (! = comment character)
$sho sym nr
$sho sym nam
$sho sym ord
$sho sym onm

If you have no such delimiter, the command to extract part of a string is F$EXTRACT. You specify a starting position (actually: offset in the string, where the first character is at offset 0) and the size.
If you know what value you're looking for and it's size, but don't know the offset, use F$LOCATE to find the offset.

Example:

$ months="JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
$ dt = F$TIME() ! unix 'date' but in a different format
$ mmm=f$extract(3,3,dt)
$ m3= f$locate(mmm, months)
$ mm = m3/3 +1
$ sho sym mm

(Beware: although VMS is in essence case-insensitive, DCL is NOT! F$SEARCH will not find mm in months if case is different!)

If you specify:
$ months="JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC"

you can do:

$ i=0
$LOOP:
$ IF F$ELEMENT(I, months) .EQS. mm THEN GOTO ENDLOOP
$ IF I .LE. 11 THEN
$ I = I+1
$ GOTO LOOP
$ ELSE
$ WRITE SYS$OUTPUT "Invalid month"
$ GOTO ENDLOOP
$ ENDIF
$ ENDLOOP
$

Just try them out.
'Scripting' in unix can be done in VMS as well, of course. Just invoke the editor, put above commands in a file and save it as a_job.COM. Start it by specifying:

$ @a_job

and it will execute each command.

But again: use HELP (and check the HP.COm site for OpenVMS documentation)

I don't know the unix 'head' command, but 'tail''s equivalent in VMS is:

$ TYPE/TAIL <filename>

Willem


Willem Grooters


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to OpenVMS Forum Home


Sponsored links

Ads by Google


Results for: Extract string

how to extract command fields?? www.computing.net/answers/openvms/how-to-extract-command-fields/511.html

String too long to write into file www.computing.net/answers/openvms/string-too-long-to-write-into-file/453.html

To split a string with a space www.computing.net/answers/openvms/to-split-a-string-with-a-space/321.html