A utility to extract, process, and replace text in files based on line and column offsets.
Project description
Splice Replace
splice-replace is a Python utility to extract, process, and replace text in a file by running an external script on the extracted text.
Usage
./splice_replace.py <script_to_run> <file_path> <start_line> <start_col> <end_line> <end_col>
./splice_replace.py 'sed s/big/small/' myfile.txt 2 10 4 15
Example
Use sed on section of file:
echo "01 First Line" > myfile.txt
echo "02 Second Line" >> myfile.txt
echo "03 Third Line" >> myfile.txt
echo "04 Fourth Line" >> myfile.txt
echo "05 Fifth Line" >> myfile.txt
./splice_replace.py 'sed s/Second/2nd/' myfile.txt 2 1 2 17
Which results in the file:
01 First Line
02 2nd Line
03 Third Line
04 Fourth Line
05 Fifth Line
Use sort on section of file:
echo "01 MUPPET CHARACTERS" > myfile.txt
echo "02 HERE IS A LIST OF MUPPET CHARACTERS ALPHABETICALLY" >> myfile.txt
echo "03" >> myfile.txt
echo "Kermit" >> myfile.txt
echo "Gonzo" >> myfile.txt
echo "Beaker" >> myfile.txt
echo "Animal" >> myfile.txt
echo "Fozzie Bear" >> myfile.txt
echo "09" >> myfile.txt
echo "10 THAT CONCLUDES THE LIST" >> myfile.txt
./splice_replace.py 'sort' myfile.txt 4 1 9 1
Which results in the file:
01 MUPPET CHARACTERS
02 HERE IS A LIST OF MUPPET CHARACTERS ALPHABETICALLY
03
Animal
Beaker
Fozzie Bear
Gonzo
Kermit
09
10 THAT CONCLUDES THE LIST
Installation
From source:
git clone
cd splice-replace
pip install -r requirements.txt
python setup.py install
Using pip:
pip install splice-replace
Motivation
Intellij IDEA has a feature called "External Tools" which allows you to run external scripts. You can pass the script
special variables like $FilePath$, $SelectionStartLine$, $SelectionStartColumn$, $SelectionEndLine$, and $SelectionEndColumn$.
I wanted to be able to configure the external tool to run a script on a selection of text in a file such that the script receives
that selection as stdin and gives the replacement as stdout. IntelliJ IDEA does not support this feature directly, so I wrote this utility.
For example if you wanted to run the sort command on a selection of text in a file, you could configure an external tool to run.
First, you would create the external tool in Settings -> Tools -> External Tools, then give it a name like "sort", set the program to
/path/to/splice-replace and the arguments to "sort" $FilePath$ $SelectionStartLine$ $SelectionStartColumn$ $SelectionEndLine$ $SelectionEndColumn$.
Then you could select a section of text in a file and run the external tool "sort" on that selection.
Of course, you can replace sort with your own utility or script.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file splice_replace-0.1.2.tar.gz.
File metadata
- Download URL: splice_replace-0.1.2.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3002fb52d2f5f351a4c9fc56d52ae1be515d9488c9c2880ded8a9d113d2eafee
|
|
| MD5 |
26bd7e18af5143276f955cde9508d2ff
|
|
| BLAKE2b-256 |
0ffe9d9be12166eba027c1d14462618521a92e65134a398e1047c55577a1610b
|
File details
Details for the file splice_replace-0.1.2-py3-none-any.whl.
File metadata
- Download URL: splice_replace-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58a77a5f8bc218ea8dfd8a6ab274e0c8261d529be8b6f9303cb35fd7bc885f8a
|
|
| MD5 |
5a31bf0e5104ca743671c5535ba99c40
|
|
| BLAKE2b-256 |
e4aa5c1ac51156e9261c8efdc833b13e51d01186f8b49240b3ddf70e22bd8730
|