Home > Programming > Get a SQL Server GREP Command Line Text Search

Get a SQL Server GREP Command Line Text Search

in Programming

SQL server grep is a command line text search utility written originally for Unix. The origin of the term is the first letters of Global / Regular Expression / Print series of text editor instructions. The function is used to search for text matching a set of conditions for a given destination. It was no surprise that when developers started coding for SQL Server, it became a common function.

The only probable limitation for this utility is that the destination database should be in *.txt format. As the name suggests, this particular command is used to look for ‘Global’ standard input for a specified ‘Regular Expression’ and ‘Print’ it to an output. The query line for this function is pretty simple. For example, if you want to search this article (assuming that it is saved as a .txt extension named SQL_SERVER_GREP.txt), for the word ‘function’; your command line will be:

grep function SQL_SERVER_GREP.txt

The function can also accept several useful options such as:

  • To print matching lines with block number
  • To display number of matching lines
  • To match cases insensitively
  • To censor error messages
  • To display non matching lines

Versatility of the GREP Function

Query3The function also supports operands like pattern_list and file. The STDIN or standard input is used only when there are no ‘file’ operands specified. While running this particular utility, take extra care when using characters in pattern_list that could also interest the command interpreter. Use the pattern_list command in single quotes to be on the safe side.

The best thing about the SQL Server Grep utility command is that it has been enhanced to be upwardly compatible with the historical functionality of egrep and fgrep commands too! It therefore comes as no surprise that when developers run SWL queries for expressions in *.txt files, the grep figures up in their favorites and adds them to the results.

Previous post:

Next post: