2- FINDSTR
My best command in DOS is FINDSTR. It is very useful for searching for specific text pattern in files. It’s functionality is similar to the grep command on Linux OS.
Syntax
FINDSTR string(s) [pathname(s)] [/R] [/C:"string"] [/G:StringsFile] [/F:file] [/D:DirList] [/A:color] [/OFF[LINE]] [options]
Key
string Text to search for.
pathname(s) The file(s) to search.
/C:string Use string as a literal search string (may include spaces).
/R Use string as a regular expression.
/G:StringsFile Get search string from a file (/ stands for console).
/F:file Get a list of pathname(s) from a file (/ stands for console).
/d:dirlist Search a comma-delimited list of directories.
/A:color Display filenames in colour (2 hex digits)
options can be any combination of the following switches:
/I Case-insensitive search.
/S Search subfolders.
/P Skip any file that contains non-printable characters
/OFF[LINE] Do not skip files with the OffLine attribute set.
/L Use search string(s) literally.
/B Match pattern if at the Beginning of a line.
/E Match pattern if at the END of a line.
/X Print lines that match exactly.
/V Print only lines that do NOT contain a match.
/N Print the line number before each line that matches.
/M Print only the filename if a file contains a match.
/O Print character offset before each matching line.
Examples:
Search for "Cannon" OR "Printer" in the files.
FINDSTR "Cannon Printer " Press.txt Paper.txt
FINDSTR /C:"Cannon Printer" Contacts.txt
Search every file in the current folder and all subfolders for the word "Printer", regardless of upper/lower case, note that /S will only search below the current directory:
FINDSTR /s /i Printer *.*
Join two files, return only the lines that they both have in common:
FINDSTR /g:"file1.txt" "file2.txt"
Search for pattern with multiple words
findstr /C:"John vincint hogan..." filename
/C indicates that the search pattern has to be matched literally.
Search all the text files in the current folder for the string "Cannon", display the filenames in Blue on Red
FINDSTR /A:2F /C:Cannon *.txt
Read the file C:\source.txt, remove all the blank lines and write to D:\Target.txt
FINDSTR /v "^$" Z:\source.txt >Z:\Target.txt
Find any positive integers in the file datafile.txt and include any lines that are a zero (0):
FINDSTR /r "^[1-9][0-9]*$ ^0$" Sales.txt
if you need find both words "john" and "bob"
/i /r /c:"john.*bob" /c:"bob.*john"
-i -r -c:"john.*bob" /c:"bob.*john"
/irc:"john.*bob" /c:"bob.*john"
To find every line in datafile.txt containing the word Printer, preceeded by any number of spaces, and to prefix each line found with a consecutive number:
FINDSTR /b /n /c:" *Printer" datafile.txt
Finding a string only if surrounded by the standard delimiters
Find the word "Glass", but not the words "PaperGlass" or "MirrorGlass":
FINDSTR "\<Glass\>" C:\data\datafile.txt
Find any words that begin with the letters 'emp', such as 'employer' or 'employee'
FINDSTR "\<emp.*" C:\work\inventory.txt
You can use regular expressions with findstr /R switch. Typical command would be as below.
findstr /R pattern filename.txt
Search for the occurrence of all words ending with ‘xyz’ in a file.
findstr /R [a-z]*xyz filename.txt
Regular Expressions
FINDSTR can use the following keywords which have special meaning either as an operator or delimiter. FINDSTR support for regular expressions is limited and non-standard, only the following keywords are supported:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of
xyz\> Word position: end of word
No comments:
Post a Comment