deleting lines from a text file

Started by tom, March 27, 2007, 03:12:26 PM

Previous topic - Next topic

tom

howdy guys,



I have a text file, and I want to delete all the lines before a certain line, and after a certain line.


(set 'a (parse (read-file "chapter-1.txt") "n"))

(a 22) happens to be "-------"

I want (a 24) back to (a 0) deleted.



I need to find another string, at whatever position, and delete to the end of the file.



What's the best way to do this?



Thanks!

newdep

#1
I would use 'Currentline , 'Seek and 'Search
-- (define? (Cornflakes))

m35

#2
So you have a text file like this?


Line
   1 <some>
   2 <some>
    ...
  21 <some>
  22 -------
  23 <some>
  24 <some>
  25 <some>
    ...
   n "another string"
    ...
 eof

You want to remove lines 1 to 24, then find "another string" and delete from there to the end of the file?



After your line of code, something like this might do the trick (UNTESTED!)
(set 'a-rest (join (slice a 24) "n"))
(slice a-rest 0 (find "another string" a-rest))

As newdep said, you could also perform this task using such functions as (read-line), (current-line), (seek) and (search).

cormullion

#3
You can remove a line:


(replace {-------} f)



or empty a numbered line with an empty string (and clean it later):


(nth-set (f 4) "") ; line 4, 0-based
(clean empty? f)


or empty a line containing a string pattern


(nth-set (f (find "line 4" f 0)) "")

or empty a sequence of lines that contain a string


(println f)
(dolist (i (ref-all "-------" f))
  (nth-set (f i) ""))


or make a new list by extracting a sequence of line numbers starting at a matching line:


(select f (sequence (find "line 4" f 0) (length f)))

(lots of scope with select, with the list-selection...)



It might depend on how big your file is to start with, but you probably won't notice too many problems with a 'chapter'..

tom

#4
What is the f ?

cormullion

#5
:-) a list of strings. You called it 'a'.