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.




 
(context 'ABC)
 
(define (remove-lines-from-a-file filename start num)    
    (setf new-content "")
    (setf row-counter 0)
    (setf start-delete-row start)
    (setf end-delete-row (+ start num -1))
    (setf file-content (read-file filename))
    (setf max-rows (length (parse file-content "\n" 0)))
 
    (cond
        ((<= start 0)
            (println "Start line must be >= 1. Value passed: " start))
        ((<= num 0)
            (println "# of lines to remove must be >= 1. Value passed: " num))
        ((> start max-rows)
            (println "Start line must be <= " max-rows ". Value passed: " start))
        ((> end-delete-row max-rows)
            (println "Not so much lines available to be removed. Max " (- max-rows start-delete-row) ". Value passed: " num))
        (true
            (dolist (row (parse file-content "\n" 0))
                (++ row-counter)
                (if (or (< row-counter start-delete-row) (> row-counter end-delete-row))
                    (setf new-content (append new-content row "\n")) 
                    )
            )
            (write-file filename new-content)
        )
    )
)
 
(context 'MAIN)
 
(ABC:remove-lines-from-a-file "foobar.txt" 8 3)
(exit)
 