Giter VIP home page Giter VIP logo

strs's Introduction

Easy string tools for the shell

strs has more than 50 tools that make working with strings in the shell easier.

strs brings common string convenience methods to shells like Bash, because string manipulation in shells can be hard.

$ str capitalize "hey there! :fire:" | str to-emoji
Hey there! ๐Ÿ”ฅ

$ str repeat 5 โญ | str join ๐ŸŒ™
โญ ๐ŸŒ™ โญ ๐ŸŒ™ โญ ๐ŸŒ™ โญ ๐ŸŒ™ โญ

Table of Contents

Commands

capitalize casefold center col count
contains endswith find has-emoji index
isalnum isalpha isascii isdecimal isdigit
isidentifier islower isnumeric isprintable isspace
istitle isupper join length lower
ljust lstrip nth partition replace
replace-first repeat rfind rindex rpartion
rsplit rstrip rjust upper split
strip sbob startswith substring slice
title to-ascii to-emoji from-emoji zerofill

Usage

Practical example

If you're on Debian, you can use strs to take your apt sources from Debian testing, point them to Debian stable on the fly, and then send them to a stable machine:

$ str replace testing stable < sources.list | ssh host "cat > /etc/apt/sources.list"

To do the same with sed, you'd need to know sed's regex syntax, if your sed comes with the -i feature flag, and if it's GNU sed or BSD sed.

strs, on the other hand, has a uniform interface and set of features across platforms, shells and operating systems, including Windows.

String manipulation in the shell

strs has string tools that are similar to those that are built into Bash, and it has commands for features that Bash doesn't have syntactic sugar for, as well.

The following examples of Bash code only work with Bash, whereas strs commands will work if you're using Bash, zsh, PowerShell or something else.

String length

Bash

string='This is an example.'

$ echo "${#string}"
19

strs

$ str length "$string"
19

Or, using pipes:

$ echo $string | str length
19

Strip

Bash

front='This'
end='example.'

$ echo "${string#$front}"  # from front
 is an example.

$ echo "${string%$end}"  # from end
This is an
strs
$ str lstrip $front "$string"
 is an example.

$ str rstrip $end "$string"
This is an

$ str strip $front$end "$string"
 is an

Or, using pipes:

$ echo $string | str lstrip $front
 is an example.

$ echo $string | str rstrip $end
This is an

$ echo $string | str strip $front$end
 is an

Capitalization

Bash

$ echo "${string^}"  # capitalize first char
This is an example.

$ echo "${string^^}"  # capitalize all
THIS IS AN EXAMPLE.

$ echo "${string,,}"  # lower all
this is an example.

strs

$ str capitalize "$string"
This is an example.

$ str upper "$string"
THIS IS AN EXAMPLE.

$ str lower "$string"
this is an example.

Or:

$ echo $string | str capitalize
This is an example.

$ echo $string | str upper
THIS IS AN EXAMPLE.

$ echo $string | str lower
this is an example.

Replace

Bash

old='an'
new='a'

$ echo "${string//$old/$new}"  # replace all
This is a example.

$ echo "${string/$old/$new}"  # replace first
This is a example.

strs

$ str replace $old $new "$string"
This is a example.

$ str replace $old $new "$string" --count 1
This is a example.

$ str replace-first $old $new "$string"
This is a example.

Or:

$ echo $string | str replace $old $new
$ echo $string | str replace $old $new --count 1
$ echo $string | str replace-first $old $new

String manipulation tools

strs has string manipulation commands that don't have syntactic sugar in Bash.

string='This is an example.'

$ str casefold "$string"
this is an example.
$ echo $string | str casefold
this is an example.

Center

width=40

$ str center $width "$string"
          This is an example.           
$ echo $string | str center $width
          This is an example.           

Count

countChar='e'

$ str count $countChar "$string"
2
$ echo $string | str count $countChar
2

Find

find='e'

$ str find $find "$string"
11
$ echo $string | str find $find
11

Index

$ str index $find "$string"
11
$ echo $string | str index $find
11

Join

on='_'

$ str join $on $string
This_is_an_example.
$ str split ' ' "$string" | str join $on
This_is_an_example.

Partition

part=' '

$ str partition "$part" "$string"
This

is an example.
$ echo $string | str partition "$part"
[...]

Split

split=' '

$ str split "$split" "$string"
This
is
an
example.
$ echo $string | str split "$split"
[...]

Strip

strip='.'

$ str strip $strip "$string"
This is an example
$ echo $string | str strip $strip
This is an example

Swap case

$ str swapcase "$string"
tHIS IS AN EXAMPLE.
$ echo $string | str swapcase
tHIS IS AN EXAMPLE.

To title case

$ str title "$string"
This Is An Example.
$ echo $string | str title
This Is An Example.

Zero fill

$ str zfill $width "$string"
000000000000000000000This is an example.
$ echo $string | str zfill $width
000000000000000000000This is an example.

Repeat

$ str repeat 3 "$string"
This is an example.
This is an example.
This is an example.
$ echo $string | str repeat 3
[...]

Left justify

$ str ljust $width "$string" --fillchar '*'
This is an example.*********************
$ echo $string | str ljust $width --fillchar '*'
This is an example.*********************

Left strip

$ str lstrip T "$string"
his is an example.
$ echo $string | str lstrip T
his is an example. 

Right find

$ str rfind $find "$string"
17
$ echo $string | str rfind $find
17

Right index

$ str rindex $find "$string"
17
$ echo $string | str rindex $find
17

Right justify

$ str rjust $width "$string"
                     This is an example.
$ echo $string | str rjust $width
                     This is an example.

Right strip

remove='.'

$ str rstrip $remove "$string"
This is an example
$ echo $string | str rstrip $remove
This is an example

Right partition

$ str rpartition "$part" "$string"
This is an

example.
$ echo $string | str rpartition "$part"
[...]

Right split

$ str rsplit "$split" "$string"
This
is
an
example.
$ echo $string | str rsplit "$split"
[...]

More string tools

strs has tools that deal with UTF-8, ASCII and emojis, and it has tools that aren't found in Python or common shells.

To ASCII

$ str to-ascii "It is 20ยฐ Celsius outside."
It is 20deg Celsius outside.
$ str to-ascii "ว ฤš ว ว‘ ว“ ฤŒ ฤŽ วฆ ศž วฐ วจ ฤฝ ล‡ ล˜ ล  ลค ลฝ"
A E I O U C D G H j K L N R S T Z

Substring

$ str substring 3 "Hey there! ๐Ÿ”ฅ"
Hey

You can use negative indices like you can in Python:

$ str substring -3 "Hey there! ๐Ÿ”ฅ" --start 4
there

Slice

You can use Python's slice syntax directly, too.

$ str slice 4:-3 "Hey there! ๐Ÿ”ฅ"
there

Contains

$ str contains ๐Ÿ”ฅ "Hey there! ๐Ÿ”ฅ"; echo $?
0

Emojis

$ str has-emoji "Hey there! ๐Ÿ”ฅ"; echo $?
0
$ str from-emoji "Hey there! ๐Ÿ”ฅ"
Hey there! :fire:

Get columns

$ str col 2 'hello world'
world
$ echo -e 'hello\tworld' | str col 2
world

Return nth lines

$ sudo dmesg | str nth 50
[73627.811739] Filesystems sync: 0.02 seconds
$ str sbob "squidward likes krabby patties"
sQuIdWaRd LiKeS kRaBbY pAtTiEs

String validation tools

strs also brings Python's string validation methods to the shell.

Here's an example of how you'd use them in a conditional statement, followed by examples of other validation tools:

string='This is an example.'


if str startswith T "$string" && str endswith . "$string"; then
  printf "Starts with T and ends with .\n"

elif str contains example "$string"; then
  printf "Contains 'example'\n"

elif !str isalnum "$string"; then
  printf "Isn't alphanumeric\n"

fi

Starts with

$ str startswith T "$string"; echo $?
0
$ echo $string | str startswith T; echo $?
0

Ends with

$ str endswith . "$string"; echo $?
0
$ echo $string | str endswith .; echo $?
0

Is alphanumeric

$ str isalnum "$string"; echo $?
0
$ echo $string | str isalnum; echo $?
0

Is alphabetic

$ str isalpha "$string"; echo $?
1
$ echo $string | str isalpha; echo $?
1

Is ASCII

$ str isascii "$string"; echo $?
0
$ echo $string | str isascii; echo $?
0

Is decimal

$ str isdecimal "$string"; echo $?
1
$ echo $string | str isdecimal; echo $?
1

Is digit

$ str isdigit "$string"; echo $?
1
$ echo $string | str isdigit; echo $?
1

Is valid Python identifier

$ str isidentifier "$string"; echo $?
1
$ echo $string | str isidentifier; echo $?
1

Is lower case

$ str islower "$string"; echo $?
1
$ echo $string | str islower; echo $?
1

Is numeric

$ str isnumeric "$string"; echo $?
1
$ echo $string | str isnumeric; echo $?
1

Is printable

$ str isprintable "$string"; echo $?
0
$ echo $string | str isprintable; echo $?
0

Is space character

$ str isspace "$string"; echo $?
1
$ echo $string | str isspace; echo $?
1

Is title case

$ str istitle "$string"; echo $?
1
$ echo $string | str istitle; echo $?
1

Is upper case

$ str isupper "$string"; echo $?
1
$ echo $string | str isupper; echo $?
1

Installation

Prerequisites

  • A Unix shell like Bash, or PowerShell or Command Prompt on Windows
  • Python 3.10+
  • requirements.txt

PyPI

python3 -m pip install strs

You can view the strs package on PyPI.

strs's People

Contributors

alexdelorenzo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

strs's Issues

Optimize launch time

Problem

Here's the issue:

$ time python -c "import sys, emoji, fire, more_itertools"
real    0m0.121s
user    0m0.105s
sys     0m0.019s

$ time python -c "import fire"
real    0m0.106s
user    0m0.086s
sys     0m0.023s

$ time python -m strs isalnum 1
real    0m0.146s
user    0m0.115s
sys     0m0.033s

Start up time, at minimum, will have a ~100ms duration on a NVMe drive with hot caches.

This doesn't really matter when using strs on the command line, but when you're writing scripts with a lot of string manipulation, it can be relatively slow:

$ time echo "$(str repeat 5 test | str join : | str split : | str nth 1 2 3 | str join ' ' | str col 1)"
test
real    0m0.234s
user    0m0.861s
sys     0m0.168s

Potential solutions

Using click can shave off ~34ms from start up:

$ time python3 -c "import click"
real    0m0.072s
user    0m0.059s
sys     0m0.016s

Nuitka, in general, can give a 4x execution speed up, which can result in a ~28ms to ~37ms start up time. Mypyc and Nuitka don't support Python 3.10, but Numba does and so does Cython. I have to look further into PyOxidizer and PyO3.

It would also help to profile it and see exactly what's going on during start up. I think the script might just be hitting the disk heavily with imports that are spread across many files. Dynamically loading dependencies might help in that case, or inlining.

Or, instead of relying on pipes to pass strings through different manipulation commands, strs can use Fire's command chaining feature, or click's command chaining feature. The CLI API might have to change, and the input handling might need to be refactored, in order for command chaining to work nicely with the current code and how it's structured.

That, or I can just port it to Rust.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.