TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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.
-
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))
-
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.
-
Perhaps:
(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))
-
Perhaps:
(while (setq x (car strLst))
(write-line x openfile)
(setq strLst (cddddr strLst)))
What he said ^^ :-)
-
or
(while (setq new_line (read-line open_file))
(repeat 3 (read-line open_file))
(write-line new_line new_file)
)
-
Here is a twist on Lee's routine. Catching file errors I hope.
;; 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)
)
-
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.
-
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.
-
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:
;; 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))
-
One More: :-)
;; 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)
)
-
Very nice idea Alan!
Only thing I would change:
(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:
-
Good catch Lee 8-)
-
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!
-
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: :-)
-
other version...
(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
-
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:
-
Yes '(t nil nil nil)
-
Yes '(t nil nil nil)
Not quite.
The While loop relies on the 't' being last... :wink:
-
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 better have a close look. 8-)
-
I see, very clever.
But this works.
(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 see, very clever.
But this works.
It does indeed 8-)
-
<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 ) ...
-
<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:
(defun blah (/ lI Il ...
And using comments that describe the code as doing something completely different...
trying to find the thread now...
-
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 :)
:-)
-
<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:
(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)
-
<snip>
Ahh yes
*click-click* Oh my goodness. yeah dont do that. just put the keyboard down and step away.
-
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.
-
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?
-
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.