Author Topic: Removing Lines from Text File  (Read 8342 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: 12912
  • 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: 12912
  • 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: 12912
  • 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: 12912
  • 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:  :-)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Removing Lines from Text File
« Reply #15 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

Lee Mac

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

« Last Edit: January 13, 2010, 03:52:49 PM by Lee Mac »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Removing Lines from Text File
« Reply #17 on: January 13, 2010, 04:01:30 PM »
Yes '(t nil nil nil)
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: 12912
  • London, England
Re: Removing Lines from Text File
« Reply #18 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:

VovKa

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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Removing Lines from Text File
« Reply #20 on: January 13, 2010, 04:41:56 PM »
I better have a close look. 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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Removing Lines from Text File
« Reply #21 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
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: 12912
  • London, England
Re: Removing Lines from Text File
« Reply #22 on: January 13, 2010, 05:06:42 PM »
I see, very clever.
But this works.

It does indeed  8-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Removing Lines from Text File
« Reply #23 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 ) ...

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Removing Lines from Text File
« Reply #25 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 :)

 :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

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

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Removing Lines from Text File
« Reply #27 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Removing Lines from Text File
« Reply #28 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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Removing Lines from Text File
« Reply #29 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?
TheSwamp.org  (serving the CAD community since 2003)

ziko30

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