Saturday 17 September 2011

grep in Windows (dos)

I sometimes wish that there was a grep command in windows, well, it turns out that there is, sort of, it's called find.

You can just pipe the output of a command to find and it will do pretty much the same as grep, e.g.
netstat -anp TCP | find  "5555"
TCP    0.0.0.0:5555          0.0.0.0:0              LISTENING
TCP    10.168.2.115:5555     10.168.2.115:39898    ESTABLISHED
TCP    10.168.2.115:39898    10.168.2.115:5555    ESTABLISHED
You can use the switch /v to get all lines that don't contain the string:
dir | find /V ".cer"
 Volume in drive C has no label.
 Volume Serial Number is B8E9-71D1

 Directory of C:\Dev\CACerts

05/09/2011  18:26    <DIR>          .
05/09/2011  18:26    <DIR>          ..
12/08/2011  18:52             3,298 tony.pfx
12/08/2011  19:19             3,274 tonytest.pfx
              2 File(s)         6,572 bytes
               2 Dir(s)   4,861,059,072 bytes free
It's possible to pipe the output multiple times. In the example below you'll see only established connections on port 5555, which may or may not be of any use but it's neat.
netstat -anp TCP | find "ESTABLISHED" | find "5555"
TCP    10.168.20.115:5555     10.168.20.115:41184    ESTABLISHED
TCP    10.168.20.115:41184    10.168.20.115:5555     ESTABLISHED
I guess you can use it as a sort of logical and capability, similarly using /V switch , you can use it as not and, e.g. for established and not in port 5555
netstat -anp TCP | find "ESTABLISHED" | find /V "5555"
 TCP    10.168.20.115:445      10.168.20.101:1974     ESTABLISHED
 TCP    10.168.20.115:3389     10.168.20.203:60815    ESTABLISHED

No comments:

Post a Comment