newLISP Fan Club

Forum => Anything else we might add? => Topic started by: tom on March 27, 2007, 03:12:26 PM

Title: deleting lines from a text file
Post by: tom on March 27, 2007, 03:12:26 PM
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!
Title:
Post by: newdep on March 27, 2007, 03:24:46 PM
I would use 'Currentline , 'Seek and 'Search
Title:
Post by: m35 on March 27, 2007, 06:07:58 PM
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).
Title:
Post by: cormullion on March 28, 2007, 01:23:24 AM
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'..
Title:
Post by: tom on March 28, 2007, 03:39:44 AM
What is the f ?
Title:
Post by: cormullion on March 28, 2007, 03:58:24 AM
:-) a list of strings. You called it 'a'.