GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing fishshell.com — string - manipulate strings — fish-shell 4.9.3 documentation string - manipulate strings¶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 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 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¶string collect [-a | --allow-empty] [-N | --no-trim-newlines] [STRING ...]
A command like If invoked with multiple arguments instead of input, Any trailing newlines on the input are trimmed, just as with With --allow-empty, Examples¶>_ echo "zero $(echo one\ntwo\nthree) four" zero one two three four >_ echo \"(echo one\ntwo\nthree | string collect)\" "one two three" >_ echo \"(echo one\ntwo\nthree | string collect -N)\" "one two three " >_ echo foo(true | string collect --allow-empty)bar foobar “escape” and “unescape” subcommands¶string escape [-n | --no-quoted] [--style=] [STRING ...] string unescape [--style=] [STRING ...]
--style=script (default) alters the string such that it can be passed back to --style=var ensures the string can be used as a variable name by hex encoding any non-alphanumeric characters. The string is first converted to UTF-8 before being encoded. --style=url ensures the string can be used as a URL by hex encoding any character which is not legal in a URL. The string is first converted to UTF-8 before being encoded. --style=regex escapes an input string for literal matching within a regex expression. The string is first converted to UTF-8 before being encoded.
Examples¶>_ echo \x07 | string escape \cg >_ string escape --style=var 'a1 b2'\u6161 a1_20_b2_E6_85_A1_ “join” and “join0” subcommands¶string join [-q | --quiet] [-n | --no-empty] [--] SEP [STRING ...] string join0 [-q | --quiet] [-n | --no-empty] [--] [STRING ...] Joins its STRING arguments into a single string separated by SEP (for
WARNING:
Insert a
Because Unix uses NUL as the string terminator, passing the output of Examples¶>_ seq 3 | string join ... 1...2...3 # Give a list of NUL-separated filenames to du (this is a GNU extension) >_ string join0 file1 file2 file\nwith\nmultiple\nlines | du --files0-from=- # Just put the strings together without a separator >_ string join '' a b c abc >_ set -l markdown_list '- first' '- second' '- third' # Strings with leading hyphens (also in variable expansions) are interpreted as flag arguments by default. >_ string join \n $markdown_list string join: - first: unknown option # Use '--' to prevent this. >_ string join -- \n $markdown_list - first - second - third “length” subcommand¶string length [-q | --quiet] [-V | --visible] [STRING ...]
With -V or --visible, it uses the visible width of the arguments. That means it will discount escape sequences fish knows about, account for $fish_emoji_width and $fish_ambiguous_width. It will also count each line (separated by Examples¶>_ string length 'hello, world' 12 >_ set str foo >_ string length -q $str; echo $status 0 # Equivalent to test -n "$str" >_ string length --visible (set_color red)foobar # the set_color is discounted, so this is the width of "foobar" 6 >_ string length --visible 🐟🐟🐟🐟 # depending on $fish_emoji_width, this is either 4 or 8 # in new terminals it should be 8 >_ string length --visible abcdef\r123 # this displays as "123def", so the width is 6 6 >_ string length --visible a\nbc # counts "a" and "bc" as separate lines, so it prints width for each 1 2 “lower” subcommand¶string lower [-q | --quiet] [STRING ...]
“match” subcommand¶string match [-a | --all] [-e | --entire] [-i | --ignore-case] [-g | --groups-only] [-r | --regex] [-n | --index] [-q | --quiet] [-v | --invert] [(-m | --max-matches) MAX] PATTERN [STRING ...]
If you specify the -e or --entire then each matching string is printed including any prefix or suffix not matched by the pattern (equivalent to Matching can be made case-insensitive with --ignore-case or -i. If --groups-only or -g is given, only the capturing groups will be reported - meaning the full match will be skipped. This is incompatible with --entire and --invert, and requires --regex. It is useful as a simple cutting tool instead of If --index or -n is given, each match is reported as a 1-based start position and a length. By default, PATTERN is interpreted as a glob pattern matched against each entire STRING argument. A glob pattern is only considered a valid match if it matches the entire STRING. If --regex or -r is given, PATTERN is interpreted as a Perl-compatible regular expression, which does not have to match the entire STRING. For a regular expression containing capturing groups, multiple items will be reported for each match, one for the entire match and one for each capturing group. With this, only the matching part of the STRING will be reported, unless --entire is given. When matching via regular expressions, If --invert or -v is used the selected lines will be only those which do not match the given glob pattern or regular expression. If --max-matches MAX or -m MAX is used, Exit status: 0 if at least one match was found, or 1 otherwise. Match Glob Examples¶>_ string match 'a' a a >_ string match 'a*b' axxb axxb >_ string match -i 'a*B' Axxb Axxb >_ string match -- '-*' -h foo --version bar # To match things that look like options, we need a `--` # to tell string its options end there. -h --version >_ echo 'ok?' | string match '*?' ok? # Note that only the second STRING will match here. >_ string match 'foo' 'foo1' 'foo' 'foo2' foo >_ string match -e 'foo' 'foo1' 'foo' 'foo2' foo1 foo foo2 >_ string match 'foo*' 'foo1' 'foo' 'foo2' foo1 foo2 Match Regex Examples¶>_ string match -r 'cat|dog|fish' 'nice dog' dog >_ string match -r -v "c.*[12]" {cat,dog}(seq 1 4) dog1 dog2 cat3 dog3 cat4 dog4 >_ string match -r -- '-.*' -h foo --version bar # To match things that look like options, we need a `--` # to tell string its options end there. -h --version >_ string match -r '(\d\d?):(\d\d):(\d\d)' 2:34:56 2:34:56 2 34 56 >_ string match -r '^(\w{2,4})\1$' papa mud murmur papa pa murmur mur >_ string match -r -a -n at ratatat 2 2 4 2 6 2 >_ string match -r -i '0x[0-9a-f]{1,8}' 'int magic = 0xBadC0de;' 0xBadC0de >_ echo $version 3.1.2-1575-ga2ff32d90 >_ string match -rq '(?<major>\d+).(?<minor>\d+).(?<revision>\d+)' -- $version >_ echo "You are using fish $major!" You are using fish 3! >_ string match -raq ' *(?<sentence>[^.!?]+)(?<punctuation>[.!?])?' "hello, friend. goodbye" >_ printf "%s\n" -- $sentence hello, friend goodbye >_ printf "%s\n" -- $punctuation . >_ string match -rq '(?<word>hello)' 'hi' >_ count $word 0 “pad” subcommand¶string pad [-r | --right] [-C | --center] [(-c | --char) CHAR] [(-w | --width) INTEGER] [STRING ...]
The escape sequences reflect what fish knows about, and how it computes its output. Your terminal might support more escapes, or not support escape sequences that fish knows about. If -C or --center is given, add the padding to before and after the string. If it is impossible to perfectly center the result (because the required amount of padding is an odd number), extra padding will be added to the left, unless --right is also given. If -r or --right is given, add the padding after a string. If -c or --char is given, pad with CHAR instead of whitespace. The output is padded to the maximum width of all input strings. If -w or --width is given, use at least that. Examples¶>_ string pad -w 10 abc abcdef abc abcdef >_ string pad --right --char=🐟 "fish are pretty" "rich. " fish are pretty rich. 🐟🐟🐟🐟 >_ string pad -w$COLUMNS (date) # Prints the current time on the right edge of the screen. See also¶
“shorten” subcommand¶string shorten [(-c | --char) CHARS] [(-m | --max) INTEGER] [-N | --no-newline] [-l | --left] [-q | --quiet] [STRING ...]
The escape sequences reflect what fish knows about, and how it computes its output. Your terminal might support more escapes, or not support escape sequences that fish knows about. If -m or --max is given, truncate at the given width. Otherwise, the lowest non-zero width of all input strings is used. A max of 0 means no shortening takes place, all STRINGs are printed as-is. If -N or --no-newline is given, only the first line (or last line with --left) of each STRING is used, and an ellipsis is added if it was multiline. This only works for STRINGs being given as arguments, multiple lines given on stdin will be interpreted as separate STRINGs instead. If -c or --char is given, add CHAR instead of an ellipsis. This can also be empty or more than one character. If -l or --left is given, remove text from the left on instead, so this prints the longest suffix of the string that fits. With --no-newline, this will take from the last line instead of the first. If -q or --quiet is given, The default ellipsis is The return value is 0 if any shortening occurred, 1 otherwise. Examples¶>_ string shorten foo foobar # No width was given, we infer, and "foo" is the shortest. foo fo… >_ string shorten --char="..." foo foobar # The target width is 3 because of "foo", # and our ellipsis is 3 too, so we can't really show anything. # This is the default ellipsis if your locale doesn't allow "…". foo ... >_ string shorten --char="" --max 4 abcdef 123456 # Leaving the char empty makes us not add an ellipsis # So this truncates at 4 columns: abcd 1234 >_ touch "a multiline"\n"file" >_ for file in *; string shorten -N -- $file; end # Shorten the multiline file so we only show one line per file: a multiline… >_ ss -p | string shorten -m$COLUMNS -c "" # `ss` from Linux' iproute2 shows socket information, but prints extremely long lines. # This shortens input so it fits on the screen without overflowing lines. >_ git branch | string match -rg '^\* (.*)' | string shorten -m20 # Take the current git branch and shorten it at 20 columns. # Here the branch is "builtin-path-with-expand" builtin-path-with-e… >_ git branch | string match -rg '^\* (.*)' | string shorten -m20 --left # Taking 20 columns from the right instead: …in-path-with-expand See also¶
“repeat” subcommand¶string repeat [(-n | --count) COUNT] [(-m | --max) MAX] [-N | --no-newline] [-q | --quiet] [STRING ...] string repeat [-N | --no-newline] [-q | --quiet] COUNT [STRING ...]
Examples¶Repeat Examples¶>_ string repeat -n 2 'foo ' foo foo >_ echo foo | string repeat -n 2 foofoo >_ string repeat -n 2 -m 5 'foo' foofo >_ string repeat -m 5 'foo' foofo >_ string repeat 2 'foo' foofoo >_ string repeat 2 -n 3 222 “replace” subcommand¶string replace [-a | --all] [-f | --filter] [-i | --ignore-case] [-r | --regex] [(-m | --max-matches) MAX] [-q | --quiet] PATTERN REPLACEMENT [STRING ...]
If -r or --regex is given, PATTERN is interpreted as a Perl-compatible regular expression, and REPLACEMENT can contain C-style escape sequences like t as well as references to capturing groups by number or name as $n or ${n}. If you specify the -f or --filter flag then each input string is printed only if a replacement was done. This is useful where you would otherwise use this idiom: If --max-matches MAX or -m MAX is used, Exit status: 0 if at least one replacement was performed, or 1 otherwise. Replace Literal Examples¶>_ string replace is was 'blue is my favorite' blue was my favorite >_ string replace 3rd last 1st 2nd 3rd 1st 2nd last >_ string replace -a ' ' _ 'spaces to underscores' spaces_to_underscores Replace Regex Examples¶>_ string replace -r -a '[^\d.]+' ' ' '0 one two 3.14 four 5x' 0 3.14 5 >_ string replace -r '(\w+)\s+(\w+)' '$2 $1 $$' 'left right' right left $ >_ string replace -r '\s*newline\s*' '\n' 'put a newline here' put a here “split” and “split0” subcommands¶string split [(-f | --fields) FIELDS [-a | --allow-empty]] [(-m | --max) MAX] [-n | --no-empty] [-q | --quiet] [-r | --right] SEP [STRING ...] string split0 [(-f | --fields) FIELDS [-a | --allow-empty]] [(-m | --max) MAX] [-n | --no-empty] [-q | --quiet] [-r | --right] [STRING ...]
Use -f or --fields to print out specific fields. FIELDS is a comma-separated string of field numbers and/or spans. Each field is one-indexed, and will be printed on separate lines. If a given field does not exist, then the command exits with status 1 and does not print anything, unless --allow-empty is used. See also the --delimiter option of the read command.
Be aware that commandline arguments cannot include NULs, so you likely want to pass to Examples¶>_ string split . example.com example com >_ string split -r -m1 / /usr/local/bin/fish /usr/local/bin fish >_ string split '' abc a b c >_ string split --allow-empty -f1,3-4,5 '' abcd a c d NUL Delimited Examples¶>_ # Count files in a directory, without being confused by newlines. >_ # Note: Don't use `string split0 (find . -print0)`, because arguments cannot include NUL. >_ count (find . -print0 | string split0) 42 >_ # Sort a list of elements which may contain newlines >_ set foo beta alpha\ngamma >_ set foo (string join0 $foo | sort -z | string split0) >_ string escape $foo[1] alpha\ngamma “sub” subcommand¶string sub [(-s | --start) START] [(-e | --end) END] [(-l | --length) LENGTH] [-q | --quiet] [STRING ...]
Examples¶>_ string sub --length 2 abcde ab >_ string sub -s 2 -l 2 abcde bc >_ string sub --start=-2 abcde de >_ string sub --end=3 abcde abc >_ string sub -e -1 abcde abcd >_ string sub -s 2 -e -1 abcde bcd >_ string sub -s -3 -e -2 abcde c “trim” subcommand¶string trim [-l | --left] [-r | --right] [(-c | --chars) CHARS] [-q | --quiet] [STRING ...]
The -c or --chars switch causes the set of characters in CHARS to be removed instead of whitespace. This is a set of characters, not a string - if you pass Exit status: 0 if at least one character was trimmed, or 1 otherwise. Examples¶>_ string trim ' abc ' abc >_ string trim --right --chars=yz xyzzy zany x zan “upper” subcommand¶string upper [-q | --quiet] [STRING ...]
Regular Expressions¶Both the In general, special characters are special by default, so Some features include repetitions:
Character classes, some of the more important:
Groups:
And some other things:
Comparison to other tools¶Most operations If you are familiar with these, it is useful to know how In contrast to these classics, In contrast to Like
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |