What is FINDSTR and how to use FINDSTR
FINDSTR is a command used to find specific text in files on Windows. Find String when put in short became FINDSTR. It is also used to find files with specific text. There are various FINDSTR commands to do various functions. It was first released with Windows 2000 Resource Kit with the name qgrep. It is a built-in tool in Windows and its file is available in .exe format. FINDSTR does not search null bytes like spaces in the Unicode format. There are some basic rules to use findstr command on Command Prompt or other Command Line Interpreters, which are developed by Microsoft. They are:
Each FINDSTR command must contain a string followed by a file name.You can use literal characters and meta-characters in FINDSTR commands. Literal characters do not have any special meaning in the syntax. Letters and numbers are called literal characters. Meta-characters are symbols that have specific meanings for each one. The following are the accepted meta-characters in the syntax and their meanings.
You should create a text file with search criteria on a separate line when you want to search for multiple strings.Use spaces between strings to search for multiple strings in a single command except for the syntax with /c.
Parameters and their meaning in findstr syntax
Usage of findstr commands
1] To search for the word microsoft or windows in file x.y, you should use: findstr microsoft windows x.y 2] To search for the word microsoft windows in file x.y, you should use: findstr /c:“microsoft windows” x.y In the above command /c is used to search for the specified text “microsoft windows’ in the file. 3] If you want to find the occurrences of the word Microsoft with capital M in the text file twc.txt, you should use: findstr Microsoft twc.txt 4] If you want to search for a specific word Microsoft in a directory and its sub-directories without giving importance to type cases, use: findstr /s /i Microsoft . In the above syntax, /s is used to search the current directory and its sub-directories. /i is used to ignore type case (capital M) in the string Microsoft. 5] If you want to find lines that start with To with multiple spaces before the word and want to display the line number where the strings occurred, use: findstr /b /n /r /c:^ *To .bas 6] If you want to search for multiple strings in multiple files with search criteria in stringlist.txt and files list in filelist.txt and you want to see the results stored in the file result.out, use: findstr /g:stringlist.txt /f:filelist.txt > results.out 7] If you want to find the files that contain the word thewindowsclub in a particular directory and its sub-directories, use: findstr /s /i /m <thewindowsclub> . 8] If you want to find files that contain the thewindowsclub and other words that begin with the like thesis, thermometer, etc, use: findstr /s /i /m <the. . These are the ways you can use findstr commands in Command-Line interpreters like Command Prompt, etc. You have to understand every parameter and its function as well as the meta-characters and their meaning to write syntax and use findstr command regularly.
What is Select-String and its parameters
Imagine you are writing chunks of code in PowerShell and you lost track of certain strings and text in that PowerShell file. You need to find that in many thousand lines of code within thousands of strings and words. There comes the Select-String command which lets you search for strings and text in those PowerShell input files. It is similar to grep on Linux. Select-String is a cmdlet that is used to search text and the patterns in input strings and files. It is similar to grep on Linux and FINDSTR on Windows. When used Select-String to search for some text, it finds the first match in each line and displays file name, line number, and the entire line where the match occurred. It can be used to find multiple matches per line or to display text before or after the match, or get results in Boolean expressions like True or False. You can also use it to display all the text except for the match of the expression you use in the command. The WildCards you use in FINDSTR can be used in Select-String too. Also, Select-String works with different file encodings like ASCII, Unicode, etc. It uses Byte-Order-Mark (BOM) to determine the file encoding. If the BOM is missing in the file, Select-String will assume the file as UTF8.
Parameters of Select-String
Microsoft envisioned and developed the below parameters which shall be used in syntax. -AllMatches It is used to search all the matches in a line as opposed to the first match in the line Select-Sting normally does. -CaseSensitive It represents that the match is case-sensitive. By default, Select-String is not case-sensitive. -Context It is used to capture the specified number of lines you enter before and after the line of the match. If you enter 1, it captures one line before and after the match. -Culture There are certain cultures like ordinal, invariant, etc in coding. This parameter is used to specify the culture in the syntax. -Encoding It is used to specify the encoding format of the text in files like ASCII, UTF8, UTF7, Unicode, etc. -Exclude This parameter is used to exclude certain text in the file. -Include This parameter is used to include certain text in the file. -InputObject It is used to specify the text to be searched. -List It is used to retrieve the list of files that match the text. -LiteralPath It is used to specify the path for the search. -NoEmphasis Generally, Select-String highlights the match in the file. This parameter is used to avoid highlighting. -NotMatch It is used to find the text that doesn’t match the specified pattern. -Path It is used to specify the path to be searched along with the use of wildcards. -Pattern The parameter is used to find the match in each line as a pattern. -Quiet This parameter is used to get the output in Boolean values like True or False. -Raw It is used to see only matching objects rather than Match info. -SimpleMatch The parameter is used to specify a simple match rather than a regular expression match.
Difference between FINDSTR and Select-String
FINDSTR is a pre-PowerShell era executable file that is used to search text and strings in files. Select-String is a PowerShell cmdlet that is used to search for text and patterns in files. When compared to FINDSTR, Select-String is the more powerful and complex cmdlet that displays a lot of info about the match.
What is the PowerShell equivalent of grep?
Select-String is the PowerShell equivalent of grep available on Windows. It functions the same way grep does and it gives detailed info regarding the match according to the parameters we use in the syntax.
Does FINDSTR work on Word files?
Yes, FINDSTR works on Word files. But it cannot show find the matches even if the strings you entered are there in the file. It can give you results in binary files in the .doc formats but not in the .docx formats for some unknown technical reasons. Related Read: How to check the PowerShell version in Windows.