Sometime we need to filter out a console command output and display only the portion that we want to show to the user. This could be based on any specific term or string that we want to include or exclude based on our requirement.

Today, here in this post, we are going to discuss on this and learn how to extract/filter the said information from the console command output.

There is a command named “findstr” which you can use by combining with the ‘|’ (pipe) symbol to extract or filter only the portion of the console command output, based on the included terms and excluded terms that you pass as an argument.

The command file is an executable named “findstr.exe” and can be located at system directory (generally, at “C:\Windows\System32” folder).

When you want to find and display only the records that includes a specific term or terms, you have to pass the following command switch parameters: “/r /c”. To pass a single term, use /c:”TERM”. To pass multiple terms, use /c:”TERM1” /c:”TERM2” … /c:”TERMn” … and so on. The parameter “/r” uses search strings/terms as regular expressions, whereas “/c” followed by the string/term uses it as a literal search string.

For example, the following command filters out only the command output of “dir” that has a term “Music”:

dir | C:\Windows\System32\findstr.exe /r /c:”Music”

The below command filters out only the command output of “dir” that has either one of the following terms “Music”, “Documents” or “Down”:

If you want to exclude a line that has a specific string/term, you can use the switch “/v” followed by the words under “/c” as discussed above. To exclude a single term, use /c:”TERM”. To exclude multiple terms, use /c:”TERM1” /c:”TERM2” … /c:”TERMn” … and so on.

For example, the below command filters out the command output of “dir” and prints only those lines which doesn’t have the term “Music” or “Documents”:

dir | C:\Windows\System32\findstr.exe /r /v /c:”Music” /c:”Documents”

Here you can find a screenshot of the same command and it’s associated output:

I hope that the point is clear to you now and you would be able to filter out a specific console command output based on a term to include or exclude in or from the result. You can also use the “findstr” command and the following switches to search inside a file from the command prompt.

/SSearches for matching files in the current directory and all subdirectories.

/ISpecifies that the search is not to be case-sensitive.

/XPrints lines that match exactly.

/VPrints only lines that do not contain a match.

/NPrints the line number before each line that matches.

/MPrints only the filename if a file contains a match.

/OPrints character offset before each matching line.

/PSkip files with non-printable characters.

/OFF[LINE]Do not skip files with offline attribute set.

/A:attrSpecifies color attribute with two hex digits. See "color /?"

/F:fileReads file list from the specified file(/ stands for console).

/C:stringUses specified string as a literal search string.

/G:fileGets search strings from the specified file(/ stands for console).

/D:dirSearch a semicolon delimited list of directories

stringsText to be searched for.

Regular expression quick reference:

.Wildcard: any character

*Repeat: zero or more occurrences 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

\xEscape: literal use of meta character x

\<xyzWord position: beginning of word

xyz\>Word position: end of word

Stay tuned for more articles on this topic and learn the power of console commands. See you then. Have a nice day ahead. If you liked the post, don’t forget to check out my others articles available in this blog.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

About Me

Kunal Chowdhury is a Windows Platform Development MVP of Microsoft, a Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.