September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,121 @@
|
|||
# removes the specified number of lines from a file, starting at start line (numbered from 1) #
|
||||
# returns TRUE if successful, FALSE otherwise #
|
||||
PROC remove lines = ( STRING file name, INT start line, INT number of lines )BOOL:
|
||||
IF number of lines < 0 OR start line < 1
|
||||
THEN
|
||||
# invalid parameters #
|
||||
print( ( "number of lines must be >= 0 and start line must be >= 1", newline ) );
|
||||
FALSE
|
||||
ELIF FILE temp file;
|
||||
create( temp file, stand back channel ) /= 0
|
||||
THEN
|
||||
# unable to create a temporary output file #
|
||||
print( ( "Unable to create temporary file", newline ) );
|
||||
FALSE
|
||||
ELIF NOT reset possible( temp file )
|
||||
THEN
|
||||
# rewinding the temporary file is not possible #
|
||||
# we would have to get its name ( with "idf( temp file )", close it and re-open it #
|
||||
print( ( "Temp file is not rewindable", newline ) );
|
||||
FALSE
|
||||
ELIF FILE input file;
|
||||
open( input file, file name, stand in channel ) /= 0
|
||||
THEN
|
||||
# failed to open the file #
|
||||
print( ( "Unable to open """ + file name + """", newline ) );
|
||||
FALSE
|
||||
ELSE
|
||||
# files opened OK #
|
||||
BOOL at eof := FALSE;
|
||||
# set the EOF handler for the original file #
|
||||
on logical file end( input file
|
||||
, ( REF FILE f )BOOL:
|
||||
BEGIN
|
||||
# note that we reached EOF on the latest read #
|
||||
at eof := TRUE;
|
||||
# return TRUE so processing can continue #
|
||||
TRUE
|
||||
END
|
||||
);
|
||||
# copy the input file to the temp file #
|
||||
WHILE STRING line;
|
||||
get( input file, ( line, newline ) );
|
||||
NOT at eof
|
||||
DO
|
||||
put( temp file, ( line, newline ) )
|
||||
OD;
|
||||
# copy the temp file back to the input, removing the specified lines #
|
||||
close( input file );
|
||||
IF open( input file, file name, stand out channel ) /= 0
|
||||
THEN
|
||||
# failed to open the original file for output #
|
||||
print( ( "Unable to open ", file name, " for output", newline ) );
|
||||
FALSE
|
||||
ELSE
|
||||
# opened OK - copy the temporary file #
|
||||
reset( temp file ); # rewinds the input file #
|
||||
at eof := FALSE;
|
||||
on logical file end( temp file
|
||||
, ( REF FILE f )BOOL:
|
||||
BEGIN
|
||||
# note that we reached EOF on the latest read #
|
||||
at eof := TRUE;
|
||||
# return TRUE so processing can continue #
|
||||
TRUE
|
||||
END
|
||||
);
|
||||
INT end line = ( start line - 1 ) + number of lines;
|
||||
INT line number := 0;
|
||||
WHILE STRING line;
|
||||
get( temp file, ( line, newline ) );
|
||||
NOT at eof
|
||||
DO
|
||||
# have another line #
|
||||
line number +:= 1;
|
||||
IF line number < start line OR line number > end line
|
||||
THEN
|
||||
put( input file, ( line, newline ) )
|
||||
FI
|
||||
OD;
|
||||
# close the files #
|
||||
close( input file );
|
||||
scratch( temp file );
|
||||
IF line number < start line
|
||||
THEN
|
||||
# didn't find the start line #
|
||||
print( ( "Specified start line (", whole( start line, 0 ), ") not in ", file name, newline ) );
|
||||
FALSE
|
||||
ELIF line number < end line
|
||||
THEN
|
||||
# the ending line was not in the file #
|
||||
print( ( "Final omitted line not in the file ", file name, newline ) );
|
||||
FALSE
|
||||
ELSE
|
||||
# successful operation #
|
||||
TRUE
|
||||
FI
|
||||
FI
|
||||
FI # remove lines # ;
|
||||
|
||||
# test the line removal #
|
||||
BEGIN
|
||||
FILE t;
|
||||
open( t, "test.txt", stand out channel );
|
||||
print( ( "Before...", newline ) );
|
||||
FOR i TO 10 DO
|
||||
STRING line = whole( i, - ( ( i MOD 5 ) + 3 ) );
|
||||
put( t, ( line, newline ) );
|
||||
print( ( line, newline ) )
|
||||
OD;
|
||||
close( t );
|
||||
remove lines( "test.txt", 2, 3 );
|
||||
print( ( "After...", newline ) );
|
||||
open( t, "test.txt", stand in channel );
|
||||
FOR i TO 7 DO
|
||||
STRING line;
|
||||
get( t, ( line, newline ) );
|
||||
print( ( line, newline ) )
|
||||
OD;
|
||||
close( t );
|
||||
print( ( "----", newline ) )
|
||||
END
|
||||
|
|
@ -1,27 +1,35 @@
|
|||
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
||||
' Remove File Lines V1.0 '
|
||||
' Remove File Lines V1.1 '
|
||||
' '
|
||||
' Developed by A. David Garza Marín in VB-DOS for '
|
||||
' RosettaCode. November 30, 2016. '
|
||||
' '
|
||||
' Date | Change '
|
||||
'-------------------------------------------------- '
|
||||
' 2016/11/30| Original version '
|
||||
' 2016/12/30| Added functionality to read parameters'
|
||||
' | from Command Line '
|
||||
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
||||
|
||||
'OPTION _EXPLICIT ' For QB45
|
||||
'OPTION EXPLICIT ' For VBDOS, PDS 7.1
|
||||
OPTION EXPLICIT ' For VBDOS, PDS 7.1
|
||||
|
||||
' SUBs and FUNCTIONs
|
||||
DECLARE FUNCTION DeleteLinesFromFile% (WhichFile AS STRING, Start AS LONG, HowMany AS LONG)
|
||||
DECLARE FUNCTION FileExists% (WhichFile AS STRING)
|
||||
DECLARE FUNCTION GetDummyFile$ (WhichFile AS STRING)
|
||||
DECLARE FUNCTION getFileName$ (CommandString AS STRING)
|
||||
DECLARE FUNCTION getHowManyLines& (CommandLine AS STRING)
|
||||
DECLARE FUNCTION getStartPoint& (CommandLine AS STRING)
|
||||
DECLARE FUNCTION ErrorMessage$ (WhichError AS INTEGER)
|
||||
DECLARE FUNCTION CountLines& (WhichFile AS STRING)
|
||||
DECLARE FUNCTION YorN$ ()
|
||||
|
||||
' Var
|
||||
DIM iOk AS INTEGER, iErr AS INTEGER, lStart AS LONG, lHowMany AS LONG, lSize AS LONG
|
||||
DIM sFile AS STRING
|
||||
DIM sFile AS STRING, sCommand AS STRING
|
||||
|
||||
' Const
|
||||
CONST ProgramName = "RemFLine (Remove File Lines) Enhanced V1.0"
|
||||
CONST ProgramName = "RemFLine (Remove File Lines) Enhanced V1.1"
|
||||
|
||||
' ----------------------------- Main program cycle --------------------------------
|
||||
CLS
|
||||
|
|
@ -34,41 +42,59 @@ PRINT "quantity of lines stated to be deleted is beyond the total lines in the t
|
|||
PRINT "file, the process also will be aborted. The program will give you a message"
|
||||
PRINT "if everything ran ok or if any error happened. Includes a function to count"
|
||||
PRINT "how many lines has the intended file."
|
||||
DO
|
||||
PRINT
|
||||
INPUT "Please, type the name of the file"; sFile
|
||||
sFile = LTRIM$(RTRIM$(sFile))
|
||||
IF sFile <> "" THEN
|
||||
lSize = CountLines&(sFile)
|
||||
IF lSize > 0 THEN
|
||||
PRINT "Delete starting on which line (Default=1, Max="; lSize; ")";
|
||||
INPUT lStart
|
||||
' Verifies if parameters are specified
|
||||
sCommand = COMMAND$
|
||||
IF sCommand <> "" THEN
|
||||
sFile = getFileName$(sCommand)
|
||||
lSize = CountLines&(sFile)
|
||||
lStart = getStartPoint&(sCommand) ' Defaults to 1
|
||||
lHowMany = getHowManyLines&(sCommand) ' Defaults to 1
|
||||
ELSE
|
||||
PRINT
|
||||
INPUT "Please, type the name of the file"; sFile
|
||||
sFile = LTRIM$(RTRIM$(sFile))
|
||||
IF sFile <> "" THEN
|
||||
lSize = CountLines&(sFile)
|
||||
IF lSize > 0 THEN
|
||||
PRINT "Delete starting on which line (Default=1, Max="; lSize; ")";
|
||||
INPUT lStart
|
||||
|
||||
IF lStart = 0 THEN lStart = 1
|
||||
IF lStart < lSize THEN
|
||||
PRINT "How many lines do you want to remove (Default=1, Max="; (lSize - lStart) + 1; ")";
|
||||
INPUT lHowMany
|
||||
IF lHowMany = 0 THEN lHowMany = 1
|
||||
IF lHowMany + lStart <= lSize THEN
|
||||
iOk = DeleteLinesFromFile%(sFile, lStart, lHowMany)
|
||||
ELSE
|
||||
iOk = 1
|
||||
END IF
|
||||
ELSE
|
||||
iOk = 2
|
||||
END IF
|
||||
ELSEIF lSize = -1 THEN
|
||||
iOk = 3
|
||||
ELSE
|
||||
iOk = 4 ' The file is not a text file
|
||||
IF lStart = 0 THEN lStart = 1
|
||||
IF lStart < lSize THEN
|
||||
PRINT "How many lines do you want to remove (Default=1, Max="; (lSize - lStart) + 1; ")";
|
||||
INPUT lHowMany
|
||||
IF lHowMany = 0 THEN lHowMany = 1
|
||||
END IF
|
||||
ELSE
|
||||
iOk = 5 ' Null file name not allowed
|
||||
END IF
|
||||
PRINT
|
||||
PRINT ErrorMessage$(iOk)
|
||||
PRINT "Do you want to try again? (Y/N)"
|
||||
LOOP UNTIL YorN$ = "N"
|
||||
END IF
|
||||
END IF
|
||||
|
||||
PRINT
|
||||
PRINT "Erasing "; lHowMany; "lines from "; sFile; " starting on line"; lStart; "."
|
||||
IF lSize > 0 THEN
|
||||
IF lHowMany + lStart <= lSize THEN
|
||||
iOk = DeleteLinesFromFile%(sFile, lStart, lHowMany)
|
||||
ELSEIF lHowMany + lStart > lSize THEN
|
||||
iOk = 1
|
||||
ELSEIF lStart > lSize THEN
|
||||
iOk = 2
|
||||
END IF
|
||||
ELSEIF lSize = -1 THEN
|
||||
iOk = 3
|
||||
END IF
|
||||
|
||||
IF lSize = -1 THEN
|
||||
iOk = 3
|
||||
ELSEIF lSize = 0 THEN
|
||||
iOk = 4 ' The file is not a text file
|
||||
END IF
|
||||
|
||||
IF sFile = "" THEN
|
||||
iOk = 5 ' Null file name not allowed
|
||||
END IF
|
||||
|
||||
PRINT
|
||||
PRINT ErrorMessage$(iOk)
|
||||
'----------------End of Main program Cycle ----------------
|
||||
|
||||
END
|
||||
|
|
@ -227,16 +253,54 @@ FUNCTION GetDummyFile$ (WhichFile AS STRING)
|
|||
GetDummyFile$ = LEFT$(WhichFile, i - 1) + "$dummyf$.tmp"
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION YorN$ ()
|
||||
' Var
|
||||
DIM sYorN AS STRING
|
||||
FUNCTION getFileName$ (CommandString AS STRING)
|
||||
' Var
|
||||
DIM i AS INTEGER
|
||||
DIM sFileName AS STRING
|
||||
|
||||
DO
|
||||
sYorN = UCASE$(INPUT$(1))
|
||||
IF INSTR("YN", sYorN) = 0 THEN
|
||||
BEEP
|
||||
END IF
|
||||
LOOP UNTIL sYorN = "Y" OR sYorN = "N"
|
||||
i = INSTR(CommandString, ",")
|
||||
IF i > 0 THEN
|
||||
sFileName = LEFT$(CommandString, i - 1)
|
||||
ELSEIF LEN(CommandString) > 0 THEN
|
||||
sFileName = CommandString
|
||||
END IF
|
||||
|
||||
getFileName$ = sFileName
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION getHowManyLines& (CommandLine AS STRING)
|
||||
' Var
|
||||
DIM i AS INTEGER, j AS INTEGER
|
||||
DIM l AS LONG
|
||||
|
||||
i = INSTR(CommandLine, ",")
|
||||
IF i > 0 THEN
|
||||
j = INSTR(i + 1, CommandLine, ",")
|
||||
IF j = 0 THEN
|
||||
l = 1
|
||||
ELSE
|
||||
l = CLNG(VAL(MID$(CommandLine, j + 1)))
|
||||
END IF
|
||||
END IF
|
||||
|
||||
getHowManyLines& = l
|
||||
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION getStartPoint& (CommandLine AS STRING)
|
||||
' Var
|
||||
DIM i AS INTEGER, j AS INTEGER
|
||||
DIM l AS LONG
|
||||
|
||||
i = INSTR(CommandLine, ",")
|
||||
IF i > 0 THEN
|
||||
j = INSTR(i + 1, CommandLine, ",")
|
||||
IF j = 0 THEN j = LEN(CommandLine)
|
||||
l = CLNG(VAL(MID$(CommandLine, i + 1, j - i)))
|
||||
ELSE
|
||||
i = 1
|
||||
END IF
|
||||
|
||||
getStartPoint& = l
|
||||
|
||||
YorN$ = sYorN
|
||||
END FUNCTION
|
||||
|
|
|
|||
|
|
@ -3,17 +3,9 @@ using System.IO;
|
|||
|
||||
public class Rosetta
|
||||
{
|
||||
/* C# 6 version:
|
||||
public static void Main() => RemoveLines("foobar.txt", start: 1, count: 2);
|
||||
*/
|
||||
|
||||
public static void Main() {
|
||||
RemoveLines("foobar.txt", start: 1, count: 2);
|
||||
}
|
||||
|
||||
static void RemoveLines(string filename, int start, int count = 1) {
|
||||
//Reads and writes one line at a time, so no memory overhead.
|
||||
static void RemoveLines(string filename, int start, int count = 1) =>
|
||||
File.WriteAllLines(filename, File.ReadAllLines(filename)
|
||||
.Where((line, index) => index < start - 1 || index >= start + count - 1));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
sNewFile As String 'Global string for the 'New file' details
|
||||
|
||||
Public Sub Main()
|
||||
Dim sFileName As String = User.Home &/ "foobar.txt" 'File name
|
||||
|
||||
sNewFile = DeleteLines(sFileName, 1, 2) 'Send the details to the DeleteLine routine 'The parameters should be: foobar.txt, 1, 2'
|
||||
Print "New file: -" & gb.NewLine & sNewFile 'Print details of the changed file
|
||||
File.Save(sFileName, sNewFile) 'Save the file with the original name
|
||||
|
||||
End
|
||||
|
||||
Public Sub DeleteLines(sFile As String, siStart As Short, siNum As Short) As String 'DeleteLines routine
|
||||
Dim sData As New String[] 'To store the existing file data
|
||||
Dim siCount As Short 'Counter
|
||||
Dim sTemp, sDel As String 'String variables
|
||||
|
||||
For Each sTemp In Split(File.Load(sFile), gb.NewLine) 'Load the file, split the lines by NewLine
|
||||
sData.Add(sTemp) 'Add to sData
|
||||
Next
|
||||
|
||||
Print "Original file: -" 'Print Title
|
||||
|
||||
If siStart + siNum > sData.max Then 'Check an attempt is made to remove lines beyond the end of the file
|
||||
Print "Not enough lines in file to carry out request" 'An appropriate message should appear if so
|
||||
Return "" 'Return nothing
|
||||
Endif
|
||||
|
||||
Dec siStart 'For the purpose of this task, line numbers start at one (Program starts at 0)
|
||||
|
||||
For siCount = siStart To (siStart + siNum) - 1 'Loop through the lines to be deleted
|
||||
sDel &= Str(siCount) & " " 'Add then to sDel
|
||||
Next
|
||||
|
||||
siCount = -1 'Reset counter
|
||||
|
||||
For Each sTemp In sData 'For each line in the file..
|
||||
Inc siCount 'Increase counter
|
||||
Print sTemp 'Print the line
|
||||
If InStr(sDel, Str(siCount) & " ") Then Continue 'If the line number is listed in sDel then jump to the end of the loop
|
||||
sNewFile &= sTemp & gb.NewLine 'Add the lines not to be removed into sNewFile
|
||||
Next
|
||||
|
||||
Return sNewFile 'Return the details
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.io.File
|
||||
|
||||
fun removeLines(fileName: String, startLine: Int, numLines: Int) {
|
||||
require(!fileName.isEmpty() && startLine >= 1 && numLines >= 1)
|
||||
val f = File(fileName)
|
||||
if (!f.exists()) {
|
||||
println("$fileName does not exist")
|
||||
return
|
||||
}
|
||||
var lines = f.readLines()
|
||||
val size = lines.size
|
||||
if (startLine > size) {
|
||||
println("The starting line is beyond the length of the file")
|
||||
return
|
||||
}
|
||||
var n = numLines
|
||||
if (startLine + numLines - 1 > size) {
|
||||
println("Attempting to remove some lines which are beyond the end of the file")
|
||||
n = size - startLine + 1
|
||||
}
|
||||
lines = lines.take(startLine - 1) + lines.drop(startLine + n - 1)
|
||||
val text = lines.joinToString(System.lineSeparator())
|
||||
f.writeText(text)
|
||||
}
|
||||
|
||||
fun printFile(fileName: String, message: String) {
|
||||
require(!fileName.isEmpty())
|
||||
val f = File(fileName)
|
||||
if (!f.exists()) {
|
||||
println("$fileName does not exist")
|
||||
return
|
||||
}
|
||||
println("\nContents of $fileName $message:\n")
|
||||
f.forEachLine { println(it) }
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
printFile("input.txt", "before removal")
|
||||
removeLines("input.txt", 2, 3)
|
||||
printFile("input.txt", "after removal of 3 lines starting from the second")
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
procedure remove_lines(string filename, integer start, integer n)
|
||||
integer fn = open(filename,'r')
|
||||
if fn!=-1 then
|
||||
sequence lines = get_text(fn,GT_LF_STRIPPED)
|
||||
close(fn)
|
||||
end if
|
||||
if fn=-1 or n<1 or start<1 or length(lines)<start+n-1 then
|
||||
puts(1,"bad parameters!\n")
|
||||
else
|
||||
lines[start..start+n-1] = {}
|
||||
fn = open(filename,'w')
|
||||
puts(fn,join(lines,"\n")
|
||||
close(fn)
|
||||
end if
|
||||
end procedure
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
require 'tempfile'
|
||||
require 'fileutils'
|
||||
require 'English'
|
||||
|
||||
def remove_lines(filename, start, num)
|
||||
tmp = Tempfile.new(filename)
|
||||
File.foreach(filename) do |line|
|
||||
if $NR >= start and num > 0
|
||||
num -= 1
|
||||
else
|
||||
tmp.write line
|
||||
end
|
||||
end
|
||||
tmp.close
|
||||
STDERR.puts "Warning: End of file encountered before all lines removed" if num > 0
|
||||
FileUtils.copy(tmp.path, filename)
|
||||
tmp.unlink
|
||||
end
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
def setup(start, remove)
|
||||
puts "remove #{remove} lines starting at line #{start}"
|
||||
File.open($filename, "w") {|fh| (1..5).each {|i| fh.puts i}}
|
||||
puts "before:\n" + File.read($filename)
|
||||
end
|
||||
|
||||
def teardown
|
||||
puts "after:\n" + File.read($filename)
|
||||
puts ""
|
||||
File.unlink($filename)
|
||||
end
|
||||
|
||||
$filename = "test.file"
|
||||
start = 2
|
||||
[2, 6].each do |remove|
|
||||
setup(start, remove)
|
||||
remove_lines $filename, start, remove
|
||||
teardown
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
* drop lines 20 to 30
|
||||
drop in 20/30
|
||||
|
||||
* keep lines 20 to 30, and remove everything else
|
||||
keep in 20/30
|
||||
|
|
@ -0,0 +1 @@
|
|||
sed -i "" "$start,${end}d" "$file"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fcn deleteLinesM(fname, start,num){
|
||||
blob:=File(fname).read(); // file to memory
|
||||
n:=blob.seek(Void,start-1); // seek to line and remember it
|
||||
blob.del(n,blob.seek(Void,num)-n);
|
||||
|
||||
File.stdout.write(blob);
|
||||
}
|
||||
deleteLinesM("nn.zkl", 2,5);
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fcn deleteLinesL(fname, start,num){
|
||||
if (start < 1) throw(Exception.ValueError);
|
||||
f:=File(fname);
|
||||
do(start-1) { File.stdout.write(f.readln()) }
|
||||
do(num) { f.readln(); }
|
||||
f.pump(File.stdout.write);
|
||||
}
|
||||
deleteLinesL("nn.zkl", 2,5);
|
||||
Loading…
Add table
Add a link
Reference in a new issue