The difference between this implementation of grep in python from others
- Searches for different types of input data without specifying the type
- List[str]
- TextIOWrapper: Opened files
- str: Single-line and multi-line strings
- str: Just file paths like
dir/filename.txt
- Works primarily inside the python code and returns a List[str] type that is convenient for further processing
- Has an unlimited number of input parameters
- By default, it works with normal (extended) regular expressions Yes - you can simple write in python code:
pattern = r"hello"
str_variable = "hello world"
list_variable = ["hello there", "general kenobi"]
file_path = "test_file.txt" # File contain: "This is a test file.\nhello from file\nAnother line."
result = grepli(pattern, str_variable, list_variable, file_path)
print(result)
And you get sum result:
['hello world', 'hello there', 'hello from file']
See this test code with this example: https://github.com/linxuil/grepli/blob/main/tests/test_grepli.py#L45
This project is grep function implementation for python. I use this "grepli" function for:
- Analyse log files in tests where i need info from linux
- tcpdump
- dmesg
- journalctl
- <'etc...'>
- Gateway from files in file system to list type in python
if run with dot regex
grepli . 'filepath.txt'
Try use python script via cmd without install:
$ cd <'prj_folder'>
[prj_folder]$ TST_MULTILINE_VAR='first tested line
second line test
third line'
[prj_folder]$ /bin/python3 console_scripts/main.py 'foo|text.$' foo_yes 'bar_no' 'text. not last' 'last text.' 'tests/pre_created_file.txt' "${TST_MULTILINE_VAR}"
['foo_yes', 'last text.', 'This is a test text.']
You can install via pip
in venv:
# Pull repo
$ REPO_URL='https://github.com/linxuil/grepli'&&\
git clone "${REPO_URL}" 'grepli'&&\
cd grepli
# Install venv if needed
$ sudo apt install python3-venv
# Create and activate venv in directory "venv"
$ VENV_DIR_NAME='venv'&&\
python3 -m venv "${VENV_DIR_NAME}"&&\
source venv/bin/activate
# Install module
$ pip install -e .
# Try use console command
$ grepli '\b\w{4}\b' 'text with 4 letters wods' 'text_test' 'tests/pre_created_file.txt'
This command show output contain:
- One string from input parameters
- Two lines from file
tests/pre_created_file.txt
['text with 4 letters wods', 'This is a test text.', 'Text like short life 123.']
Then you need exit from venv
$ deactivate
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/YouFeature
) - Write any changes in code
- Add changes to index (
git add -A
) - Commit your Changes (
git commit -m 'You description'
) - Push to the Branch (
git push origin feature/YouFeature
) - Open a Pull Request on github
Distributed under the MIT License. See LICENSE.md
for more information.
linxuil - linxuil.g@gmail.com
Project Link: https://github.com/linxuil/grepli