2019-03-31 09:05:09 +00:00
.. _cmd-string:
2018-12-17 01:39:33 +00:00
string - manipulate strings
2019-01-03 04:10:47 +00:00
===========================
2018-12-17 01:39:33 +00:00
2018-12-18 01:58:24 +00:00
Synopsis
--------
2018-12-16 21:08:41 +00:00
2019-09-17 09:59:04 +00:00
::
string collect [(-N | --no-trim-newlines)] [STRING...]
string escape [(-n | --no-quoted)] [--style=xxx] [STRING...]
string join [(-q | --quiet)] SEP [STRING...]
string join0 [(-q | --quiet)] [STRING...]
string length [(-q | --quiet)] [STRING...]
string lower [(-q | --quiet)] [STRING...]
string match [(-a | --all)] [(-e | --entire)] [(-i | --ignore-case)] [(-r | --regex)] [(-n | --index)] [(-q | --quiet)] [(-v | --invert)] PATTERN [STRING...]
2020-09-27 17:12:42 +00:00
string pad [(-r | --right)] [(-c | --char) CHAR] [(-w | --width) INTEGER] [STRING...]
2019-09-17 09:59:04 +00:00
string repeat [(-n | --count) COUNT] [(-m | --max) MAX] [(-N | --no-newline)] [(-q | --quiet)] [STRING...]
string replace [(-a | --all)] [(-f | --filter)] [(-i | --ignore-case)] [(-r | --regex)] [(-q | --quiet)] PATTERN REPLACEMENT [STRING...]
string split [(-m | --max) MAX] [(-n | --no-empty)] [(-q | --quiet)] [(-r | --right)] SEP [STRING...]
string split0 [(-m | --max) MAX] [(-n | --no-empty)] [(-q | --quiet)] [(-r | --right)] [STRING...]
string sub [(-s | --start) START] [(-l | --length) LENGTH] [(-q | --quiet)] [STRING...]
string trim [(-l | --left)] [(-r | --right)] [(-c | --chars CHARS)] [(-q | --quiet)] [STRING...]
string unescape [--style=xxx] [STRING...]
string upper [(-q | --quiet)] [STRING...]
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Description
2019-01-03 04:10:47 +00:00
-----------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` string `` performs operations on strings.
2018-12-16 21:08:41 +00:00
STRING arguments are taken from the command line unless standard input is connected to a pipe or a file, in which case they are read from standard input, one STRING per line. It is an error to supply STRING arguments on the command line and on standard input.
2018-12-19 20:02:45 +00:00
Arguments beginning with `` - `` are normally interpreted as switches; `` -- `` causes the following arguments not to be treated as switches even if they begin with `` - `` . Switches and required arguments are recognized only on the command line.
2018-12-16 21:08:41 +00:00
2020-11-29 11:06:48 +00:00
Most subcommands accept a `` -q `` or `` --quiet `` switch, which suppresses the usual output but exits with the documented status. In this case these commands will quit early, without reading all of the available input.
2018-12-16 21:08:41 +00:00
The following subcommands are available.
2020-06-07 18:00:01 +00:00
.. _cmd-string-collect:
2019-06-16 05:30:31 +00:00
"collect" subcommand
--------------------
2019-10-27 09:56:24 +00:00
.. include :: string-collect.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2019-06-16 05:30:31 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-collect.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2019-06-16 05:30:31 +00:00
2019-10-27 09:56:24 +00:00
Examples
^^^^^^^^
2019-06-16 05:30:31 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-collect.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2019-06-16 05:30:31 +00:00
2019-03-29 19:05:42 +00:00
"escape" and "unescape" subcommands
-----------------------------------
2019-10-27 09:56:24 +00:00
.. include :: string-escape.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-escape.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
Examples
^^^^^^^^
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-escape.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
"join" and "join0" subcommands
------------------------------
2019-03-29 19:05:42 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-join.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-join.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
Examples
^^^^^^^^
2019-03-29 19:05:42 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-join.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
"length" subcommand
2019-01-03 04:10:47 +00:00
-------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-length.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-length.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
Examples
^^^^^^^^
2019-03-29 19:05:42 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-length.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
"lower" subcommand
2019-01-03 04:10:47 +00:00
------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-lower.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2019-03-29 19:05:42 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-lower.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-lower.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
"match" subcommand
------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-match.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-match.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-match.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2020-09-27 17:12:42 +00:00
"pad" subcommand
------------------
.. include :: string-pad.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
.. include :: string-pad.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
.. include :: string-pad.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-19 02:44:30 +00:00
"repeat" subcommand
2019-01-03 04:10:47 +00:00
-------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-repeat.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2019-09-17 08:50:52 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-repeat.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
Examples
^^^^^^^^
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-repeat.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2019-03-29 19:05:42 +00:00
2019-10-27 09:56:24 +00:00
"replace" subcommand
--------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-replace.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-replace.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-replace.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2019-05-19 17:40:48 +00:00
.. _cmd-string-split:
.. _cmd-string-split0:
2019-10-27 09:56:24 +00:00
"split" and "split0" subcommands
--------------------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-split.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2019-09-17 08:50:52 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-split.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
Examples
^^^^^^^^
.. include :: string-split.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
"sub" subcommand
2019-01-03 04:10:47 +00:00
----------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-sub.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
.. include :: string-sub.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
Examples
^^^^^^^^
2019-09-17 08:50:52 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-sub.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
"trim" subcommand
2019-01-03 04:10:47 +00:00
-----------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-trim.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
.. include :: string-trim.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
Examples
^^^^^^^^
2019-09-17 08:50:52 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-trim.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
"upper" subcommand
2019-01-03 04:10:47 +00:00
------------------
2018-12-16 21:08:41 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-upper.rst
:start-after: BEGIN SYNOPSIS
:end-before: END SYNOPSIS
2019-03-29 19:05:42 +00:00
2019-10-27 09:56:24 +00:00
.. include :: string-upper.rst
:start-after: BEGIN DESCRIPTION
:end-before: END DESCRIPTION
.. include :: string-upper.rst
:start-after: BEGIN EXAMPLES
:end-before: END EXAMPLES
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Regular Expressions
2019-01-03 04:10:47 +00:00
-------------------
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
Both the `` match `` and `` replace `` subcommand support regular expressions when used with the `` -r `` or `` --regex `` option. The dialect is that of PCRE2.
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
In general, special characters are special by default, so `` a+ `` matches one or more "a"s, while `` a\+ `` matches an "a" and then a "+". `` (a+) `` matches one or more "a"s in a capturing group (`` (?:XXXX) `` denotes a non-capturing group). For the replacement parameter of `` replace `` , `` $n `` refers to the n-th group of the match. In the match parameter, `` \n `` (e.g. `` \1 `` ) refers back to groups.
2018-12-16 21:08:41 +00:00
Some features include repetitions:
2019-03-29 19:12:28 +00:00
2018-12-19 20:02:45 +00:00
- `` * `` refers to 0 or more repetitions of the previous expression
- `` + `` 1 or more
- `` ? `` 0 or 1.
- `` {n} `` to exactly n (where n is a number)
- `` {n,m} `` at least n, no more than m.
- `` {n,} `` n or more
2018-12-16 21:08:41 +00:00
Character classes, some of the more important:
2019-03-29 19:12:28 +00:00
2018-12-19 20:02:45 +00:00
- `` . `` any character except newline
- `` \d `` a decimal digit and `` \D `` , not a decimal digit
- `` \s `` whitespace and `` \S `` , not whitespace
- `` \w `` a "word" character and `` \W `` , a "non-word" character
- `` [...] `` (where "..." is some characters) is a character set
- `` [^...] `` is the inverse of the given character set
- `` [x-y] `` is the range of characters from x-y
- `` [[:xxx:]] `` is a named character set
- `` [[:^xxx:]] `` is the inverse of a named character set
- `` [[:alnum:]] `` : "alphanumeric"
- `` [[:alpha:]] `` : "alphabetic"
- `` [[:ascii:]] `` : "0-127"
- `` [[:blank:]] `` : "space or tab"
- `` [[:cntrl:]] `` : "control character"
- `` [[:digit:]] `` : "decimal digit"
- `` [[:graph:]] `` : "printing, excluding space"
- `` [[:lower:]] `` : "lower case letter"
- `` [[:print:]] `` : "printing, including space"
- `` [[:punct:]] `` : "printing, excluding alphanumeric"
- `` [[:space:]] `` : "white space"
- `` [[:upper:]] `` : "upper case letter"
- `` [[:word:]] `` : "same as \w"
- `` [[:xdigit:]] `` : "hexadecimal digit"
2018-12-16 21:08:41 +00:00
Groups:
2019-03-29 19:12:28 +00:00
2018-12-19 20:02:45 +00:00
- `` (...) `` is a capturing group
- `` (?:...) `` is a non-capturing group
- `` \n `` is a backreference (where n is the number of the group, starting with 1)
- `` $n `` is a reference from the replacement expression to a group in the match expression.
2018-12-16 21:08:41 +00:00
And some other things:
2019-03-29 19:12:28 +00:00
2018-12-19 20:02:45 +00:00
- `` \b `` denotes a word boundary, `` \B `` is not a word boundary.
- `` ^ `` is the start of the string or line, `` $ `` the end.
- `` | `` is "alternation", i.e. the "or".
2018-12-16 21:08:41 +00:00
2019-04-02 10:46:23 +00:00
Comparison to other tools
-------------------------
Most operations `` string `` supports can also be done by external tools. Some of these include `` grep `` , `` sed `` and `` cut `` .
If you are familiar with these, it is useful to know how `` string `` differs from them.
In contrast to these classics, `` string `` reads input either from stdin or as arguments. `` string `` also does not deal with files, so it requires redirections to be used with them.
2020-10-26 18:19:05 +00:00
In contrast to `` grep `` , `` string `` 's `` match `` defaults to glob-mode, while `` replace `` defaults to literal matching. If set to regex-mode, they use PCRE regular expressions, which is comparable to `` grep `` 's `` -P `` option. `` match `` defaults to printing just the match, which is like `` grep `` with `` -o `` (use `` --entire `` to enable grep-like behavior).
2019-04-02 10:46:23 +00:00
2020-10-26 18:19:05 +00:00
Like `` sed `` 's `` s/old/new/ `` command, `` string replace `` still prints strings that don't match. `` sed `` 's `` -n `` in combination with a `` /p `` modifier or command is like `` string replace -f `` .
2019-04-02 10:46:23 +00:00
2020-08-26 16:29:03 +00:00
`` string split somedelimiter `` is a replacement for `` tr somedelimiter \n `` .