Skip to main content

CLI utility that parses and transforms text

Project description

https://img.shields.io/pypi/v/pysed.svg https://travis-ci.org/dslackw/pysed.svg?branch=master https://landscape.io/github/dslackw/pysed/master/landscape.png https://img.shields.io/codacy/5ef917a8c6354d8f9d984183c8fb5847.svg https://img.shields.io/pypi/dm/pysed.svg https://img.shields.io/badge/license-GPLv3-blue.svg https://img.shields.io/github/stars/dslackw/pysed.svg https://img.shields.io/github/forks/dslackw/pysed.svg https://img.shields.io/github/issues/dslackw/pysed.svg

About

CLI utility that parses and transforms text written in Python.

Pysed is a Python stream editor, is used to perform basic text transformations from a file. It reads text, line by line, from a file and replace, insert or print all text or specific area. Actually pysed is a passage of Python module ‘re’ in terminal.

Read more for Regular Expression Syntax

[CHANGELOG]

Installation

$ pip install pysed --upgrade

uninstall

$ pip uninstall pysed

Command Line Tool Usage

pysed is utility that parses and transforms text

Usage: pysed [OPTION] {pattern} {repl} {lines/max/flag} [input-file]

Options:
  -h, --help                   display this help and exit
  -v, --version                print program version and exit
  -r, --replace                search and replace text
  -f, --findall                find all from pattern in text
  -s, --search                 search for the first matching
  -m, --match                  pattern matching in the beginning
  -l, --lines                  search pattern and print lines
  -g, --highlight              highlight and print text
  -s, --stat                   print text statistics
      --write                  write changes to file

Python regex flags

Syntax

Python syntax, Meaning

I or IGNORECASE

re.IGNORECASE, ignore case.

M or MULTILINE

re.MULTILINE, make begin/end {^, $} consider each line.

S or DOTALL

re.DOTALL, make . match newline too.

U or UNICODE

re.UNICODE, make {w, W, b, B} follow Unicode rules.

L or LOCALE

re.LOCALE, make {w, W, b, B} follow locale.

X or VERBOSE

re.VERBOSE, allow comment in regex.

Usage Examples

$ cat text.txt
This is my cat,
 whose name is Betty.
This is my dog,
 whose name is Frank.
This is my fish,
 whose name is George.
This is my goat,
 whose name is Adam.

Replace text:

$ pysed -r "name" "surname" text.txt
This is my cat,
 whose surname is Betty.
This is my dog,
 whose surname is Frank.
This is my fish,
 whose surname is George.
This is my goat,
 whose surname is Adam.

Replace text in specific lines:

$ pysed -r "name" "surname" 2,4 text.txt
This is my cat,
 whose surname is Betty.
This is my dog,
 whose surname is Frank.
This is my fish,
 whose name is George.
This is my goat,
 whose name is Adam.

Replace text in specific lines and max:

$ pysed -r "is" "IS" 1,7/1 text.txt
ThIS is my cat,
 whose surname is Betty.
This is my dog,
 whose surname is Frank.
This is my fish,
 whose name is George.
ThIS is my goat,
 whose name is Adam.

Add character to the beginning of each line:

$ pysed -r "^" "# " text.txt
# This is my cat,
#  whose name is Betty.
# This is my dog,
#  whose name is Frank.
# This is my fish,
#  whose name is George.
# This is my goat,
#  whose name is Adam.

Add character to the end of each line:

$ pysed -r "$" " #" text.txt
This is my cat, #
 whose name is Betty. #
This is my dog, #
 whose name is Frank. #
This is my fish, #
 whose name is George. #
This is my goat, #
 whose name is Adam. #

Find all matching pattern:

$ pysed -f "name " text.txt
name name name name

Find all matching pattern in specific lines:

$ pysed -f "name " "" 2,4 text.txt
name name

Search and print lines:

$ pysed -l "name" text.txt
2 whose name is Betty.
4 whose name is Frank.
6 whose name is George.
8 whose name is Adam.

Highlight text:

$ pysed -g "name" "red" text.txt
This is my cat,
 whose name is Betty.
This is my dog,
 whose name is Frank.
This is my fish,
 whose name is George.
This is my goat,
 whose name is Adam.

Print statics text:

$ pysed -t text.txt
Lines: 8, Words: 32, Chars: 125, Blanks: 27

Use the argument "--write" in any case when you want to save the changes:

$ pysed -r "name" "surname" text.txt --write


Use as piping:

$ echo "This is my cat, whose name is Betty" | pysed -r "cat" "dog"
This is my dog, whose name is Betty

$ repl="fish"
$ echo "This is my cat, whose name is Betty" | pysed -r "cat" $repl
This is my fish, whose name is Betty

$ echo "This is my cat, whose name is Betty" | pysed -r "[^\W]+" "-"
- - - -, - - - -

$ echo "This is my cat, whose name is Betty" | pysed -r "is" "IS" 0/1
ThIS is my cat, whose name is Betty

$ echo "910a13de57dfbdf6f06675db975f8407" | pysed -r "[^\d+]"
91013576066759758407

$ echo "910a13de57dfbdf6f06675db975f8407" | pysed -f "\d+"
910 13 57 6 06675 975 8407

$ echo "910a13de57dfbdf6f06675db975f8407" | pysed -s "\d+"
910

$ echo "910a13de57dfbdf6f06675db975f8407" | pysed -s "(\d+)(\w+)" "" 0/1
910

$ echo "910a13de57dfbdf6f06675db975f8407" | pysed -s "(\d+)(\w+)" "" 0/2
a13de57dfbdf6f06675db975f8407

$ echo "The temperature today is at +12 degrees Celsius" | pysed -s ".\d+"
+12

$ echo "/usr/local/bin" | pysed -r "/local" ""
/usr/bin

$ echo "/usr/local/bin" | pysed -r "/LoCal" "" //IGNORECASE
/usr/bin

Please report Issues

Download files

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

Source Distribution

pysed-0.7.2.tar.gz (21.1 kB view details)

Uploaded Source

File details

Details for the file pysed-0.7.2.tar.gz.

File metadata

  • Download URL: pysed-0.7.2.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pysed-0.7.2.tar.gz
Algorithm Hash digest
SHA256 f594bb9cb0db68be494e57f4ad1e8cd70bcfbd034b376644e4551e2a29f6f32e
MD5 c65c54c9a3f89e4263a25343c003fb74
BLAKE2b-256 595aa5887d3a9c26874a537bc3ee6f02b54bba8f02c036a73ba3220052badca7

See more details on using hashes here.

Supported by

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