Author Topic: Removing Lines from Text File  (Read 8344 times)

0 Members and 1 Guest are viewing this topic.

ziko30

  • Newt
  • Posts: 51
Removing Lines from Text File
« 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.
============== Acad Map 3D 2009================

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Removing Lines from Text File
« Reply #1 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))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ziko30

  • Newt
  • Posts: 51
Re: Removing Lines from Text File
« Reply #2 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.
============== Acad Map 3D 2009================

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Removing Lines from Text File
« Reply #3 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))
     

« Last Edit: January 12, 2010, 05:15:46 PM by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Removing Lines from Text File
« Reply #4 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 ^^  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: Removing Lines from Text File
« Reply #5 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)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Removing Lines from Text File
« Reply #6 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)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ziko30

  • Newt
  • Posts: 51
Re: Removing Lines from Text File
« Reply #7 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.
============== Acad Map 3D 2009================

ziko30

  • Newt
  • Posts: 51
Re: Removing Lines from Text File
« Reply #8 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.
============== Acad Map 3D 2009================

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Removing Lines from Text File
« Reply #9 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))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Removing Lines from Text File
« Reply #10 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)
)
« Last Edit: January 14, 2010, 08:28:22 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Removing Lines from Text File
« Reply #11 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:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Removing Lines from Text File
« Reply #12 on: January 13, 2010, 03:06:59 PM »
Good catch Lee 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ziko30

  • Newt
  • Posts: 51
Re: Removing Lines from Text File
« Reply #13 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!
============== Acad Map 3D 2009================

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Removing Lines from Text File
« Reply #14 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:  :-)