Wednesday, May 8, 2013

Equivalent of grep in windows


There are instances where we want to search for table / field referenced in sqr on windows.
The built in windows command FindStr mirrors the capabilities of the Unix command Grep.

Findstr /?
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]
  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
             subdirectories.
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See “color /?”
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename Specifies a file or files to search.

Example 1:

findstr /n /i /s /c:"ps_job" "d:\sqr\*.*" > "c:\temp\output.txt"

The above example will search for ps_job in all files on folder / sub-folders of d:\sqr\ and redirect the output to output.txt in c:\.
/n - will print the line number ps_job is referenced.
/i - will ignore case-sensitive
/s - will search string in all files on folder and subfolders od d:\sqr\
/c - denotes to search for string.

Example 2:

findstr /n /i /s /g:"d:\search_all.txt" "d:\sqr\*.*" > "c:\temp\output.txt"

we can also search for multiple strings in one command.
save the file having multiple strings in d:\search_all.txt (multiple strings should be ended with newline)
this example will search of all strings defined in d:\search_all.txt on d:\sqr\ and redirect the output to c:\temp\output.txt

No comments: