Monday, April 30, 2007

Windows Grep Functionality

Windows has a find command which can be used in batch files and in the cmd prompt to search for strings in files or in output. My most recent discoveries is the type command and using find in a grep like functionality.

Say you have a text file with lines of code/numbers/names/etc and you'd like to see the line which has the text you are looking for. You'd type the following:

> type file.txt | find /I "something"

Replace file.txt with the filename you want to search in, and something with the text you are looking for.

The /I switch is to make the search case insensitive. Type find /? at the command prompt for more options.

Here is an example. If you want to find out if your python bin directory is in your path you would type:

> SET | find /I "python"
results:
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Common Files\GTK\2.0\bin;C:\bin;C:\Python24;C:\Program Files\Windows Script Encoder;C:\Program Files\7-Zip
Note: The SET command prints out your environment variables. Much like env does on *ix.

Follow here, for more Useful Windows command prompt commands.

2 comments:

Anonymous said...

Saves my day dude. Thanks.

Anonymous said...

Thanks mate ...
You saved lot of time ...