Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

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.

 

How to filter Console output using findstr command? (www.kunal-chowdhury.com)

 

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”:

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

 

 

Here is a screenshot of the above mentioned commands:

 

How to filter console output using the findstr command (www.kunal-chowdhury.com)

 

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:

 

How to filter (exclude) console output using the findstr command (www.kunal-chowdhury.com)

 

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.

 

 

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]] strings [[drive:][path]filename[ ...]]

 

  /B                        Matches pattern if at the beginning of a line.

  /E                         Matches pattern if at the end of a line.

  /L                         Uses search strings literally.

  /R                        Uses search strings as regular expressions.

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

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

  /X                        Prints lines that match exactly.

  /V                        Prints only lines that do not contain a match.

  /N                        Prints the line number before each line that matches.

  /M                       Prints only the filename if a file contains a match.

  /O                        Prints character offset before each matching line.

  /P                        Skip files with non-printable characters.

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

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

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

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

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

  /D:dir                  Search a semicolon delimited list of directories

  strings                Text 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

              \x                         Escape: literal use of meta character x

              \<xyz                   Word 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.

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.