Native Grep and Cat Alternatives in Windows (sort of…)

If you use a Linux operating system for much you are probably familiar with the “grep” and “cat” commands and their usage.   Grep is probably one of the most powerful utilities in Linux because it allows for text and regular expression searching a number of different ways.  Grep is so popular that it has even been ported to Windows by many.  So how do you get grep functionality in Windows?  Well, you can download many of the installable versions out there and you’re set.  Unfortunately, in a pristine server environment with change control or limited privileges this is not an option.  Let’s look at a fairly decent alternative that is native to windows.

The “find” command in windows is defiantly nowhere close to as powerful as grep.  However, some of the basic functionality is still available.  First, let’s look at my personal favorite usage case.  Let’s generate some output in a terminal window with “netstat” in both Linux and Windows as the results of each are similar.  Then, let’s cut back on the results to be more manageable.    For this I am talking about “netstat –an” in Windows and “netstat –aln” in Linux.  Go ahead and issue one or each of these on your systems now.

As you see the result set is fairly long.  Being a network guy I need to know things about specific network services.  For instance, is that web service listening?  With Grep we would do something like this and get the results shown:

[root@test01 ~]# netstat -aln | grep :80
tcp        0      0 :::80                       :::*                        LISTEN
tcp        0      0 ::ffff:192.168.1.23:80        ::ffff:192.168.1.8:16587      TIME_WAIT

In the windows version you can do something similar with a command and output like this:

C:\>netstat -an | find ":80"
  TCP    0.0.0.0:80             0.0.0.0:0              LISTENING
  TCP    192.168.1.1:80           192.168.1.8:4829         ESTABLISHED

As you can see the Windows option is quoted.  It must be quoted and it must be the string you want to match.  Sorry, regular expressions are not supported as they are in Grep.  I will not put you to sleep with a stack of examples but it is worth mentioning “find” has various inclusion and exclusion options.   They are as follows:

C:\>find /?
Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
  [drive:][path]filename
             Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.

Ok, so you get the idea I’m sure.  Now, let’s talk about the “cat” command and the windows alternative “type” for a minute.  “cat” in Linux allows one or more file to be printed to the terminal.  A very nice way to get file contents into plain sign without having to launch a text editor, etc.  It is also a nice way to get file contents into another utility such as “grep” allowing a search of multiple files with only a single command string.  For the next few examples lets pick on the web server logs of each platform and explore for the host “192.168.1.8”.   In Linux we would see something like this:

[root@test01 httpd]# cat error_log | grep 192.168.1.8
[Wed Dec 05 13:44:05 2012] [error] [client 192.168.1.8] File does not exist: /var/www/html/asf

Now, let’s do something similar in Windows with “type” and “find”:

C:\WINDOWS\system32\LogFiles\W3SVC1>type ex121205.log  | find "192.168.1.8"
2012-12-05 18:37:29 W3SVC1 10.10.1.1 GET /asd - 80 - 192.168.1.8 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+rv:15.0)+Gecko/20100101+Firefox/15.0 404 0 2

As you can see there are many ways these utilities can be used.  I hope you have found this useful!  As always, feel free to leave comments or other tips you may have around these utilities.

 

This entry was posted in Networking and tagged , , , , . Bookmark the permalink.

One Response to Native Grep and Cat Alternatives in Windows (sort of…)

  1. FS says:

    I am not 100%, but I am sure you can download DJGPP and SET PATH= and get grep functionality without permissions.

    DJGPP is technically for DOS but will run fine in windows.

Leave a Reply

Your email address will not be published. Required fields are marked *