Use Python for sed like shell one-liners.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Use Python for sed or perl-like shell scripts

Pyle makes it easy to use Python as a replacement for command line tools such as sed or perl. For instance, to perform an in-place string substitution, overwriting the original file with the updated file, you might do:

pyle -ie "re.sub(r'alien(s|)?', r'ghost\1', line)"

To print the first 20 characters of each line of a file:

cat | pyle -e "line[:20]"


pyle -e "line[:20]"

Pyle imports the sh module by default, which enables easy shell command execution.

List all /tmp/ files with a filename with an even length:

ls /tmp/ | pyle -e "'-l', line) if len(line) % 2 == 0 else None"

In addition to line, a list called words is also available which is the current line split by whitespace. To print just the URLs in an Apache access log (the seventh "word" in the line):

tail access_log | pyle -e "words[6]"

Print the SHA 256 sum of each *.py file in the current directory:

$ ls *.py | pyle -m hashlib -e "'%s %s' % (hashlib.sha256(line).hexdigest(), line)"

If your expression returns a list or a tuple, the items will be printed joined by spaces. With that in mind we can simplify the above example:

$ ls *.py | pyle -m hashlib -e "(hashlib.sha256(line).hexdigest(), line)"

Print the first five lines of each file with file names and line numbers:

$ pyle -e "'%-15s:%04d %s' % (filename, 1 + num, line) if num < 5 else None" *.py

You can also specify multiple expressions by repeating the -e option. Just like in sed each expression acts on line (and words, etc.) as modified by the previous expression. E.g. replace a letter in first five words of a file:

$ pyle -e "words[:5]" -e "re.sub('A', 'B', line)"

The idea for Pyle is based on Graham Fawcett's PyLine. Pyle is mostly compatible with PyLine but requires a -e before the evaluation statement.


pip install pyle

Why Pyle?

Some of us are just simply awful at remembering the sed, perl or even bash syntax but feel right at home with Python. Python code is often a little more verbose but what good is saving characters if you can't remember what they do?

Here's an example of sed vs pyle. This isn't a very good sed expression, admittedly, but the people who will find Pyle useful are not sed experts.

To change home directories from /var/X to /home/X:

sed 's/^\(\([^:]*:\)\{5\}\)\/var\/\(.*\)/\1\/home\/\3/g' /etc/passwd
pyle -e "re.sub(r'^(([^:]*:){5})/var/(.*)', r'\1/home/\3', line)" /etc/passwd

If you find the Python code more readable, Pyle is for you.


This file and pyle --help.


Free to use and modify under the terms of the BSD open source license.