February 07, 2013

Useful Windows tool of the day

I needed to grep - Linux style - for a string piped out of windows command line output, so, I started looking for something native to windows that would achieve that for me.

I can't remember if I've just forgotten about this command or if I just didn't know that the Findstr command that's built into windows existed. Anyway, it did what I needed.

Here is the FindStr command link on TechNet with all its switches and the meta-characters it accepts for regular expressions.
http://technet.microsoft.com/en-us/library/bb490907.aspx

So now I can use the findstr command to receive the output from another command and grab the string I'm looking for.

Example use: set | findstr JAVA
This grabs the string JAVA out of the windows environment variables if it's set. In my case, that output would be: JAVA_HOME=D:\Java\jdk1.6.0_39

--------------

Findstr

Searches for patterns of text in files using regular expressions.

Syntax:
findstr [/b] [/e] [/l] [/r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/p] [/offline] [/g:file] [/f:file] [/c:string] [/d:dirlist] [/a:ColorAttribute] [strings] [[Drive:][Path] FileName [...]]

Parameters
/b   : Matches the pattern if at the beginning of a line.
/e   : Matches the pattern if at the end of a line.
/l   : Uses search strings literally.
/r   : Uses search strings as regular expressions. Findstr interprets all metacharacters as regular expressions unless you use /l.
/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 file name if a file contains a match.
/o   : Prints seek offset before each matching line.
/p   : Skips files with non-printable characters.
/offline   : Processes files with offline attribute set.
/f: file   : Reads file list from the specified file.
/c: string   : Uses specified text as a literal search string.
/g: file   : Gets search strings from the specified file.
/d: dirlist   : Searches a comma-delimited list of directories.
/a: ColorAttribute   : Specifies color attributes with two hexadecimal digits.
strings   : Specified text to be searched for in FileName.
[ Drive : ][ Path ] FileName [...] : Specifies a file or files to search.
/?   : Displays help at the command prompt.

Using regular expressions with findstr 

Findstr is capable of finding the exact text you are looking for in any ASCII file or files. However, sometimes you have only part of the information that you want to match, or you want to find a wider range of information. In such cases, findstr has the powerful capability to search for patterns of text using regular expressions.
Regular expressions are a notation for specifying patterns of text, as opposed to exact strings of characters. The notation uses literal characters and metacharacters. Every character that does not have special meaning in the regular expression syntax is a literal character and matches an occurrence of that character. For example, letters and numbers are literal characters. A metacharacter is a symbol with special meaning (an operator or delimiter) in the regular-expression syntax.

The following table lists the metacharacters that findstr accepts.

Character
Value
.
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 metacharacter x
\<xyz
Word position: beginning of word
xyz\>
Word position: end of word
The special characters in regular expression syntax are most powerful when you use them together. For example, the following combination of the wildcard character (.) and repeat (*) character match any string of characters:
.*
Use the following expression as part of a larger expression that matches any string beginning with "b" and ending with "ing":
b.*ing

Share:

0 comments:

Post a Comment