Task

Remove a specific line or a number of lines from a file.

This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed).

For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2

Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed.

An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.




* Remove lines from a file
 
* Function to remove a chunk of lines from a file, will go backward, too, if numlines < 0
    define("remlines(filename,startline,numlines)row,rowcount,lastline,swap")
    :(remlines_end)
remlines
	input(.inp1,1,filename)		:f(freturn)
	output(.tmp1,2,"/tmp/Removem")	:f(freturn)
	rowcount = 0
	lastline = (ge(numlines,0) startline + numlines - 1, startline + numlines + 1)
	ge(numlines,0) :s(remlines2)
	swap = lastline
	lastline = startline
	startline = swap
remlines2 
	row = inp1			:f(remlines3)
	rowcount = rowcount + 1
	ge(rowcount,startline) le(rowcount,lastline)	:s(remlines2)
	tmp1 = row
	:(remlines2)
remlines3
	endfile(1)
	endfile(2)
	input(.rmv1,2,"/tmp/Removem")	:f(freturn)
	output(.fnout1,1,filename)	:f(freturn)
remlines4
	fnout1 = rmv1			:s(remlines4)
	endfile(1)
	endfile(2)
	:(return)
remlines_end
 
	testfile = "/tmp/foobar.txt"
 
* Populate test file with 5 rows, 1, 2, 3, 4, and 5
	output(.testout,60,testfile) :f(failed)
	i = 1
pop1
	testout = i
	i = lt(i,5) i + 1 :s(pop1)
	endfile(60)
 
 
* Now remove lines 2, 3, and 4 from test file
	remlines(testfile,2,3) :f(failed)
 
 
* Print revised file
	input(.testprt,70,testfile) :f(failed)
print1
	output = testprt :s(print1)
	:(end)
 
failed
	output = "Either the main file or the temporary file failed to open"
end
 
 