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
|