Author Topic: (Challenge) find strings in file  (Read 3562 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challenge) find strings in file
« on: March 29, 2005, 12:45:45 PM »
Write a function that, given a valid file name, returns all the lines that contain the search string.

example:
Code: [Select]

(defun return-found (file_name search_string)
  (foreach line-in-file
  (if line-has-search-string
    ...
    )
  )
  output-lines-that-match
  )

 
No time limit, post it if ya got it, or when you get it.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
(Challenge) find strings in file
« Reply #1 on: March 29, 2005, 12:52:01 PM »
Suggestion: Add a flag to the function declaration so that the function can perform case sensitive / insensitive searches.

i.e.

Code: [Select]
(defun FileScan ( fileName scanText caseFlag )

   ...

)


Where caseFlag would be nil or non nil.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challenge) find strings in file
« Reply #2 on: March 29, 2005, 12:59:50 PM »
Good idea MP. Let's include that as bonus points.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
(Challenge) find strings in file
« Reply #3 on: March 29, 2005, 02:12:09 PM »
So it's ok to post a quick and dirty little solution? I thought (at one time at band camp) that you or se7en had everyone defer posting their solutions until 'x' time was up. Was I dreamin'?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challenge) find strings in file
« Reply #4 on: March 29, 2005, 02:37:14 PM »
No you were not dreaming. I gave up on that idea because even after a 24 hour period it was still the same folks posting to these challenges. So, yes it's ok to post what you have, let's see your "quick and dirty" solution. :)
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
(Challenge) find strings in file
« Reply #5 on: March 29, 2005, 02:46:45 PM »
Well ... unless I'm missing something fundamentally all that's required is --

Code: [Select]
(vl-remove-if-not
   '(lambda (string) (wcmatch string pattern))
    listOfStrings
)

but in the interests of reusable / convenience functions perhaps this --

Code: [Select]
(defun FileToList ( filename / handle result )

    ;;================================================================
    ;;
    ;;  FileToList ( filename )
    ;;
    ;;  If the specified file can be opened as input return the file
    ;;  as an ordered list of strings.
    ;;
    ;;  © 199?-2005 Michael Puckett
    ;;
    ;;================================================================
    ;;
    ;;  e.g. (FileToList (findfile "acad.mnl"))
    ;;
    ;;  (
    ;;
    ;;      (";;;     ACAD.MNL")
    ;;      (";;;     Copyright (C) 1992 - 1997 by Autodesk, Inc.")
    ;;      (";;;")
    ;;      (";;;     Permission to use, copy, modify, and dis...")
    ;;      (";;;     for any purpose and without fee is hereb...")
    ;;      (";;;     that the above copyright notice appears ...")
    ;;      (";;;     that both that copyright notice and the ...")
    ;;      (";;;     restricted rights notice below appear in...")
    ;;      (";;;     documentation.")
    ;;
    ;;      ...
    ;;
    ;;  )
    ;;
    ;;================================================================

    (cond
        (   (and
                (eq 'str (type filename))
                (setq handle (open filename "r"))
            )
            (while (setq stream (read-line handle))
                (setq result
                    (cons
                        stream
                        result
                    )
                )
            )
            (close handle)
            (reverse result)
        )
    )
)

and this --

Code: [Select]
(defun WCMatchFile ( filename pattern caseSensitive / foo i )

    ;;================================================================
    ;;
    ;;  WCMatchFile ( filename pattern caseSensitive )
    ;;
    ;;  Return a list of all lines in a file that match the pattern.
    ;;
    ;;  © 2005 Michael Puckett
    ;;
    ;;================================================================
    ;;
    ;;  Using the format of --
    ;;
    ;;      (   (LineNumber "The text ...")
    ;;          (LineNumber "More text ...")
    ;;          ...
    ;;      )
    ;;
    ;;  e.g. (WCMatchFile (findfile "acad.mnl") "*defun*" nil)
    ;;
    ;;      (
    ;;
    ;;          (32 "[defun ai_tiledvp_chk [new]")
    ;;          (52 "[defun ai_tiledvp [num ori]")
    ;;          (119 "[defun ai_popmenucfg [param]")
    ;;          (127 "[defun ai_putmenucfg [param cfgval]")
    ;;          (131 "[defun *merr* [msg]")
    ;;          (137 "[defun *merrmsg* [msg]")
    ;;          (144 "[defun ai_showedge_alert []")
    ;;          (149 "[defun ai_hideedge_alert []")
    ;;          (154 "[defun ai_viewports_alert []")
    ;;          (160 "[defun ai_refedit_alert []")
    ;;          (166 "[defun ai_support_assistance_alert []")
    ;;          (171 "[defun la_support_assistance_alert []")
    ;;
    ;;          ...
    ;;
    ;;      )
    ;;
    ;;================================================================

    (cond
        (   caseSensitive
            (defun foo ( pair )
                (wcmatch (cadr pair) pattern)
            )
        )
        (   (defun foo ( pair )
                (wcmatch (strcase (cadr pair)) pattern)
            )
            (setq pattern (strcase pattern))
        )
    )

    (setq i 0)

    (vl-remove-if-not 'foo
        (mapcar
           '(lambda (stream) (list (setq i (1+ i)) stream))
            (FileToList filename)
        )
    )
)

would be of use.

Nominally tested.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challenge) find strings in file
« Reply #6 on: March 29, 2005, 03:51:54 PM »
Here's a real simple, albeit limited version.
Code: [Select]

(defun FindWord (
                 ; ARGUMENTS
                 fn   ; valid file name
                 word ; word to find
                 /
                 ; LOCAL VARIABLES
                 fo line search found
                 )

  ;; Simple word finder.
  ;;
  ;; searches the given file(fn) for 'word' and returns
  ;; a list of the the line(s) that match.

  (setq fo (open fn "r"))

  (while (setq line (read-line fo))
         (if (vl-string-search word line)
           (setq found (cons line found))
           )
         )

  (close fo)
  found
  )
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
(Challenge) find strings in file
« Reply #7 on: March 29, 2005, 04:05:51 PM »
Nice and compact Mark. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(Challenge) find strings in file
« Reply #8 on: March 29, 2005, 04:37:39 PM »
Borrowing code from MP, this is my attempt.
Code: [Select]
(defun WCMatchFile (filename pattern caseSensitive / handle result idx)
  (setq idx 0)
  (and
    (eq 'str (type filename))
    (or (wcmatch filename "*\\*")
        (setq filename (findfile filename))
    )
    (setq handle (open filename "r"))
    (while (setq idx (1+ idx) stream (read-line handle))
      (if (or
            (and caseSensitive
                 (wcmatch stream pattern))
            (and (not caseSensitive)
                 (wcmatch (strcase stream) (strcase pattern)))
          )
        (setq result (cons (list idx stream) result))
      )
      T
    )
    (close handle)
  )
  (reverse result)
)


You can feed it this
Code: [Select]
(WCMatchFile (findfile "acad.mnl") "*defun*" nil)
or this:
Code: [Select]
(WCMatchFile "acad.mnl" "*defun*" 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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challenge) find strings in file
« Reply #9 on: March 30, 2005, 07:07:46 AM »
Two more variations.
Code: [Select]

(defun FindWordC (
                  ; ARGUMENTS
                  fn   ; valid file name
                  word ; word to find
                  case ; switch, T turns off case matching
                  /
                  ; LOCAL VARIABLES
                  fo line search found
                  )

  ;; Simple word finder.
  ;;
  ;; searches the given file(fn) for 'word' and returns
  ;; a list of the the line(s) that match.
  ;; If 'case' is T then preform case insensitivity search
  ;;
  ;; Example
  ;; (FindWordC <filename> "word" T)
  ;; will match 'word', 'WORD', or 'Word'

  (setq fo (open fn "r"))

  (while (setq line (read-line fo))
         (cond
           (case
             (if (vl-string-search word (strcase line T))
               (setq found (cons line found))
               )
             )
           (T
             (if (vl-string-search word line)
               (setq found (cons line found))
               )
             )
           )
         )

  (close fo)
  found
  )

(defun FindPat (
                ; ARGUMENTS
                fn       ; valid file name
                pattern  ; 'wcmatch' pattern
                /
                ; LOCAL VARIABLES
                fo line search found
                )

  ;; Pattern finder.
  ;;
  ;; searches the given file(fn) for 'wcmatch' pattern
  ;; and returns a list of the the line(s) that match.

  (setq fo (open fn "r"))

  (while (setq line (read-line fo))
         (if (wcmatch line pattern)
           (setq found (cons line found))
           )
         )

  (close fo)
  found
  )
TheSwamp.org  (serving the CAD community since 2003)