find word in file...

nalsur8

Usenet Archive
Joined
Dec 29, 2003
Messages
35
Reaction score
0
in text file (*.txt) have so many line,text and character i want to find only one word inside that file, how to find the word
 
You can open and collect the lines first or check while reading each line.

(setq ofile (open <file> "r"))
(while (setq oline (read-line ofile))
(progn
(setq olist (append olist (list oline)))
)
)
(close ofile)

(wcmatch oline (strcat <wildcard> <word> <wildcard>))

etc.etc.etc.
 
a very simple way would be like:
(findText "C:\\textFile.txt" "FindMe")
or
(findText2 "C:\\textFile.txt" "FindMe")

(defun findText(filename textToFind / *error*)
(defun *error*(msg)
(if fh(progn
(close fh)
(setq fh nil)
)
)
)

(setq fh(open filename"r"))

(while(setq lin (read-line fh))
(if lin
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) ) (close fh) (setq fh nil)
)

or a little safer, read into list first then process list (defun findText2(filename textToFind / *error*) (defun *error*(msg) (if fh(progn (close fh) (setq fh nil) ) )
)

(setq fh(open filename"r"))
(while(setq lin (read-line fh))
(if lin (setq textlist(cons lin textlist)))
)
(close fh)
(setq fh nil)

(setq textlist(reverse textlist))
(foreach lin textlist
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) )
)

and a much more thorough example by Master Uhden is:

;; FindFiles.LSP (c) 01-21-03, John F. Uhden, Cadlantic/CADvantage ;; Program dedicated to the DOS-challenged Mark Propst. ;; It's purpose is to find files within a directory and/or subdirectories ;; given the user's choice of file specification and string to search ;; within the files found (case-insensitive). ;; It will write the results to a file of the user's choice in the format ;; File Name ;; <tab> Line containing search word ;; <tab> Line containing search word ;; etc. ;; Requires DOSLIB2K from McNeel.com (many thanks to Dale Fugier) ;; ;; Yes, this could be organized a whole lot better, and documentation is ;; sorely lacking, but that's what you get with off-the-cuff freeware. ;] ;; Fixed the localizing of fp (01-22-03) thanks to Mark's astuteness. (defun c:FindFiles ( / *error* @findfiles file path spec match subs i j fp) (defun *error* (errmsg) (if (= (type fp) 'FILE)(close fp)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*QUIT*")) (princ (strcat "\nERROR: " errmsg)) ) (princ) ) (defun @findfiles (path spec match subs / files spaces dirs @dirs @find) (defun @find (file / line lines) (and (setq fp (open file "r")) (while (setq line (read-line fp)) (and (not (wcmatch (vl-string-left-trim " " line) ";*")) (vl-string-search (strcase match)(strcase line)) (setq lines (cons line lines)) ) T ) (setq fp (close fp)) ) (if lines (cons file (reverse lines))) ) (cond ((or (/= (type path) 'STR)(= path "")) (setq path (strcase (vl-filename-directory (findfile (car (vl-directory-files "" "*.*" 1)) ) ) ) ) ) ((wcmatch path ",*/,*\\")) (1 (setq path (strcat path "\\"))) ) (if (/= (type spec) 'STR)(setq spec "*.*")) (if (/= (type match) 'STR)(setq match "")) (setq spaces " ") (defun @dirs (path / dirs found) (princ (strcat "\rSearching " path (substr spaces (strlen path)))) (princ) (and (setq found (vl-directory-files path spec 1)) (setq found (mapcar (function (lambda (x)(strcat path x))) found)) (or (= match "") (setq found (vl-remove nil (mapcar '@find found))) ) (setq files (append files found)) ) (and (= subs "Yes") (setq dirs (vl-directory-files path "*.*" -1)) (setq dirs (vl-remove-if (function (lambda (x)(vl-position x '("." "..")))) dirs) dirs (mapcar (function (lambda (x)(strcat path x "\\"))) dirs) ) ) (foreach dir dirs (@dirs dir)) ) (terpri) (@dirs path) (reverse files) ) (and (setq i 0 j 0) (or dos_getdir (arxload "doslib2k.arx" nil) (alert "DOSLIB2K.ARX is required\nYou can download from mcneel.com") ) (setq path (dos_getdir "Browse for Folder" "" "Select a Folder to Search")) (setq file (getfiled "Select Output File" path "" 149)) (setq spec (getstring "\nFile specification: ")) (or (vl-directory-files path spec 1) (alert "No matching files found.") ) (not (initget "Yes No")) (or (setq subs (getkword "\nSearch subdirectories, <Yes>/No: ")) (setq subs "Yes") ) (setq match (getstring T "\nLiteral string to find: ")) (or (setq files (@findfiles path spec match subs)) (alert "No files found containing string.") ) (or (setq fp (open file "w")) (alert (strcat "\nUnable to write to file\n" file)) ) (foreach item files (write-line (car item) fp) (setq i (1+ i)) (foreach item (cdr item) (write-line (strcat "\t" item) fp) (setq j (1+ j)) ) ) (not (setq fp (close fp))) (princ (strcat "\nFound " (itoa i) " files with " (itoa j) " matching lines.")) (princ (strcat "\nResults written to file " file)) ) (*error* nil)
)
 
thank to all

Mark Propst said:
a very simple way would be like:
(findText "C:\\textFile.txt" "FindMe")
or
(findText2 "C:\\textFile.txt" "FindMe")

(defun findText(filename textToFind / *error*)
(defun *error*(msg)
(if fh(progn
(close fh)
(setq fh nil)
)
)
)

(setq fh(open filename"r"))

(while(setq lin (read-line fh))
(if lin
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) ) (close fh) (setq fh nil)
)

or a little safer, read into list first then process list (defun findText2(filename textToFind / *error*) (defun *error*(msg) (if fh(progn (close fh) (setq fh nil) ) )
)

(setq fh(open filename"r"))
(while(setq lin (read-line fh))
(if lin (setq textlist(cons lin textlist)))
)
(close fh)
(setq fh nil)

(setq textlist(reverse textlist))
(foreach lin textlist
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) )
)

and a much more thorough example by Master Uhden is:

;; FindFiles.LSP (c) 01-21-03, John F. Uhden, Cadlantic/CADvantage ;; Program dedicated to the DOS-challenged Mark Propst. ;; It's purpose is to find files within a directory and/or subdirectories ;; given the user's choice of file specification and string to search ;; within the files found (case-insensitive). ;; It will write the results to a file of the user's choice in the format ;; File Name ;; <tab> Line containing search word ;; <tab> Line containing search word ;; etc. ;; Requires DOSLIB2K from McNeel.com (many thanks to Dale Fugier) ;; ;; Yes, this could be organized a whole lot better, and documentation is ;; sorely lacking, but that's what you get with off-the-cuff freeware. ;] ;; Fixed the localizing of fp (01-22-03) thanks to Mark's astuteness. (defun c:FindFiles ( / *error* @findfiles file path spec match subs i j fp) (defun *error* (errmsg) (if (= (type fp) 'FILE)(close fp)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*QUIT*")) (princ (strcat "\nERROR: " errmsg)) ) (princ) ) (defun @findfiles (path spec match subs / files spaces dirs @dirs @find) (defun @find (file / line lines) (and (setq fp (open file "r")) (while (setq line (read-line fp)) (and (not (wcmatch (vl-string-left-trim " " line) ";*")) (vl-string-search (strcase match)(strcase line)) (setq lines (cons line lines)) ) T ) (setq fp (close fp)) ) (if lines (cons file (reverse lines))) ) (cond ((or (/= (type path) 'STR)(= path "")) (setq path (strcase (vl-filename-directory (findfile (car (vl-directory-files "" "*.*" 1)) ) ) ) ) ) ((wcmatch path ",*/,*\\")) (1 (setq path (strcat path "\\"))) ) (if (/= (type spec) 'STR)(setq spec "*.*")) (if (/= (type match) 'STR)(setq match "")) (setq spaces " ") (defun @dirs (path / dirs found) (princ (strcat "\rSearching " path (substr spaces (strlen path)))) (princ) (and (setq found (vl-directory-files path spec 1)) (setq found (mapcar (function (lambda (x)(strcat path x))) found)) (or (= match "") (setq found (vl-remove nil (mapcar '@find found))) ) (setq files (append files found)) ) (and (= subs "Yes") (setq dirs (vl-directory-files path "*.*" -1)) (setq dirs (vl-remove-if (function (lambda (x)(vl-position x '("." "..")))) dirs) dirs (mapcar (function (lambda (x)(strcat path x "\\"))) dirs) ) ) (foreach dir dirs (@dirs dir)) ) (terpri) (@dirs path) (reverse files) ) (and (setq i 0 j 0) (or dos_getdir (arxload "doslib2k.arx" nil) (alert "DOSLIB2K.ARX is required\nYou can download from mcneel.com") ) (setq path (dos_getdir "Browse for Folder" "" "Select a Folder to Search")) (setq file (getfiled "Select Output File" path "" 149)) (setq spec (getstring "\nFile specification: ")) (or (vl-directory-files path spec 1) (alert "No matching files found.") ) (not (initget "Yes No")) (or (setq subs (getkword "\nSearch subdirectories, <Yes>/No: ")) (setq subs "Yes") ) (setq match (getstring T "\nLiteral string to find: ")) (or (setq files (@findfiles path spec match subs)) (alert "No files found containing string.") ) (or (setq fp (open file "w")) (alert (strcat "\nUnable to write to file\n" file)) ) (foreach item files (write-line (car item) fp) (setq i (1+ i)) (foreach item (cdr item) (write-line (strcat "\t" item) fp) (setq j (1+ j)) ) ) (not (setq fp (close fp))) (princ (strcat "\nFound " (itoa i) " files with " (itoa j) " matching lines.")) (princ (strcat "\nResults written to file " file)) ) (*error* nil)
)

nalsur8 said:
in text file (*.txt) have so many line,text and character i want to find only one word inside that file, how to find the word
 
ok i found the text and i need to modify the text
and write-line to file back

Mark Propst said:
a very simple way would be like:
(findText "C:\\textFile.txt" "FindMe")
or
(findText2 "C:\\textFile.txt" "FindMe")

(defun findText(filename textToFind / *error*)
(defun *error*(msg)
(if fh(progn
(close fh)
(setq fh nil)
)
)
)

(setq fh(open filename"r"))

(while(setq lin (read-line fh))
(if lin
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) ) (close fh) (setq fh nil)
)

or a little safer, read into list first then process list (defun findText2(filename textToFind / *error*) (defun *error*(msg) (if fh(progn (close fh) (setq fh nil) ) )
)

(setq fh(open filename"r"))
(while(setq lin (read-line fh))
(if lin (setq textlist(cons lin textlist)))
)
(close fh)
(setq fh nil)

(setq textlist(reverse textlist))
(foreach lin textlist
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) )
)

and a much more thorough example by Master Uhden is:

;; FindFiles.LSP (c) 01-21-03, John F. Uhden, Cadlantic/CADvantage ;; Program dedicated to the DOS-challenged Mark Propst. ;; It's purpose is to find files within a directory and/or subdirectories ;; given the user's choice of file specification and string to search ;; within the files found (case-insensitive). ;; It will write the results to a file of the user's choice in the format ;; File Name ;; <tab> Line containing search word ;; <tab> Line containing search word ;; etc. ;; Requires DOSLIB2K from McNeel.com (many thanks to Dale Fugier) ;; ;; Yes, this could be organized a whole lot better, and documentation is ;; sorely lacking, but that's what you get with off-the-cuff freeware. ;] ;; Fixed the localizing of fp (01-22-03) thanks to Mark's astuteness. (defun c:FindFiles ( / *error* @findfiles file path spec match subs i j fp) (defun *error* (errmsg) (if (= (type fp) 'FILE)(close fp)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*QUIT*")) (princ (strcat "\nERROR: " errmsg)) ) (princ) ) (defun @findfiles (path spec match subs / files spaces dirs @dirs @find) (defun @find (file / line lines) (and (setq fp (open file "r")) (while (setq line (read-line fp)) (and (not (wcmatch (vl-string-left-trim " " line) ";*")) (vl-string-search (strcase match)(strcase line)) (setq lines (cons line lines)) ) T ) (setq fp (close fp)) ) (if lines (cons file (reverse lines))) ) (cond ((or (/= (type path) 'STR)(= path "")) (setq path (strcase (vl-filename-directory (findfile (car (vl-directory-files "" "*.*" 1)) ) ) ) ) ) ((wcmatch path ",*/,*\\")) (1 (setq path (strcat path "\\"))) ) (if (/= (type spec) 'STR)(setq spec "*.*")) (if (/= (type match) 'STR)(setq match "")) (setq spaces " ") (defun @dirs (path / dirs found) (princ (strcat "\rSearching " path (substr spaces (strlen path)))) (princ) (and (setq found (vl-directory-files path spec 1)) (setq found (mapcar (function (lambda (x)(strcat path x))) found)) (or (= match "") (setq found (vl-remove nil (mapcar '@find found))) ) (setq files (append files found)) ) (and (= subs "Yes") (setq dirs (vl-directory-files path "*.*" -1)) (setq dirs (vl-remove-if (function (lambda (x)(vl-position x '("." "..")))) dirs) dirs (mapcar (function (lambda (x)(strcat path x "\\"))) dirs) ) ) (foreach dir dirs (@dirs dir)) ) (terpri) (@dirs path) (reverse files) ) (and (setq i 0 j 0) (or dos_getdir (arxload "doslib2k.arx" nil) (alert "DOSLIB2K.ARX is required\nYou can download from mcneel.com") ) (setq path (dos_getdir "Browse for Folder" "" "Select a Folder to Search")) (setq file (getfiled "Select Output File" path "" 149)) (setq spec (getstring "\nFile specification: ")) (or (vl-directory-files path spec 1) (alert "No matching files found.") ) (not (initget "Yes No")) (or (setq subs (getkword "\nSearch subdirectories, <Yes>/No: ")) (setq subs "Yes") ) (setq match (getstring T "\nLiteral string to find: ")) (or (setq files (@findfiles path spec match subs)) (alert "No files found containing string.") ) (or (setq fp (open file "w")) (alert (strcat "\nUnable to write to file\n" file)) ) (foreach item files (write-line (car item) fp) (setq i (1+ i)) (foreach item (cdr item) (write-line (strcat "\t" item) fp) (setq j (1+ j)) ) ) (not (setq fp (close fp))) (princ (strcat "\nFound " (itoa i) " files with " (itoa j) " matching lines.")) (princ (strcat "\nResults written to file " file)) ) (*error* nil)
)

nalsur8 said:
in text file (*.txt) have so many line,text and character i want to find only one word inside that file, how to find the word
 
(setq fh(open filename"r"))(setq writefilehandle(open fname "w"));*******caution this will overwrite existing file******** ;*****make sure you want to do this*********** ;either that or make a temp file and rename afterwards

(foreach existingline textlist(setq newline "whatever you want new line to be") (write-line writefile newline) ) (write-line writefile existingline)
)

nalsur8 said:
ok i found the text and i need to modify the text
and write-line to file back

Mark Propst said:
a very simple way would be like:
(findText "C:\\textFile.txt" "FindMe")
or
(findText2 "C:\\textFile.txt" "FindMe")

(defun findText(filename textToFind / *error*)
(defun *error*(msg)
(if fh(progn
(close fh)
(setq fh nil)
)
)
)

(setq fh(open filename"r"))

(while(setq lin (read-line fh))
(if lin
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) ) (close fh) (setq fh nil)
)

or a little safer, read into list first then process list (defun findText2(filename textToFind / *error*) (defun *error*(msg) (if fh(progn (close fh) (setq fh nil) ) )
)

(setq fh(open filename"r"))
(while(setq lin (read-line fh))
(if lin (setq textlist(cons lin textlist)))
)
(close fh)
(setq fh nil)

(setq textlist(reverse textlist))
(foreach lin textlist
(if (wcmatch (strcase lin)(strcase (strcat "*" textToFind "*"))) (progn (print"Here's your text") (print lin) ) ) ) )
)

and a much more thorough example by Master Uhden is:

;; FindFiles.LSP (c) 01-21-03, John F. Uhden, Cadlantic/CADvantage ;; Program dedicated to the DOS-challenged Mark Propst. ;; It's purpose is to find files within a directory and/or subdirectories ;; given the user's choice of file specification and string to search ;; within the files found (case-insensitive). ;; It will write the results to a file of the user's choice in the format ;; File Name ;; <tab> Line containing search word ;; <tab> Line containing search word ;; etc. ;; Requires DOSLIB2K from McNeel.com (many thanks to Dale Fugier) ;; ;; Yes, this could be organized a whole lot better, and documentation is ;; sorely lacking, but that's what you get with off-the-cuff freeware. ;] ;; Fixed the localizing of fp (01-22-03) thanks to Mark's astuteness. (defun c:FindFiles ( / *error* @findfiles file path spec match subs i j fp) (defun *error* (errmsg) (if (= (type fp) 'FILE)(close fp)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*QUIT*")) (princ (strcat "\nERROR: " errmsg)) ) (princ) ) (defun @findfiles (path spec match subs / files spaces dirs @dirs @find) (defun @find (file / line lines) (and (setq fp (open file "r")) (while (setq line (read-line fp)) (and (not (wcmatch (vl-string-left-trim " " line) ";*")) (vl-string-search (strcase match)(strcase line)) (setq lines (cons line lines)) ) T ) (setq fp (close fp)) ) (if lines (cons file (reverse lines))) ) (cond ((or (/= (type path) 'STR)(= path "")) (setq path (strcase (vl-filename-directory (findfile (car (vl-directory-files "" "*.*" 1)) ) ) ) ) ) ((wcmatch path ",*/,*\\")) (1 (setq path (strcat path "\\"))) ) (if (/= (type spec) 'STR)(setq spec "*.*")) (if (/= (type match) 'STR)(setq match "")) (setq spaces " ") (defun @dirs (path / dirs found) (princ (strcat "\rSearching " path (substr spaces (strlen path)))) (princ) (and (setq found (vl-directory-files path spec 1)) (setq found (mapcar (function (lambda (x)(strcat path x))) found)) (or (= match "") (setq found (vl-remove nil (mapcar '@find found))) ) (setq files (append files found)) ) (and (= subs "Yes") (setq dirs (vl-directory-files path "*.*" -1)) (setq dirs (vl-remove-if (function (lambda (x)(vl-position x '("." "..")))) dirs) dirs (mapcar (function (lambda (x)(strcat path x "\\"))) dirs) ) ) (foreach dir dirs (@dirs dir)) ) (terpri) (@dirs path) (reverse files) ) (and (setq i 0 j 0) (or dos_getdir (arxload "doslib2k.arx" nil) (alert "DOSLIB2K.ARX is required\nYou can download from mcneel.com") ) (setq path (dos_getdir "Browse for Folder" "" "Select a Folder to Search")) (setq file (getfiled "Select Output File" path "" 149)) (setq spec (getstring "\nFile specification: ")) (or (vl-directory-files path spec 1) (alert "No matching files found.") ) (not (initget "Yes No")) (or (setq subs (getkword "\nSearch subdirectories, <Yes>/No: ")) (setq subs "Yes") ) (setq match (getstring T "\nLiteral string to find: ")) (or (setq files (@findfiles path spec match subs)) (alert "No files found containing string.") ) (or (setq fp (open file "w")) (alert (strcat "\nUnable to write to file\n" file)) ) (foreach item files (write-line (car item) fp) (setq i (1+ i)) (foreach item (cdr item) (write-line (strcat "\t" item) fp) (setq j (1+ j)) ) ) (not (setq fp (close fp))) (princ (strcat "\nFound " (itoa i) " files with " (itoa j) " matching lines.")) (princ (strcat "\nResults written to file " file)) ) (*error* nil)
)

nalsur8 said:
in text file (*.txt) have so many line,text and character i want to find only one word inside that file, how to find the word
 

Latest threads

Back
Top