comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » ? Number of lines in a file
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: ? Number of lines in a file [message #8104 is a reply to message #8014] Mon, 10 February 1997 00:00 Go to previous messageGo to previous message
R. Bauer is currently offline  R. Bauer
Messages: 137
Registered: November 1996
Senior Member
David Gunter wrote:
>
> I need to know how many lines are in a file so I use:
>
> spawn, "fgrep -cv 'gbrsh' "+filename, n_lines
>
> Is there a more efficient way to do this? I've searched the manuals with no
> luck.
>
> BTW, I'm running on a UNIX system for those confused by the above line. ;)
>


It is!
Look at this,

Idl is very fast in processing of arrays. So the idea was to find out
how many bytes are in a file then it could be opened and read at once as
a bytearr.
After this I count the 10B which indicates the number of lines.

I include my two routines to this mail.




; Copyright R.Bauer 2. Jan. 1996
; the idea to use fstat instead of spawn ls -l was given by
; Phil Williams

function filesize, filename

if n_params(0) lt 1 then begin
help: print, ' Diese Hilfe kommt mit a=filesize().'
print,' '
print,' stellt fest wieviele Bytes in einer Datei sind.'
print,''
print,'Example'
print,"a=filesize('testfile.asc')"
print,'----------------------------------------------------- --'
return,-1
help_open: print,'(filesize) Das File: ',filename,' gibt es nicht.'
return,-1
ENDIF


openr, lun, filename, /get_lun,error=err
if err ne 0 then goto, help_open
stats = fstat(lun)
free_lun, lun

return, stats.size
end

======= cut here =======

; MODIFICATION HISTORY:
; Copyright R.Bauer 2. Jan. 1996
;-


function fileline, filename

if n_params(0) lt 1 then begin
help: print, ' Diese Hilfe kommt mit a=fileline().'
print,' '
print,' stellt fest wieviele Zeilen in einer ASCII Datei sind.'
print,''
print,'Example'
print,"a=fileline('testfile.asc')"
print,'----------------------------------------------------- --'
return,-1
help_open: print,'(fileline) Das File: ',filename,' gibt es nicht.'
return,-1
ENDIF

byt=filesize(filename)


if byt eq -1 then goto, help_open

lesefeld=bytarr(byt)

openr,lun,filename,/get_lun,error=err
if err ne 0 then goto, help_open
readu,lun,lesefeld ;lese=string(a)
close,lun

free_lun,lun
line=where(lesefeld eq 10B,count_line)
;help,count_line

return,count_line
END

--
R.Bauer

Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@kfa-juelich.de
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: 32-bit Unsigned Integers, Was: Unsigned Integers - How?
Next Topic: Re: help command

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 19:35:36 PDT 2025

Total time taken to generate the page: 0.00423 seconds