Sanket
Sanket

Reputation: 746

Equivalent of `grep -o` for findstr command in Windows

In Unix, the command grep -o prints out only the matched string. This is very helpful when you are searching for a pattern using regular expression and only interested in what matched exactly and not the entire line.

However, I'm not able to find something similar for the Windows command findstr. Is there any substitute for printing only matched string in windows?

For example:

grep -o "10\.[0-9]+\.[0-9]+\.[0-9]+" myfile.txt

The above command prints only the IP address in myfile.txt of the form 10.*.*.* but not the entire lines which contain such IP adresses.

Upvotes: 1

Views: 1644

Answers (2)

thucnguyen
thucnguyen

Reputation: 1746

Just use your familiar grep and other great Linux commands by downloading this UnxUtils (ready .exe binaries). Add it to your PATH environment variable for convenience

Upvotes: 1

Bill_Stewart
Bill_Stewart

Reputation: 24565

PowerShell:

select-string '10\.[0-9]+\.[0-9]+\.[0-9]+' myfile.txt | foreach-object {
   $_.Matches[0].Groups[0].Value
}

Upvotes: 2

Related Questions