findstring
This program, findstring, allows you to search for a specific string within files in a directory, including support for searching within PDF and DOCX files. It works similarly to the grep -rI command but adds the ability to read and search through PDF and DOCX files.
Install
pip install findstring
Usage
findstring [OPTIONS] search_string
Options
search_string: The string to search for in the files.
Optional Arguments
-
-h,--help: Show the help message and exit. This option provides a summary of how to usefindstring, including descriptions of all available options. -
-b,--binary:
Scan binary files as well. If this option is used, the tool will attempt to read binary files and search for the specified string. -
-d,--directory:
Root directory to start searching from. If not specified, the current directory (.) is used by default. -
-t,--text:
Show the matched lines containing the search string in the output. -
-l,--max_length:
Maximum number of characters to be shown as a result. The default is 0, which means no limit is set. When--max_lengthis specified,--textis also enabled. -
-v,--verbose:
Enable verbose output. The program will provide more detailed information about its operation, including which directories and files are being checked.
Features
-
PDF Support:
The program can search within PDF files using thepdfminerlibrary. It extracts text from the PDF and searches for the specified string. -
DOCX Support:
DOCX files are also supported, with text extraction handled by thedocxlibrary. -
Binary File Scanning:
When the--binaryflag is enabled, the program will attempt to read binary files and search for the specified string. -
Context Display:
When the--textoption is enabled, the tool will display the lines containing the search string, with maximum numbers of characters specified by the--max_lengthoption.
Examples
Search for a string in the current directory
findstring "example_string"
Search for a string in a specific directory
findstring -d /path/to/directory "example_string"
Search with verbose output and show matched lines
findstring -tv "example_string"
Limit the length of the output text to 50 characters
findstring -l 50 "example_string"
Search in binary files
findstring -b "example_string"
Error Handling
The program will attempt to handle errors such as unreadable files gracefully. If an error occurs while reading a file, the program will skip the file and continue processing the rest, optionally displaying an error message if verbose mode is enabled.
Highlighting Search Results
The program is configured to highlight matching search results in bold red text by default. This highlighting is controlled by the GREP_COLORS environment variable, specifically using the mt= option.
If you wish to change the color or style of the highlighted text, you can modify the mt= setting in the GREP_COLORS environment variable. The mt= value is a color code that specifies the style and color used for matching text.
For example:
- Bold Red (default):
mt=1;31 - Bold Green:
mt=1;32 - Underline Blue:
mt=4;34
To apply a custom color, you can set the GREP_COLORS environment variable in your shell as follows:
export GREP_COLORS='mt=1;32'
This example would change the highlighted text to bold green.
The program automatically detects if the output is directed to a terminal (TTY). If not, it will print the plain text without any colorization.
Using as a Library
You can use findstring as a library as:
from findstring import findstring
findstring("/path/to/directory" "example_string")
Arguments of findstring function is as follows.
-
root_dir(str):
The root directory to start searching from. -
search_string(str):
The string to search for within the files. -
verbose(bool, optional):
IfTrue, print additional information during the search process. Default isFalse. -
show_text(bool, optional):
IfTrue, display the matched line containing the search string. Default isFalse. -
max_length(int, optional):
Maximum number of characters to display in the result. Default is0(no limit). -
binary(bool, optional):
IfTrue, scan binary files as well. Default isFalse.
Release files for findstring 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| findstring-1.0.0.tar.gz | 9.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| findstring-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.7 kB
Release files / findstring-1.0.0.tar.gz
| Download URL | findstring-1.0.0.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
635064d20aa26b693f4ff0275b62d57caa47ac0530834ed306d42bd7692cb259
|
|
BLAKE2b-256 checksum How to use checksums |
68f89c80eed7188156190418742f588ed0cb58bed038f264bacc3fc0d73f74de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / findstring-1.0.0-py3-none-any.whl
| Download URL | findstring-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2d9bbcf1f79ac60e966457900773f3f562bf4677b33b51df2b47924340bacf43
|
|
BLAKE2b-256 checksum How to use checksums |
0402906cb231a45e61f69da96f72feffc08ccdaffd84b5c6528b383a64c61e5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|