TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Dommy2Hotty on December 08, 2004, 02:32:49 PM

Title: Search a text file and return find
Post by: Dommy2Hotty on December 08, 2004, 02:32:49 PM
Anyone (CAB) have a lisp that would search a text file for a certain word and return any lines that contain that word?  I want to make a text file containing all of our jobs in this format:
Code: [Select]

2004-160-WD-Newport-Lot115-AR
2004-161-WD-Durbin-Lot64-JW
2004-162-BW-Caroline
2004-163-BW-Caroline-3 Car

Then I was thinking the lisp would call a DCL file that would have a user input field for the search item (e.g. Caroline).  The results would be displayed in a box in the dialog box
Code: [Select]

Results:
2004-162-BW-Caroline
2004-163-BW-Caroline-3 Car


This would be used to find job numbers quickly...I'm assuming AutoCAD could search a text file a lot faster than Windows can search a 20 gig backup for filenames...
Title: Search a text file and return find
Post by: M-dub on December 08, 2004, 02:36:03 PM
It's not a lisp routine, but DrawingSearcher will find strings of text within drawings and tell you which ones they are.  You can then bring the results into a spreadsheet if you want a drawing list.  It will also search through many other file formats.

Don't know if that helps at all or not...
Title: Search a text file and return find
Post by: Dommy2Hotty on December 08, 2004, 02:46:23 PM
Not in this particular case...but thank you

I'm going to write this code, but you know how it goes...if someone already made it and you have access to it, why spend the time to code it?  Just fishing in The Swamp...
Title: Search a text file and return find
Post by: M-dub on December 08, 2004, 02:50:34 PM
Can't say that I blame you! ;)

Just wait...when you finish writing the code for it, you'll find out that someone already did it.  That's what Murphy told me, anyway.  :razz:
Title: Search a text file and return find
Post by: Dommy2Hotty on December 08, 2004, 03:08:41 PM
Quote from: M-dub
Can't say that I blame you! ;)

Just wait...when you finish writing the code for it, you'll find out that someone already did it.  That's what Murphy told me, anyway.  :razz:


Ain't that the truth...
Title: Search a text file and return find
Post by: David Bethel on December 08, 2004, 03:27:29 PM
Code: [Select]
(defun c:stxt (/ output file search nl tc rf tl)
   (setq file (if txt_file
           (getfiled "TXT File to Search" txt_file "txt" 2)
           (getfiled "TXT File to Search" "/acad/" "txt" 2)))
   (setq txt_file file)

   (setq search (getstring t "\nText To Search For:   "))

   (initget 1 "Yes No")
   (setq tc (getkword "\nCase Sensative Search (Y/N):   "))

   (setq rf (open file "r"))
   (while (setq nl (read-line rf))
          (if (= tc "No")
              (setq tl (strcase nl)
                    search (strcase search))
              (setq tl nl))
          (if (wcmatch tl (strcat "*" search "*"))
              (setq output (cons nl output))))
   (close rf)

   (prin1 output))



It would need some tweaking for your exact use, but returns a list of all strings containing the search phrase.  -David
Title: Search a text file and return find
Post by: Dommy2Hotty on December 08, 2004, 03:30:43 PM
Murphey strikes again!!!

Thanks David...I'll start with that!
Title: Search a text file and return find
Post by: M-dub on December 08, 2004, 03:31:58 PM
:lol:
Title: Search a text file and return find
Post by: Dommy2Hotty on December 08, 2004, 04:35:25 PM
Thanks again David...here's my modified version...
Code: [Select]

(defun c:Job (/ output file search nl tc rf tl)

   (setq search (getstring t "\nText To Search For:   "))
 
   (setq tc "No")

   (setq rf (open "Z:/JobSupport/Job_List.txt" "r"))
   (while (setq nl (read-line rf))
          (if (= tc "No")
              (setq tl (strcase nl)
                    search (strcase search))
              (setq tl nl))
          (if (wcmatch tl (strcat "*" search "*"))
              (setq output (cons nl output))))
   (close rf)

   (foreach n output (print n))
  (textscr)
  )
Title: Search a text file and return find
Post by: David Bethel on December 08, 2004, 06:48:21 PM
A bit condensed

  1| (
defun c:Job (/ output search nl rf)
  2|  
  3|   (
setq search (getstring t "\nText To Search For:   "))
  4|  
  5|   (
setq rf (open "Z:/JobSupport/Job_List.txt" "r"))
  6|  
  7|   (
while (setq nl (read-line rf))
  8|          (
if (wcmatch (strcase nl)
  9|                       (
strcat "*" (strcase search) "*"))
 10|              (
setq output (cons nl output))))
 11|   (
close rf)
 12|  
 13|   (
textpage)
 14|   (
foreach n output (print n))
 15|   (
prin1))


Now to figure out how to have a monspaced font.....

-David
Title: Search a text file and return find
Post by: Dommy2Hotty on December 08, 2004, 07:22:58 PM
; error: too many arguments

by the by...colorful...might I inquire as to what you used to color that?
Title: Search a text file and return find
Post by: Keith™ on December 08, 2004, 08:03:01 PM
Never used lisp to do it ... well not exclusively ... I always used "FIND" and specified a redirector to a file ....

Code: [Select]

find /I "%1" "%2" >> output.txt


Put it in a batch file, supply the search string and path or file to search ...
Title: Search a text file and return find
Post by: David Bethel on December 09, 2004, 07:49:41 AM
It a lsp 2 htm program I wrote a while back.  It is more for true browser use than NG posts.

You would have to get rid of all of the line numbers for the routine to work properly.  Kinda more of an instuctional tool.  I usually provide a link at the end to a working version.

http://www.davidbethel.com/lisp/job.htm

-David
Title: Search a text file and return find
Post by: Dommy2Hotty on December 26, 2004, 05:27:50 AM
Got another question concerning this...Is there a way to just return the newest revision of the output?  The revision number in our zip files come after the job number, which comes after the date...you'll be able to tell...

Code: [Select]
"2003-015-12-WD-Newport.zip"
"2003-015-11-WD-Newport.zip"
"2003-015-10-WD-Newport.zip"
"2003-015-09-WD-Newport.zip"
"2003-015-08-WD-Newport.zip"
"2003-015-07-WD-Newport.zip"
"2003-015-06-WD-Newport.zip"
"2003-015-05-WD-Newport.zip"
"2003-015-04-WD-Newport.zip"
"2003-015-03-WD-Newport.zip"
"2003-015-02-WD-Newport.zip"
"2003-015-01-WD-Newport.zip"


I just want it to return the 2003-015-12-WD-Newport.zip, not all previous revisions.
Title: Search a text file and return find
Post by: David Bethel on December 26, 2004, 09:22:12 AM
In the sample you posted, it looks like (last (acad_strlsort  list)) would work
Code: [Select]

(last
  (acad_strlsort
    (list
"2003-015-12-WD-Newport.zip"
"2003-015-11-WD-Newport.zip"
"2003-015-10-WD-Newport.zip"
"2003-015-09-WD-Newport.zip"
"2003-015-08-WD-Newport.zip"
"2003-015-07-WD-Newport.zip"
"2003-015-06-WD-Newport.zip"
"2003-015-05-WD-Newport.zip"
"2003-015-04-WD-Newport.zip"
"2003-015-03-WD-Newport.zip"
"2003-015-02-WD-Newport.zip"
"2003-015-01-WD-Newport.zip"
)))


-David