deleting lines from a text file

For the Compleat Fan
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

deleting lines from a text file

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

m35
Posts: 171
Joined: Wed Feb 14, 2007 12:54 pm

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

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

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

What is the f ?

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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

Locked