;+ ; NAME: ; nthline ; ; PURPOSE: (one line) ; Return the nth line in a file ; ; DESCRIPTION: ; Return the nth line in a file ; ; CATEGORY: ; File IO ; ; CALLING SEQUENCE: ; line = nthline(filename, linenum) ; ; INPUTS: ; filename - file from which to get lines ; linenum - the line number to return (long or array of long) ; ; OPTIONAL INPUT PARAMETERS: ; None ; ; KEYWORD INPUT PARAMETERS: ; /VERBOSE -- If set, print diagnostics to screen. ; ; KEYWORD OUTPUT PARAMETERS: ; NSTARS -- Returns the number of successfully-retrieved lines. ; ; OUTPUTS: ; If input is a scalar, then output is a single string. ; If input is a vector, then output is an array of strings. ; If a line is not found (linenum out of range), returned value is ''. ; ; COMMON BLOCKS: ; None ; ; SIDE EFFECTS: ; None ; ; PROCEDURES: ; spawned call to sed ; ; RESTRICTIONS: ; ; EXAMPLE: ; print, nthline(102,[430L, 1000L]) ; ; MODIFICATION HISTORY: ; Written 2006 Apr 23 Leslie Young SwRI ;- function nthline, filename, linenum, maxline=maxline nline = n_elements(linenum) lines = strarr(nline) if not keyword_set(maxline) then maxline = file_lines(filename) for i = 0L, nline-1 do begin n = linenum[i] if n ge 0 and i le maxline then begin nstr = strtrim(string(linenum[i]+1),2) ; build the unix command to spawn cmd = 'sed -e{' + nstr + 'P} -n ' + filename ; print, cmd spawn, cmd, result ; help, result lines[i] = result end end return, lines end