Author Topic: Summarize multiple txt files  (Read 2310 times)

0 Members and 1 Guest are viewing this topic.

Ron Heigh

  • Guest
Summarize multiple txt files
« on: January 17, 2005, 01:04:45 PM »
Does anybody know of a way to open 100's of txt files and append the first line of each to a new file with the original filename?
I'm searching for a specific first line but don't have all day to keep opening them.

M-dub

  • Guest
Summarize multiple txt files
« Reply #1 on: January 17, 2005, 01:09:46 PM »
If you're just LOOKING for a file, could you not do a search for "files containing text"?  It might take a little while to search for a phrase, but it should work...:?

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Summarize multiple txt files
« Reply #2 on: January 17, 2005, 01:45:28 PM »
You trying to do this using lisp?
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Summarize multiple txt files
« Reply #3 on: January 17, 2005, 01:55:30 PM »
If you have grep installed you can use this, grep -m 1 -e '^.*$' *.ext > output file that will look something like this.
 
Code: [Select]

[ned(~/src/python/work)] grep -m 1 -e '^.*$' *.py
ab.py:import string, os, sys, re
add_book.py:import string, os, sys, re
get_file_by_ext.py:import os
gfs.py:import os, re
gfx.py:import os, re
mkfilelst.py:# Creates a list of file names from a given path.
pwd2html.py:import os, sys, time
te.py:import sys, os
xren.py:## A simple program that renames files by changing their case.


You might be able to use find to do this too.
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Summarize multiple txt files
« Reply #4 on: January 17, 2005, 02:22:19 PM »
You can alter this routine to do that.

Code: [Select]
;Finds all lisp files, sorts and writes first two lines to screen.
 ;Type SCANLSP2.
 ;******************************************************************************
 ;  "SCANLSP2"                       Public Domain,   by Ron W. Lloyd    11/7/89
 ;  This is version 2.0 of SCANLISP, a lisp files manager.
 ;  This routine writes the names of all .LSP files in a given directory to
 ;  the screen with two lines of comments for each file. The comments are the
 ;  first two lines of the file, assuming that most files are briefly
 ;  commented there. If not, you can add your own as above.  The files are
 ;  alphabetized and you are given the opportunity to load, delete, or read
 ;  any file or exit the routine.
 ;
 ;  Note: Routine redefines *error*. You may wish to reset it later by typing
 ;  (setq *error* #err) at your command prompt, if you abort this routine.
 ;******************************************************************************
 ;redefine the error message to exit cleanly and tidy up files.
(defun *error* (msg)
  (prompt "\e[m")
  (command "sh"
           "erase $ooz.$$l"
           "sh"
           "erase $gnk.$$l"
  )
  (close hunt)
  (close sfile)
  (close file)
  (close lfile)
  (setq msg nil)
  (princ)
)
 ;*****************************************************************************
 ;Convert backslashes for path handling.
(defun bs (path / inc wpath char)
  (setq inc 1
        wpath ""
  )
  (while (/= "" (setq char (substr path inc 1)))
    (setq wpath (strcat wpath
                        (if (= "/" char)
                          "\\"
                          char
                        )
                )
    )
    (setq inc (1+ inc))
  )
  (if (and (/= wpath "") (/= (substr wpath (strlen wpath) 1) "\\"))
    (setq wpath (strcat wpath "\\"))
  )
  wpath
)
 ;*****************************************************************************
 ;Load a lisp file.
(defun ld (/ lname)
  (setq ftest nil)
  (while (not ftest)
    (setq lname (getstring "\nName of file to load: (.LSP assumed) "))
    (setq lname (strcat pth lname ".LSP"))
    (setq ftest (findfile lname))
    (if (= ftest nil)
      (prompt (strcat (strcase lname) " not found."))
    )
  )
  (prompt (strcat "Loading " (strcase lname) " .  .  .  .  .  ."))
  (load lname)
)
 ;******************************************************************************
 ;Delete a lisp file. (Actually, renames to .BAK)
(defun dlt (/ dname dlsp blsp fatal)
  (setq dname (getstring "\nName of file to delete: (.LSP assumed) ")
        dlsp  (strcase (strcat pth dname ".LSP"))
        hunt  (open dlsp "r")
        blsp  (strcase (strcat pth dname ".BAK"))
  )
  (if hunt
    (progn
      (initget "Y N")
      (setq
        fatal (getkword
                (strcat "\nAbout to erase " dlsp ". Are you sure? Yes/<No>: ")
              )
      )
      (if (= fatal "Y")
        (progn
          (setq renm (strcat "rename " dlsp " " dname ".bak"))
          (command "sh" renm)
          (prompt (strcat dlsp
                          "has been re-named "
                          blsp
                          ". It can be erased from DOS."
                  )
          )
          (prin1)
        )
      )
      (close hunt)
    )
    (prompt (strcat dlsp " not found."))
  )
  (initget "Y N")
  (setq ans3 (getkword "\nContinue? <Yes>/No: "))
  (if (or (null ans3) (= ans3 "Y"))
    (setq go t)
    (setq go nil)
  )
)
 ;******************************************************************************
 ;Read a lisp file one screen at a time.
(defun seefile (/ fname sfile more i line)
  (setq sfile nil)
  (while (not sfile)
    (setq fname (getstring "\nName of file to see: (.LSP assumed)  "))
    (setq sfile (open (strcase (strcat pth fname ".LSP")) "r"))
    (if (not sfile)
      (prompt (strcat (strcase pth) (strcase fname) ".LSP not found."))
      (progn
        (setq more t)
        (while more
          (setq i 23)
          (while (> i 0)
            (setq line (read-line sfile))
            (if line
              (progn
                (prompt line)
                (terpri)
              )
              (progn
                (setq i 1)
                (setq more nil)
                (prompt "\e[1m")
                (prompt "\n****End of file****")
                (prompt "\e[m")
              )
            )
            (setq i (- i 1))
          )
          (if more
            (progn
              (initget "M X")
              (prompt "\e[1m")
              (setq more (getkword "More/<eXit>: "))
              (prompt "\e[m")
              (if (= more "X")
                (setq more nil)
              )
            )
          )
        )
        (close sfile)
      )
    )
  )

)
 ;******************************************************************************
 ;Main lisp file scan program.
(defun c:scanlsp2
       (/ pth dr1 file str cnt i chr2 name flnam lfile lst go ans ans2)
  (setvar "cmdecho" 0)
  (setq pth (getstring "\nEnter PATH to scan: <Current> "))
  (setq pth (bs pth))
  (if (null pth)
    (setq dr1 "dir *.lsp > $ooz.$$l")
    (setq dr1 (strcat "dir " pth "*.lsp > $ooz.$$l"))
  )
  (command "sh" dr1)
  (command "sh" "sort < $ooz.$$l > $gnk.$$l")
  (setq file (open "$gnk.$$l" "r"))
  (repeat 2
    (read-line file)
  )
  (setq str (read-line file))
  (setq cnt (atoi (substr str 7 6)))
  (if (/= cnt 0)
    (progn
      (textscr)
      (repeat 2
        (read-line file)
      )
      (setq go t)
      (while go
        (setq i 1)
        (while (and (< i 8) (> cnt 0))
          (setq chr2 t)
          (setq name (chr (read-char file)))
          (while (/= chr2 " ")
            (setq chr2 (chr (read-char file)))
            (if (/= chr2 " ")
              (setq name (strcat name chr2))
            )
          )
          (setq flnam (strcat name ".LSP"))
          (prompt "\e[1m")
          (prompt flnam)
          (print)
          (prompt "\e[m")
          (setq lfile (open (strcat pth flnam) "r"))
          (setq lst (list (read-line lfile) (read-line lfile)))
          (foreach s lst (princ s) (terpri))
          (close lfile)
          (setq cnt (- cnt 1))
          (setq go (read-line file))
          (setq i (+ i 1))
        ) ;while index less than 9
        (if (> cnt 0)
          (progn
            (setq go nil)
            (initget "R M L X D")
            (prompt "\e[1m")
            (setq ans
                   (getkword
                     "                       Read file/Load/Delete/eXit/<More>: "
                   )
            )
            (prompt "\e[m")
            (if (null ans)
              (setq go t)
            )
            (if (= ans "R")
              (progn
                (seefile)
                (initget "C X")
                (prompt "\e[1m")
                (setq ans4
                       (getkword "\n                      eXit/<Continue scan>: "
                       )
                )
                (prompt "\e[m")
                (if (null ans4)
                  (setq ans4 "C")
                )
                (if (= ans4 "C")
                  (setq go t)
                )
              )
            )
            (if (= ans "M")
              (setq go t)
            )
            (if (= ans "L")
              (ld)
            )
            (if (= ans "D")
              (dlt)
            )
          ) ;progn
          (progn
            (setq go nil)
            (initget "R L X D")
            (prompt "\e[1m")
            (setq ans2
                   (getkword
                     "\n(End of LISP directory.)     Read file/Load/Delete/<eXit>: "
                   )
            )
            (prompt "\e[m")
            (if (= ans2 "L")
              (ld)
            )
            (if (= ans2 "D")
              (dlt)
            )
            (if (= ans2 "R")
              (seefile)
            )
          )
        )
      ) ;while go
    ) ;progn
    (prompt (strcat "\nNo LISP files found in " (strcase pth)))
  ) ;if count not 0
  (command "sh"
           "erase $ooz.$$l"
           "sh"
           "erase $gnk.$$l"
  )
  (close file)
  (setq *error* #err)
  (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.

Ron Heigh

  • Guest
Summarize multiple txt files
« Reply #5 on: January 17, 2005, 06:25:40 PM »
Thankyou very much for your replies.
The FIND option of windows doesn't seem to find the file I need.
Plus, it will be nice to have an index of hte files.
I prefer to use lisp to do this as I don't really know grep.