Read a specific line from a file
Some languages have special semantics for obtaining a known line number from a file. The task is to demonstrate how to obtain the contents of a specific line within a file. For the purpose of this task demonstrate how to the contents of the seventh line of a file can be obtained, and store this in a variable. If the file does not contain seven lines, or the seventh line is empty, or too big to be retrieved, output an appropriate message. If no special semantics are available for obtaining the required line, it is permissible to read line by line. Note that empty lines are considered and should still be counted,
J
<lang j>readLine=:4 :0
(1+x) {:: <;.2]1!:1 boxxopen y
)</lang>
Thus:
<lang bash>$ cal 2011 > cal.txt</lang>
<lang j> 7 readLine 'cal.txt' 23 24 25 26 27 28 29 27 28 27 28 29 30 31 </lang>
Note that this code assumes that the last character in the file is the line end character, and that the line end character is a part of the line to be retrieved.