Operating System - OpenVMS
1820475 Members
3227 Online
109624 Solutions
New Discussion юеВ

line or word count in a file

 
dvvkm
Occasional Contributor

line or word count in a file

Hi ,
Can any one help me in finding out the line or words count in the file ,if possible even the character count with spaces or without spaces as i need to cross-check the files in VMS and the same file downloaded into windows.Any kind of script or command is required.Am in great need.Pls send in your solutions asap.
DV
8 REPLIES 8
Volker Halle
Honored Contributor

Re: line or word count in a file

DV,

welcome to the OpenVMS ITRC forum.

$ SEARCH file/STATIS any-string

will at least report:

Records searched: xxx
Characters searched: xxx

Volker.
Hein van den Heuvel
Honored Contributor

Re: line or word count in a file

Welcome to the OpenVMS ITRC forum

To get that kind of information from a file, you'll need to read it. VMS does NOT have a standard tool (like a Unix wc) to do this.
So you'll have to create a DCL script, an editor script, or program.
Personally I would use PERL.

For example, the following one-liner will print the number of Characters, Words and Lines for a file.

perl -ne "$w+=split; $c+=length; END {print qq($c $w $.)}" file.txt

The number of whitespace characters could be counted by adding $x++=s/\s//g
Untested. Left as an excercise.

Cheers,
Hein

Jon Pinkley
Honored Contributor

Re: line or word count in a file

Augmenting what Volker said, you can use the following command if you just want the number of records and number of characters.

$ search file ""/noooutput/stat

That will display only the statistics, and is marginally more efficient than specifying a search string.

If you want to know if the files are the same, have you considered using something like md5sum?

Jon
it depends
Hein van den Heuvel
Honored Contributor

Re: line or word count in a file

This perl one liner prints counts for:
Characters, whiteSpace, Words and Lines.

$ perl -ne "$w+=split; $c+=length; $s+=s/\s//g}{print qq($c $s $w $.)" file.txt

the -n generates an implied loop:
while ($_ = ) {
[program]
}

the -e indicated the program is in the next argument

split is normally used as @words=split(/\s+/,$_) but here all defaults are accepted and the output is used as a scalar, not an array in which case it returns the numer of elements for the array)

similar for s/\s\//g which indicates to replace each whitespace (\s) in default variable $_ by nothing, counting as it goes.

$. is the current line.

}{ closes the main [program] in the loop and starts a new, end block which is then closed by the } provided through -n

Enjoy!
Hein
dvvkm
Occasional Contributor

Re: line or word count in a file

Hi guys,

Really am happy to see the solutions. But apparently i am sorry heuvel i cant use any of those perl commands.I just need them in VMS command or even DCL script to find the total count of words and lines in a file.Thanks volker and jon for your prompt reply :-).Its exactly what i want. Just with curiosity am asking this - is it possible to get the total word count in a file.

Volker Halle
Honored Contributor

Re: line or word count in a file

DV,

there is no native OpenVMS command to get the word count of a file.

On recent versions of OpenVMS, you could install GNV. This provide the wc command:

AXPVMS $ bash
bash$ wc login.com
94 358 3213 login.com
bash$ exit
exit
AXPVMS $

You can download GNV from:

http://h71000.www7.hp.com/opensource/opensource.html#gnv

GNV (GNU's Not VMS) is an open source, GNU-based UNIX environment for OpenVMS that provides UNIX application developers, system managers, and users a UNIX-style environment on OpenVMS

Volker.
dvvkm
Occasional Contributor

Re: line or word count in a file

Thanks a lot for all your patience in solving my problem and for your accurate and prompt answers. thanks
dvvkm
Occasional Contributor

Re: line or word count in a file

Thanks for all of you