? Number of lines in a file [message #8014] |
Fri, 31 January 1997 00:00  |
gunter
Messages: 13 Registered: October 1996
|
Junior Member |
|
|
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. ;)
--
david gunter
http://www.mcs.anl.gov/people/gunter/
-------------------------------------
"When you are a Bear of Very Little Brain, and you Think of Things, you find
sometimes that a Thing which seemed very Thingish inside you is quite
different when it gets out into the open and has other people looking at it."
- A.A. Milne, "The House At Pooh Corner"
|
|
|
|
Re: ? Number of lines in a file [message #8104 is a reply to message #8014] |
Mon, 10 February 1997 00:00  |
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
|
|
|
Re: ? Number of lines in a file [message #8137 is a reply to message #8014] |
Thu, 06 February 1997 00:00  |
derekfox
Messages: 1 Registered: February 1997
|
Junior Member |
|
|
In article <1997Feb5.160127.7541@queens-belfast.ac.uk>, D.Kennedy@qub.ac.uk (David Kennedy) writes:
> In article <5ctkah$rqq@uwm.edu>,
> gunter@alpha1.csd.uwm.edu (David Gunter) writes:
>> 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. ;)
>
> Well, this isn't much easier but 'wc -l'(UNIX) returns the number of lines
> in a file, it simplifies things if nothing else.
> --
If you're willing to have another one-trick pony sitting in your
$IDL_PATH, using IDL-only routines will be much faster (factor of 50
improvement on my machine), after compilation, than any "spawn".
Try this "n_lines.pro".
One warning: results seem to differ form those of `wc -l` for non-ascii
files.
--Derek Fox
function n_lines,file
on_ioerror,ioerr
openr,unit,file,/get_lun,error=err
if err ne 0 then begin
ioerr:
message,'Error reading file '+file,/inform
return,-1
endif
nlines=0l & line=''
while not eof(unit) do begin
readf,unit,line
nlines=nlines+1
endwhile
free_lun,unit
return,nlines
end
|
|
|
Re: ? Number of lines in a file [message #8138 is a reply to message #8014] |
Thu, 06 February 1997 00:00  |
Karlheinz Knipp
Messages: 2 Registered: November 1996
|
Junior Member |
|
|
David Kennedy wrote:
>
> In article <5ctkah$rqq@uwm.edu>,
> gunter@alpha1.csd.uwm.edu (David Gunter) writes:
>> 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. ;)
>
> Well, this isn't much easier but 'wc -l'(UNIX) returns the number of lines
> in a file, it simplifies things if nothing else.
> --
> David Kennedy, Dept. of Pure & Applied Physics, Queen's University of Belfast
> Email: D.Kennedy@Queens-Belfast.ac.uk | URL: http://star.pst.qub.ac.uk/~dcjk/
> Hi! I'm a .signature virus! Copy me into yours and join the fun!
1. Under UNIX it's also possible to use: awk "END {print NR}"
but again: it's a system-call
2. How about the following:
;
------------------------------------------------------------ ------------------
; open file, get number of bytes, associate data
openr, uni, input_file, /get_lun & point_lun, uni, 0
stat = fstat(uni)
bytes = stat.size
if bytes eq 0 then begin
free_lun, uni
return, 0
endif
i = assoc(uni, bytarr(bytes))
;
------------------------------------------------------------ ------------------
; count
test = where(i(0) eq 10b)
if test(0) eq -1 then count = 0 else count = n_elements(test)
;
------------------------------------------------------------ ------------------
; free file, return & end
free_lun, uni
return, count
end
Have fun,
Karl
--
Karlheinz Knipp knipp@digitalmap.hi.bosch.de
Robert Bosch Data GmbH Dep. DG-PMM
Robert Bosch Str. 200 D-31139 Hildesheim
Tel.: +49 5121 49 5406 Fax.: +49 5121 49 4815
|
|
|