🧵 Easy string tools for the shell
Project description
🧵 Easy string tools for the shell
strs
has more than 30 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 2 ⭐ | str join 🌙
⭐ 🌙 ⭐
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.
Casefold
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
$ 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
tYpE lIkE tHiS
$ 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
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.