Page 1 of 1

deleting lines from a text file

Posted: Tue Mar 27, 2007 10:12 pm
by 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.

Code: Select all

(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!

Posted: Tue Mar 27, 2007 10:24 pm
by newdep
I would use 'Currentline , 'Seek and 'Search

Posted: Wed Mar 28, 2007 1:07 am
by m35
So you have a text file like this?

Code: Select all

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!)

Code: Select all

(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).

Posted: Wed Mar 28, 2007 8:23 am
by cormullion
You can remove a line:

Code: Select all

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


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

Code: Select all

(nth-set (f 4) "") ; line 4, 0-based
(clean empty? f)
or empty a line containing a string pattern

Code: Select all

(nth-set (f (find "line 4" f 0)) "")
or empty a sequence of lines that contain a string

Code: Select all

(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:

Code: Select all

(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'..

Posted: Wed Mar 28, 2007 10:39 am
by tom
What is the f ?

Posted: Wed Mar 28, 2007 10:58 am
by cormullion
:-) a list of strings. You called it 'a'.