File: string.rst

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (372 lines) | stat: -rw-r--r-- 11,116 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
string - manipulate strings
===========================

Synopsis
--------

.. synopsis::

    string collect [-a | --allow-empty] [-N | --no-trim-newlines] [STRING ...]
    string escape [-n | --no-quoted] [--style=] [STRING ...]
    string join [-q | --quiet] [-n | --no-empty] SEP [STRING ...]
    string join0 [-q | --quiet] [-n | --no-empty] [STRING ...]
    string length [-q | --quiet] [STRING ...]
    string lower [-q | --quiet] [STRING ...]
    string match [-a | --all] [-e | --entire] [-i | --ignore-case]
                 [-g | --groups-only] [-r | --regex] [-n | --index]
                 [-q | --quiet] [-v | --invert]
                 PATTERN [STRING ...]
    string pad [-r | --right] [-C | --center] [(-c | --char) CHAR] [(-w | --width) INTEGER]
               [STRING ...]
    string repeat [(-n | --count) COUNT] [(-m | --max) MAX] [-N | --no-newline]
                  [-q | --quiet] [STRING ...]
    string repeat [-N | --no-newline] [-q | --quiet] COUNT [STRING ...]
    string replace [-a | --all] [-f | --filter] [-i | --ignore-case]
                   [-r | --regex] [-q | --quiet] PATTERN REPLACE [STRING ...]
    string shorten [(-c | --char) CHARS] [(-m | --max) INTEGER]
                   [-N | --no-newline] [-l | --left] [-q | --quiet] [STRING ...]
    string split [(-f | --fields) FIELDS] [(-m | --max) MAX] [-n | --no-empty]
                 [-q | --quiet] [-r | --right] SEP [STRING ...]
    string split0 [(-f | --fields) FIELDS] [(-m | --max) MAX] [-n | --no-empty]
                  [-q | --quiet] [-r | --right] [STRING ...]
    string sub [(-s | --start) START] [(-e | --end) END] [(-l | --length) LENGTH]
               [-q | --quiet] [STRING ...]
    string trim [-l | --left] [-r | --right] [(-c | --chars) CHARS]
                [-q | --quiet] [STRING ...]
    string unescape [--style=] [STRING ...]
    string upper [-q | --quiet] [STRING ...]

Description
-----------

``string`` performs operations on strings.

*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.

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.

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.

The following subcommands are available.

"collect" subcommand
--------------------

.. include:: string-collect.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-collect.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-collect.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"escape" and "unescape" subcommands
-----------------------------------

.. include:: string-escape.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-escape.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-escape.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"join" and "join0" subcommands
------------------------------

.. include:: string-join.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-join.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-join.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"length" subcommand
-------------------

.. include:: string-length.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-length.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-length.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"lower" subcommand
------------------

.. include:: string-lower.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-lower.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

.. include:: string-lower.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"match" subcommand
------------------

.. include:: string-match.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-match.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

.. include:: string-match.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"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

Examples
^^^^^^^^

.. include:: string-pad.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

See also
^^^^^^^^

.. include:: string-pad.rst
   :start-after: BEGIN SEEALSO

"shorten" subcommand
--------------------

.. include:: string-shorten.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-shorten.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-shorten.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

See also
^^^^^^^^

.. include:: string-shorten.rst
   :start-after: BEGIN SEEALSO

"repeat" subcommand
-------------------

.. include:: string-repeat.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-repeat.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-repeat.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"replace" subcommand
--------------------

.. include:: string-replace.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-replace.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

.. include:: string-replace.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"split" and "split0" subcommands
--------------------------------

.. include:: string-split.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-split.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

Examples
^^^^^^^^

.. include:: string-split.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"sub" subcommand
----------------

.. 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
^^^^^^^^

.. include:: string-sub.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"trim" subcommand
-----------------

.. 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
^^^^^^^^

.. include:: string-trim.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

"upper" subcommand
------------------

.. include:: string-upper.rst
   :start-after: BEGIN SYNOPSIS
   :end-before: END SYNOPSIS

.. include:: string-upper.rst
   :start-after: BEGIN DESCRIPTION
   :end-before: END DESCRIPTION

.. include:: string-upper.rst
   :start-after: BEGIN EXAMPLES
   :end-before: END EXAMPLES

Regular Expressions
-------------------

Both the ``match`` and ``replace`` subcommand support regular expressions when used with the **-r** or **--regex** option. The dialect is that of PCRE2.

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.

Some features include repetitions:

- ``*`` 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

Character classes, some of the more important:

- ``.`` 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"

Groups:

- ``(...)`` 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.

And some other things:

- ``\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".

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.

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).

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``.

``string split somedelimiter`` is a replacement for ``tr somedelimiter \n``.