Operating System - OpenVMS
1822494 Members
2495 Online
109642 Solutions
New Discussion юеВ

counting number of lines in file using dcl script

 
SOLVED
Go to solution
Jaimin Parikh
Frequent Advisor

counting number of lines in file using dcl script

Friends:

Can anybody help me in writing a script to count number of lines for a ascii file? I do not want to use open /read loop thing.

If anybody can help me, I would be highly oblige.
JAIMIN PARIKH : Share your knowledge and help those who need your help!!
5 REPLIES 5
Craig A Berry
Honored Contributor

Re: counting number of lines in file using dcl script

You can use various options on the search command to see how many lines are in a file:

$ search/statistics/nooutput sys$login:login.com ""

Files searched: 1 Buffered I/O count: 5
Records searched: 48 Direct I/O count: 2
Characters searched: 1162 Page faults: 26
Records matched: 48 Elapsed CPU time: 0 00:00:00.00
Lines printed: 0 Elapsed time: 0 00:00:00.02

but that doesn't store the result in a symbol -- is that something you need to do? If so, you probably do need to read in a loop, which isn't all that bad. Here's an example:

$ open/read a 'p1
$ count = 0
$ loop:
$ read/end=all_done a line
$ count = count + 1
$ goto loop
$ all_done:
$ close a
$ write sys$output "There are ",count," records in ", f$parse("''p1'")
$ exit
Lokesh_2
Esteemed Contributor
Solution

Re: counting number of lines in file using dcl script

and how about this:

1. Below is my text file:

DCCDT2> typ a.txt
this is test file
this is test file
this is test file

2. Here is my command procedure:

DCCDT2> type d.com
$set noon
$pipe sear/stat a.txt "" | sear sys$pipe lines > c.dat
$open/read x c.dat
$read x y
$d :== 'F$extract(14,19,y)
$sho sym d
$close x
$exit

3. And here I execute my command procedure . It give me no. of lines in file :

DCCDT2> @d
D == "3"
DCCDT2>
What would you do with your life if you knew you could not fail?
Hein van den Heuvel
Honored Contributor

Re: counting number of lines in file using dcl script


When you get that count, what are you going to do with it? If you use a tool like AWK or PERL then you can potentially immediatly use the number and add more value to it.

An other example of a tool telling you how many records there were:

$ app/log tmp.tmp nl:
%APPEND-S-APPENDED, U$1:[HEIN]TMP.TMP;4 appended to NL: (275 records)

Up to you to 'catch' that output and parse it.

A trivial perl example to set a logical name (NR) to the number of lines. It just does nothing reading the files and uses the perl built-in $. to report the count.


$ perl -E "while (<>){}; $ENV{""NR""}=$." < tmp.tmp
$ show log NR
"NR" = "275" (LNM$PROCESS_TABLE)

And an awk example:
$ gawk /comm="END {print NR}" tmp.tmp
275

What I could not figure out quickly enough, maybe a problem with the awk I have, is to return the numnber of lines encodeded in the exit status. Something like:

$ gawk /comm="END {exit 2*NR+1}" your-file

You could then maybe convert back to lines,
but it only returns 'abort' status (%X1000002C)

$ write sys$output "lines: ", ($STATUS - %X10000001)/2


hth,
Hein.
John Gillings
Honored Contributor

Re: counting number of lines in file using dcl script

You can do it in one "line" using PIPE. There is no avoiding "open read loop", but you can avoid the loop being DCL. I'm using SEARCH/STATISTICS.

(beware wrapping...)

$ file="your-file-name"
$ PIPE SEARCH/STATISTICS/NOOUTPUT 'file' " " | -
SEARCH SYS$PIPE "records searched" | -
(READ SYS$PIPE line ; -
lines=F$ELEMENT(2," ",F$EDIT(line,"COMPRESS")) ; -
DEFINE/JOB lines &lines)

On completion, the JOB logical name "LINES" will contain the line count of your file. If the file doesn't exist, the count will be 0.

You'd be much better off writing a trivial program, 2 parameters, file name and symbol name:

$ countlines 'file' lines
$ WRITE SYS$OUTPUT "line count=''lines'"
A crucible of informative mistakes
Hein van den Heuvel
Honored Contributor

Re: counting number of lines in file using dcl script


and a little more as a one-liner also with pipes:

$pipe sea/log file xxx |( read sys$pipe l ; l=f$ele(3," ",l) ; defi/job l &l
)


Hein.