TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ziko30 on January 12, 2010, 04:05:59 PM

Title: Removing Lines from Text File
Post by: ziko30 on January 12, 2010, 04:05:59 PM
Hello All,
   
   I am not sure if this can be done because I don't have in depth AutoLisp knowledge, but if it is possible: I have a text file that I want to clean up. I need to remove every 3 out of 4 lines starting at the top and keeping the very first line(see example file).
   When the text lines are removed, I want the space created between the lines if any to be removed also. Suggestions of other ways to get this done are welcome.
   Thanks.
Title: Re: Removing Lines from Text File
Post by: ronjonp on January 12, 2010, 04:27:16 PM
If I'm understanding what you want (keep every 4th line) then this should do it:

(while (setq strlst (cdddr strlst)) (write-line (car strlst) openfile))
Title: Re: Removing Lines from Text File
Post by: ziko30 on January 12, 2010, 04:55:00 PM
If I'm understanding what you want (keep every 4th line) then this should do it:

(while (setq strlst (cdddr strlst)) (write-line (car strlst) openfile))

Thx for your time Ron: I want to keep 1 line out of every 4. Keep the very first line and delete the next 3 and then repeat.
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 12, 2010, 04:58:10 PM
Perhaps:

Code: [Select]
(defun c:Remove_Lines

       (/ *error*   ;; Local Function

        file open_file new_line String_List first_line)

  ;;  --------{ Error Handler }--------

  (defun *error* (msg)
   
    (and open_file (close open_file))
   
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ))

  ;;  --------{ File Retrieval }--------
 
  (if (setq file (getfiled "Select File" "" "txt" 16))
    (progn
     
      ;;  --------{ Reading File }--------
     
      (setq open_file (open file "r"))
     
      (while (setq new_line (read-line open_file))
        (setq String_List (cons new_line String_List)))
     
      (setq String_List (reverse String_List) open_file (close open_file))
     
      ;;  --------{ Writing File }--------
     
      (setq open_file (open file "w"))
     
      (while (setq first_line (car String_List))
        (write-line first_line open_file)
        (setq String_List (cddddr String_List)))
     
      (setq open_file (close open_file)))

    (princ "\n*Cancel*"))

  (princ))
     

Title: Re: Removing Lines from Text File
Post by: ronjonp on January 12, 2010, 05:01:33 PM
Perhaps:

Code: [Select]
(while (setq x (car strLst))
  (write-line x openfile)
  (setq strLst (cddddr strLst)))

What he said ^^  :-)
Title: Re: Removing Lines from Text File
Post by: VovKa on January 12, 2010, 05:49:10 PM
or
Code: [Select]
(while (setq new_line (read-line open_file))
   (repeat 3 (read-line open_file))
   (write-line new_line new_file)
)
Title: Re: Removing Lines from Text File
Post by: CAB on January 12, 2010, 05:57:23 PM
Here is a twist on Lee's routine. Catching file errors I hope.
Code: [Select]
;;  CAB version of Lee's routine
;;  Create a new text file with every 4 lines for source file
(defun c:Remove_Lines
       (/ *error*   ;; Local Function
        file open_file new_line String_List first_line)

  ;;  --------{ Error Handler }--------
  (defun *error* (msg)  
    (and (= (type open_file) 'FILE) (close open_file))
    (or (and (null msg) (Princ "\nFinished."))
(wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ))

  ;;  --------{ File Retrieval }--------
  (if (and (setq file (getfiled "Select File" "" "txt" 16))
  (or (setq open_file (open file "r"))
      (prompt "\nCould not Open File for reading.")))
    (progn
      ;;  --------{ Reading File }--------
      (while (setq new_line (read-line open_file))
        (setq String_List (cons new_line String_List)))
      (setq open_file (close open_file))
      (setq String_List (reverse String_List))
      
      ;;  --------{ Writing File }--------
      (setq file (strcat (substr file 1 (-(strlen file)4)) "New.txt"))
      (if (or (setq open_file (open file "w"))
     (prompt "\nCould not Open File for writing."))
(progn
          (while (setq first_line (car String_List))
             (write-line first_line open_file)
             (setq String_List (cddddr String_List)))
          (print file)
          (setq open_file (close open_file))
        )
       )
    )
  )
  (*error* nil)
  (princ)
)
Title: Re: Removing Lines from Text File
Post by: ziko30 on January 12, 2010, 06:11:47 PM
Thanks all... I tried Lee's = Ron's.   It worked great. I have several files of about 1.2 million lines to reduce. I will look at VovKa and CAB's to see if I can learn a thing two.
Title: Re: Removing Lines from Text File
Post by: ziko30 on January 13, 2010, 12:12:48 PM
Thanks CAB ... writing to a new text file helps. I just wish I knew  which part of the code to modify if I needed to keep, say every 5th line, 6th line or some other number instead of the 4th.
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 12:20:33 PM
Thanks CAB ... writing to a new text file helps. I just wish I knew  which part of the code to modify if I needed to keep, say every 5th line, 6th line or some other number instead of the 4th.

Check this:

Code: [Select]
;;  Lee Mac's Version of CAB version of Lee's routine
;;  Create a new text file with every 4 lines for source file

(defun c:Remove_Lines
       
       (/ *error* Remove_n   ;; Local Function
       
        file open_file new_line String_List first_line)

  (defun remove_n (n lst / i)
    (setq i -1)
    (vl-remove-if
      (function (lambda (x) (< (setq i (1+ i)) n))) lst))

  ;;  --------{ Error Handler }--------
 
  (defun *error* (msg) 
    (and (= (type open_file) 'FILE) (close open_file))
   
    (or (and (null msg) (Princ "\nFinished."))
(wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
   
    (princ))

  ;;  --------{ File Retrieval }--------
 
  (if (and (setq file (getfiled "Select File" "" "txt" 16))
   (or (setq open_file (open file "r"))
       (prompt "\nCould not Open File for Reading.")))
    (progn
      ;;  --------{ Reading File }--------
     
      (while (setq new_line (read-line open_file))
        (setq String_List (cons new_line String_List)))
      (setq open_file (close open_file))
      (setq String_List (reverse String_List))
     
      ;;  --------{ Writing File }--------
     
      (setq file (strcat (substr file 1 (- (strlen file) 4)) "New.txt"))
     
      (if (or (setq open_file (open file "w"))
      (prompt "\nCould not Open File for writing."))
(progn
         
          (while (setq first_line (car String_List))
             (write-line first_line open_file)
             (setq String_List (Remove_n [color=red]4[/color] String_List))) ;; [color=red]Remove 3 lines[/color]
         
          (print file)
          (setq open_file (close open_file))))))
 
  (princ))
Title: Re: Removing Lines from Text File
Post by: CAB on January 13, 2010, 02:09:31 PM
One More:  :-)
Code: [Select]
;;  CAB version of Lee's routine
;;  Create a new text file with every 4 lines for source file
(defun c:Remove_Lines
       (/ *error*   ;; Local Function
        file open_file new_line String_List first_line FileExt every i)

[color=red]  ;;  --------{ User Options }--------
  (setq FileExt  "New.txt") ; FileName.txt --->  FileNameNew.txt
  (setq every 4) ; copy every 4th line[/color]
  
  ;;  --------{ Error Handler }--------
  (defun *error* (msg)  
    (and (= (type open_file) 'FILE) (close open_file))
    (or (and (null msg) (Princ "\nFinished."))
(wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ))

  ;;  --------{ File Retrieval }--------
  (if (and (setq file (getfiled "Select File" "" "txt" 16))
  (or (setq open_file (open file "r"))
      (prompt "\nCould not Open File for reading.")))
    (progn
      ;;  --------{ Reading File }--------
      (while (setq new_line (read-line open_file)) ; read every line
        (setq String_List (cons new_line String_List)))
      (setq open_file (close open_file))
      (setq String_List (reverse String_List))
      
      ;;  --------{ Writing File }--------
      (setq file (strcat (substr file 1 (-(strlen file)4)) FileExt))
      (if (or (setq open_file (open file "w"))
     (prompt "\nCould not Open File for writing."))
(progn
 (setq i 0.)
          (while (setq str (nth (fix i) String_List))
   (if (zerop (rem i every))
             (write-line str open_file)
   )
             (setq i (1+ i)))
          (print file)
          (setq open_file (close open_file))
        )
       )
    )
  )
  (*error* nil)
  (princ)
)
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 02:48:50 PM
Very nice idea Alan!

Only thing I would change:

Code: [Select]
(setq i -1.)
(while (setq str (nth (fix (setq i (1+ i))) String_List))
  (if (zerop (rem i every))
    (write-line str open_file)))

But that's just being petty  :wink:
Title: Re: Removing Lines from Text File
Post by: CAB on January 13, 2010, 03:06:59 PM
Good catch Lee 8-)
Title: Re: Removing Lines from Text File
Post by: ziko30 on January 13, 2010, 03:33:28 PM
Good catch Lee 8-)

Lee & CAB: Tested both...worked as advertised. This must be a walk in the park for you guys... but many thanks!
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 03:35:53 PM
Good catch Lee 8-)

Lee & CAB: Tested both...worked as advertised. This must be a walk in the park for you guys... but many thanks!

You're welcome, I had fun with it   :wink:  :-)
Title: Re: Removing Lines from Text File
Post by: ElpanovEvgeniy on January 13, 2010, 03:37:11 PM
other version...

Code: [Select]
(defun f (a b c) (and a b (write-line b c)))
(defun c:test (/  c d)
 ;; '(nil nil nil t) - Pattern of lines of the text for record
 (setq d (getfiled "Select File" "" "txt" 16)
       c (open (strcat d "-New.txt") "w")
       d (open d "r")
 ) ;_  setq
 (while (foreach a '(nil nil nil t) (f a (read-line d) c)))
 (close d)
 (close c)
) ;_  defun
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 03:44:32 PM
Very nice solution Evgeniy, although I think he/she wanted to keep the first line, and discard the rest (other three), so that while loop may need some re-jigging...  :wink:

But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:

Title: Re: Removing Lines from Text File
Post by: CAB on January 13, 2010, 04:01:30 PM
Yes '(t nil nil nil)
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 04:34:34 PM
Yes '(t nil nil nil)

Not quite.

The While loop relies on the 't' being last...  :wink:
Title: Re: Removing Lines from Text File
Post by: VovKa on January 13, 2010, 04:41:51 PM
But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:
i believe Evgeniy has a Kelvinator for "forum-posting" purposes :)
Title: Re: Removing Lines from Text File
Post by: CAB on January 13, 2010, 04:41:56 PM
I better have a close look. 8-)
Title: Re: Removing Lines from Text File
Post by: CAB on January 13, 2010, 04:58:44 PM
I see, very clever.
But this works.
Code: [Select]
(defun f (flag str fn) (and flag str (write-line str fn)))
(defun c:test (/ fname fnr fnw s)
 ;; '(nil nil nil t) - Pattern of lines of the text for record
 (setq fname (getfiled "Select File" "" "txt" 16)
       fnw (open (strcat fname "-New.txt") "w")
       fnr (open fname "r")
 ) ;_  setq
 (while (foreach a '(t nil nil nil) (f a (setq s (read-line fnr)) fnw) s))
 (close fnw)
 (close fnr)
) ;_  defun
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 05:06:42 PM
I see, very clever.
But this works.

It does indeed  8-)
Title: Re: Removing Lines from Text File
Post by: JohnK on January 13, 2010, 05:27:48 PM
<snip>
But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:

heh, Dave Stein used to use vowels. *lmao*
(defun blah ( / a e i o u ) ...

Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 05:53:31 PM
<snip>
But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:

heh, Dave Stein used to use vowels. *lmao*
(defun blah ( / a e i o u ) ...



Haha, reminds me of an article I remember someone posting on here describing 'good programming practices', like having two variables like:

Code: [Select]
(defun blah (/ lI Il ...

And using comments that describe the code as doing something completely different...

trying to find the thread now...
Title: Re: Removing Lines from Text File
Post by: Kerry on January 13, 2010, 05:53:46 PM
But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:
i believe Evgeniy has a Kelvinator for "forum-posting" purposes :)

 :-)
Title: Re: Removing Lines from Text File
Post by: Lee Mac on January 13, 2010, 05:56:13 PM
<snip>
But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:

heh, Dave Stein used to use vowels. *lmao*
(defun blah ( / a e i o u ) ...



Haha, reminds me of an article I remember someone posting on here describing 'good programming practices', like having two variables like:

Code: [Select]
(defun blah (/ lI Il ...

And using comments that describe the code as doing something completely different...

trying to find the thread now...

Ahh yes (http://www.theswamp.org/index.php?topic=1786.msg22798#msg22798)
Title: Re: Removing Lines from Text File
Post by: JohnK on January 13, 2010, 06:18:23 PM
<snip>
Ahh yes
*click-click* Oh my goodness. yeah dont do that. just put the keyboard down and step away.
Title: Re: Removing Lines from Text File
Post by: ElpanovEvgeniy on January 14, 2010, 01:03:45 AM
But I do wish that you would give you variables some meaningful names... makes it so much easier to follow...  :wink:
i believe Evgeniy has a Kelvinator for "forum-posting" purposes :)

I also am Kelvinator...  8-)

It is convenient me, when local variables short, they do not prevent to see music of the program and its rhythm.
Long names of variables, can borrow more places, than sense the code.
Title: Re: Removing Lines from Text File
Post by: Mark on January 14, 2010, 07:54:14 AM
Thanks CAB ... writing to a new text file helps. I just wish I knew  which part of the code to modify if I needed to keep, say every 5th line, 6th line or some other number instead of the 4th.
Is there some kind of pattern you are looking for?
Title: Re: Removing Lines from Text File
Post by: ziko30 on January 14, 2010, 08:45:52 AM
Thanks CAB ... writing to a new text file helps. I just wish I knew  which part of the code to modify if I needed to keep, say every 5th line, 6th line or some other number instead of the 4th.
Is there some kind of pattern you are looking for?

No, I am not looking for a pattern. These are grid coordinates and I needed to reduce the size of my files and spread out the data to speed up importing data into an AutoCAD vertical product. I keep the first line because it is the origin of my grid. Some of the files have about 1.5 million lines and others about 300 thousand lines. I just want about 30 thousand lines so I need to take out more lines in some files and less in others.