56 lines
1.3 KiB
Text
56 lines
1.3 KiB
Text
* 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
|