Skip to main content

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 8 1

Which results in the file:

01 MUPPET CHARACTERS
02 HERE IS A LIST OF MUPPET CHARACTERS ALPHABETICALLY
03
Animal
Beaker
Gonzo
Kermit
Fozzie Bear
09
10 THAT CONCLUDES THE LIST

Installation

git clone
cd splice-replace
pip install -r requirements.txt
python setup.py install

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

splice_replace-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

splice_replace-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file splice_replace-0.1.1.tar.gz.

File metadata

  • Download URL: splice_replace-0.1.1.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

Hashes for splice_replace-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f31e24e3c1e868ef24123bf50bc5f055b7878eae478ff5224737e9ef2844a3bf
MD5 6222ee17819735c39b42c29b23f55b21
BLAKE2b-256 66b9009cf72e73dc40f2c5c13e446df5c3c74219512215af06ee6803efb2b656

See more details on using hashes here.

File details

Details for the file splice_replace-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: splice_replace-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for splice_replace-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cdf6e77a9b1f41aa96d0626c21764c991cd1c2fe7a8e070718e1ae9341774271
MD5 7b6a560f99aead94741537914dee61a9
BLAKE2b-256 0ddc1105b8af21f77c2d018ac2586310f0cc8ed83067ffb941b84cddcfcd2ee3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page