Remove lines from a file: Difference between revisions

m
→‎{{header|11l}}: new way of specifying file open mode
m (syntax highlighting fixup automation)
m (→‎{{header|11l}}: new way of specifying file open mode)
 
(4 intermediate revisions by 4 users not shown)
Line 17:
V l = File(:argv[1]).read_lines()
l.del(Int(:argv[2]) - 1 .+ Int(:argv[3]))
File(:argv[1], ‘w’WRITE).write(l.join("\n"))</syntaxhighlight>
 
=={{header|Ada}}==
Line 246:
back
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">removeFileLines: function [filename, start, cnt][
previous: ø
ensure -> all? @[
exists? filename
previous: read.lines filename
start < size previous
(start + cnt) < size previous
]
 
final: previous\[0..start-1] ++ previous\[start+cnt..dec size previous]
write filename join.with:"\n" final
]
 
removeFileLines "myfile.txt" 3 10</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 2,768 ⟶ 2,785:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func remove_lines(file, beg, len) {
var lines = file.open_r.lines;
lines.splice(beg, len).len == len || warn "Too few lines";
file.open_w.printwrite(lines.join("\n"), :utf8)
}
 
remove_lines(File(__FILE__), 2, 3);</syntaxhighlight>
 
=={{header|Snobol4}}==
Line 3,141 ⟶ 3,158:
{{trans|Kotlin}}
More or less.
<syntaxhighlight lang="ecmascriptwren">import "os" for Platform
import "io" for File
 
Line 3,191 ⟶ 3,208:
Line 8
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">def LF=$0A, EOF=$1A;
 
proc EatLine; \Read and discard a text line from input file
int Ch;
[repeat Ch:= ChIn(3);
if Ch = EOF then
[Text(0, "Attempted to remove a line beyond EOF"); exit];
until Ch = LF;
];
 
proc CopyLine; \Read and write a text line
int Ch;
[repeat Ch:= ChIn(3);
if Ch = EOF then
[Text(0, "Attempted to copy a line beyond EOF"); exit];
ChOut(3, Ch);
until Ch = LF;
];
 
proc RemoveLines(FileName, StartLine, NumLines);
int FileName, StartLine, NumLines;
int N, Ch, IH, OH;
[IH:= FOpen(FileName, 0);
FSet(IH, ^i); \open specified file for input
OpenI(3);
 
OH:= FOpen("file.tmp", 1);
FSet(OH, ^o); \open temporary file for output
OpenO(3);
 
for N:= 1 to StartLine-1 do CopyLine; \copy lines up to start of Start line
for N:= 1 to NumLines do EatLine; \discard NumLines
loop [Ch:= ChIn(3); \copy remainder of input file
if Ch = EOF then quit;
ChOut(3, Ch);
];
Close(3);
FClose(OH); FClose(IH);
 
OH:= FOpen("foobar.txt", 1);
FSet(OH, ^o); \open file for output
OpenO(3);
 
IH:= FOpen("file.tmp", 0);
FSet(IH, ^i); \open temp file for input
OpenI(3);
 
loop [Ch:= ChIn(3); \copy file
if Ch = EOF then quit;
ChOut(3, Ch);
];
Close(3);
];
 
RemoveLines("foobar.txt", 1, 2)
]</syntaxhighlight>
 
=={{header|zkl}}==
1,466

edits