Read a specific line from a file: Difference between revisions

→‎{{header|REXX}}: added the REXX language. ----
(→‎{{header|REXX}}: added the REXX language. ----)
Line 615:
> seven <- scan('Incoming/quotes.txt', '', skip = 6, nlines = 1, sep = '\n')
Read 1 item
</lang>
 
=={{header|REXX}}==
<lang REXX>
/*read a specific line from a file.*/
 
parse arg fileId n . /*get the user args: fileid n */
if fileID=='' then fileId='JUNK.TXT' /*assume the default: JUNK.TXT */
if n=='' then n=7 /*assume the default (n=7) */
 
L=lines(fileid) /*first, see if the file exists. */
if L==0 then do; say 'error, fileID not found:' fileId; exit; end
 
if n\==1 then call linein fileId,n-1 /*second, read previous rec. to N*/
L=lines(fileid) /* L = # lines left in the file.*/
q=linein(fileId,n) /*read the Nth line, store in Q.*/
 
select
when L==0 & length(q)==0 then say 'line' n "not found."
when L==0 then say 'line' n "has a zero length."
otherwise say 'file' fileId "record" n '=' q
end
 
 
/*----------------------------------------------------------------------+
|------ Normally, we could just use: ----- |
| |
| q=linein(fileId,n) /*read a specific record num. */ |
| |
|---- but LINEIN will just return a null when a record isn't found or|
|---- when the fileID doesn't exist, or N is beyond the end-of-file. |
+----------------------------------------------------------------------*/
</lang>
 
Line 633 ⟶ 665:
 
puts getNthLine("/etc/passwd", 7)</lang>
 
 
=={{header|Scala}}==