Read a specific line from a file: Difference between revisions

PascalABC.NET
(PascalABC.NET)
(5 intermediate revisions by 4 users not shown)
Line 1,202:
 
===Using Java 11===
<syntaxhighlight lang="java">
 
<syntaxhighlight>
 
import java.io.IOException;
import java.nio.file.Files;
Line 1,268 ⟶ 1,266:
<pre>julia> open(line -> read_nth_lines(line, 7), "path/to/file")
"Hi, I am the content of the seventh line\n"</pre>
 
=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>(0:"135-0.txt")7
"will have to check the laws of the country where you are located before\r"</syntaxhighlight>
 
Note that line 0 is the first line of the file. If you dislike the concept of a "zeroth line", you should use 7-1 rather than 7 to retrieve the seventh line:
 
<syntaxhighlight lang=K>(0:"135-0.txt")7-1
"www.gutenberg.org. If you are not located in the United States, you\r"</syntaxhighlight>
 
=={{header|Kotlin}}==
Line 1,558 ⟶ 1,565:
while (<>) { $. == $n and print, exit }
die "file too short\n";</syntaxhighlight>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var linenum := 4;
ReadLines('_a.pas').Skip(linenum-1).First.Print;
end.
</syntaxhighlight>
 
=={{header|Phix}}==
Line 2,103 ⟶ 2,118:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func getNthLine(filename, n) {
var file = File.new(filename);
file.open_r.each { |line|
Num($.) == n && return line;
}
warn "file #{file} does not have #{n} lines, only #{$.}\n";
return nil;
}
 
var line = getNthLine("/etc/passwd", 7);
printsay line if defined (line;)</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 2,305 ⟶ 2,320:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
var lines = File.read("input.txt").replace("\r", "").split("\n")
242

edits