PSED(1)

πŸ“¦
version 1.003 Β· 2 versions
, in fedora 43

πŸ’… PSED(1) β€” User Contributed Perl Documentation πŸ’…

okay bestie, buckle UP, because this is psed β€” the stream editor β€” and she is literally that girl. this is the Perl-flavoured one, sitting in the User Contributed Perl Documentation section, and she's about to eat. ✨


πŸŽ€ NAME πŸŽ€

psed β€” a stream editor. yes, that kind of stream editor. iconic behaviour, no notes. πŸ¦‹


✨ SYNOPSIS ✨

psed [-an] script [file ...]
psed [-an] [-e script] [-f script-file] [file ...]

s2p  [-an] [-e script] [-f script-file]

look at her. four whole ways to show up. πŸ’…


🌸 DESCRIPTION 🌸

So a stream editor, right? She reads the input stream β€” which is made of the files you listed (or standard input, if you didn't list any) β€” and she processes it line by line by applying a script made of edit commands, then writes the resulting lines to standard output. And yes, the filename "-" is allowed, and it means "read from standard input", periodt.

The edit script is composed from arguments of -e options and script-files, in the given order. Also, a single script argument may be specified as the first parameter. Slay.

And here's a fun little plot twist: if this program gets invoked with the name s2p, she acts as a sed-to-Perl translator. see "SED SCRIPT TRANSLATION" for the tea. πŸ«–

sed returns an exit code of 0 on success or >0 if an error occurred. simple as. 😌


πŸ‘‘ OPTIONS πŸ‘‘

  • -a β€” So by default, a file you name as the argument to the w edit command gets opened before input processing even starts. With -a, opening those files is delayed until the first line is actually written to the file. patient queen. 🧚
  • -e script β€” The editing commands defined by script get appended to the script. Multiple commands must be separated by newlines or semicolons. pick your fighter.
  • -f script-file β€” Editing commands from the specified script-file are read and appended to the script. πŸ“œ
  • -n β€” By default, a line is written to standard output after the editing script has been applied to it. The -n option suppresses automatic printing. she's quiet. she's mysterious. πŸ™ˆ

πŸ’– COMMANDS πŸ’–

The sed command syntax is defined as

[address[,address]][!]function[argument]

with whitespace being permitted before or after addresses, and between the function character and the argument. The addresses and the address inverter (!) are used to restrict the application of a command to the selected line(s) of input.

Each command must be on a line of its own, except where noted in the synopses below.

The edit cycle performed on each input line consists of reading the line (without its trailing newline character) into the pattern space, applying the applicable commands of the edit script, then writing the final contents of the pattern space and a newline to standard output. A hold space is also provided for saving the contents of the pattern space for later use. she's got storage, bestie. πŸ’Ύβœ¨

πŸ¦‹ Addresses πŸ¦‹

A sed address is either a line number or a pattern, and those can be combined arbitrarily to construct ranges. Lines are numbered across all input files β€” so it's one big long count, not a fresh start per file. iykyk.

Any address may be followed by an exclamation mark (!), which selects all lines not matching that address. the audacity. love her. πŸ’…

  • number β€” The line with the given number is selected. that's it. that's the whole thing.
  • $ β€” A dollar sign ($) is the line number of the last line of the input stream. the final boss. πŸ‘‘
  • /regular expression/ β€” A pattern address is a basic regular expression (peep "BASIC REGULAR EXPRESSIONS"), sitting between the delimiting character /. Any other character except \ or newline may be used to delimit a pattern address β€” but only when the initial delimiter is prefixed with a backslash (\). quirky but valid. ✨

If no address is given, the command selects every line. all of them. every single one. no cap.

If one address is given, it selects the line (or lines) matching the address.

Two addresses select a range that begins whenever the first address matches, and ends (including that line) when the second address matches. And here's the fine print, bestie, listen up:

  • If the first (second) address is a matching pattern, the second address is not applied to the very same line to determine the end of the range.
  • Likewise, if the second address is a matching pattern, the first address is not applied to the very same line to determine the begin of another range.
  • If both addresses are line numbers, and the second line number is less than the first line number, then only the first line is selected. yeah. that's the rule. don't fight it.

🌈 Functions 🌈

The maximum permitted number of addresses is indicated with each function synopsis below. read the brackets, girlies, they matter. πŸ‘€

The argument text consists of one or more lines following the command. Embedded newlines in text must be preceded with a backslash. Other backslashes in text are deleted and the following character is taken literally. careful with those. 🫣


  • [1addr]a\ text β€” Write text (which must start on the line following the command) to standard output immediately before reading the next line of input β€” either by executing the N function or by beginning a new cycle. she's got impeccable timing. ⏰
  • [2addr]b [label] β€” Branch to the : function with the specified label. If no label is given, branch to the end of the script. bye bestie. πŸ‘‹
  • [2addr]c\ text β€” The line, or range of lines, selected by the address is deleted. The text (which must start on the line following the command) is written to standard output. With an address range, this occurs at the end of the range. so she swaps out the whole block for your text. cute.
  • [2addr]d β€” Deletes the pattern space and starts the next cycle. gone. deleted. πŸ—‘οΈ
  • [2addr]D β€” Deletes the pattern space through the first embedded newline or to the end. If the pattern space becomes empty, a new cycle is started; otherwise, execution of the script is restarted. spicy.
  • [2addr]g β€” Replace the contents of the pattern space with the hold space. copy-paste energy. πŸ“‹
  • [2addr]G β€” Append a newline and the contents of the hold space to the pattern space.
  • [2addr]h β€” Replace the contents of the hold space with the pattern space.
  • [2addr]H β€” Append a newline and the contents of the pattern space to the hold space.
  • [1addr]i\ text β€” Write the text (which must start on the line following the command) to standard output. just vibes, no deleting. 🌸
  • [2addr]l β€” Print the contents of the pattern space: non-printable characters are shown in C-style escaped form; long lines are split and have a trailing \ at the point of the split; the true end of a line is marked with a $. The escapes are: \a, \t, \n, \f, \r, \e for BEL, HT, LF, FF, CR, ESC, respectively, and \ followed by a three-digit octal number for all other non-printable characters. forensic scientist behaviour, honestly. πŸ”
  • [2addr]n β€” If automatic printing is enabled, write the pattern space to standard output. Replace the pattern space with the next line of input. If there is no more input, processing is terminated. πŸ’€
  • [2addr]N β€” Append a newline and the next line of input to the pattern space. If there is no more input, processing is terminated. same energy, different letter.
  • [2addr]p β€” Print the pattern space to standard output. (Use the -n option to suppress automatic printing at the end of a cycle if you want to avoid double printing of lines.) two prints is not a serve, bestie. πŸ™…
  • [2addr]P β€” Prints the pattern space through the first embedded newline or to the end.
  • [1addr]q β€” Branch to the end of the script and quit without starting a new cycle. she's leaving. let her. πŸšͺ
  • [1addr]r file β€” Copy the contents of file to standard output immediately before the next attempt to read a line of input. Any error encountered while reading file is silently ignored. no drama, no error message, we move on.
  • [2addr]s/regular expression/replacement/flags β€” Substitute the replacement string for the first substring in the pattern space that matches the regular expression. Any character other than backslash or newline can be used instead of a slash to delimit the regular expression and the replacement. To use the delimiter as a literal character within the regular expression and the replacement, precede the character by a backslash (\).

Literal newlines may be embedded in the replacement string by preceding a newline with a backslash.

Within the replacement, an ampersand (&) is replaced by the string matching the regular expression. The strings \1 through \9 are replaced by the corresponding subpattern (see "BASIC REGULAR EXPRESSIONS"). To get a literal & or \ in the replacement text, precede it by a backslash.

The following flags modify the behaviour of the s command:

  • g β€” The replacement is performed for all matching, non-overlapping substrings of the pattern space. go big or go home. πŸ”₯
  • 1..9 β€” Replace only the n-th matching substring of the pattern space. picky, but make it fashion.
  • p β€” If the substitution was made, print the new value of the pattern space. πŸ’–
  • w file β€” If the substitution was made, write the new value of the pattern space to the specified file. πŸ“
  • [2addr]t [label] β€” Branch to the : function with the specified label if any s substitutions have been made since the most recent reading of an input line or execution of a t function. If no label is given, branch to the end of the script. she keeps receipts on whether a swap happened. 🧾
  • [2addr]w file β€” The contents of the pattern space are written to the file.
  • [2addr]x β€” Swap the contents of the pattern space and the hold space. trading places. ✨
  • [2addr]y/string1/string2/ β€” In the pattern space, replace all characters occurring in string1 by the character at the corresponding position in string2. It is possible to use any character (other than a backslash or newline) instead of a slash to delimit the strings. Within string1 and string2, a backslash followed by any character other than a newline is that literal character, and a backslash followed by an n is replaced by a newline character. transliteration station, all aboard. πŸš‚
  • [1addr]= β€” Prints the current line number on standard output. shoutout to the count. πŸ”’
  • [0addr]: [label] β€” The command specifies the position of the label. It has no other effect. she's just a landmark, bestie.
  • [2addr]{ [command] and [0addr]} β€” These two commands begin and end a command list. The first command may be given on the same line as the opening { command. The commands within the list are jointly selected by the address(es) given on the { command (but may still have individual addresses). group project, but everyone's actually contributing. πŸ’…
  • [0addr]# [comment] β€” The entire line is ignored (treated as a comment). If, however, the first two characters in the script are #n, automatic printing of output is suppressed, as if the -n option were given on the command line. sneaky little shortcut. 🀫

✨ BASIC REGULAR EXPRESSIONS ✨

okay so a Basic Regular Expression (BRE) β€” that's the POSIX 1003.2 flavour, bestie, the OGs β€” is made of atoms, which match parts of a string, and bounds, which say how many times to repeat the atom right before them. that's it. that's the whole vibe. πŸ’–


πŸ¦‹ Atoms

the possible atoms of a BRE are, in order of me being obsessed with them:

  • . β€” matches any single character. the wildcard. the main character. πŸ‘‘
  • ^ and $ β€” match the null string at the beginning or the end of a string, respectively.
  • a bracket expression, wrapped up in [ and ] (more on that below, keep reading).
  • any single character with no other significance β€” it just matches that character, periodt.
  • a \ before one of ., ^, $, [, *, \ β€” matches the character after the backslash. so the backslash is like "no babe, this one's literal".
  • a sequence of atoms enclosed in \( and \) becomes an atom, and it establishes the target for a backreference β€” that's the substring that actually matched the stuff inside. 🫢
  • and finally, \ followed by one of the digits 0 through 9 is a backreference. yes, literally just that.

tiny detail that matters: a ^ that is not first, or a $ that is not last, doesn't have any special significance β€” and you don't need to backslash it to make it literal. same deal for a ] that doesn't terminate a bracket expression. it's just a character living its life. πŸ™ˆ

warning bestie: an unescaped backslash cannot be last in a BRE. it just can't. don't try it.

🌸 Bounds

the BRE bounds are:

  • * β€” specifies 0 or more matches of the preceding atom
  • \{count\} β€” specifies that many repetitions
  • \{minimum,\} β€” gives a lower limit
  • \{minimum,maximum\} β€” defines a lower and upper bound, the full package deal πŸ’…

fun little fact: a bound appearing as the first item in a BRE is taken literally. it's just a character there. no repetition energy. ✨


πŸŽ€ Bracket Expressions

a bracket expression is a list of characters, character ranges, and character classes, all enclosed in [ and ], and it matches any single character from that whole represented set. it's giving "pick a card, any card". 🌈

a character range is written as two characters separated by -, and it represents all characters (according to the character collating sequence) that are not less than the first and not greater than the second.

heads up: ranges are very collating-sequence-dependent, and portable programs should avoid relying on them. fr fr, don't build your whole personality on a range.

a character class is one of the class names

alnum     digit     punct
alpha     graph     space
blank     lower     upper
cntrl     print     xdigit

enclosed in [: and :], and it represents the set of characters as defined in ctype(3). slay. 🧚

if the first character after [ is ^, the sense of matching is inverted. uno reverse card. πŸƒ

now, the escape-the-specials cheat sheet:

  • to include a literal "^", place it anywhere else but first.
  • to include a literal "]", place it first, or immediately after an initial ^.
  • to include a literal "-", make it the first character (or the second one after ^), or the last character, or the second endpoint of a range.

the special bracket expression constructs "[[:<:]]" and "[[:>:]]" match the null string at the beginning and end of a word respectively. (note that neither is identical to Perl's '\b' atom β€” they're cousins, not twins, iykyk.) πŸ’œ


⭐ Additional Atoms

since some sed implementations hand out extra regular expression atoms (stuff that's not defined in POSIX 1003.2), psed is capable of translating the following backslash escapes:

  • \< β€” this is the same as "[[:>:]]".
  • \> β€” this is the same as "[[:<:]]".
  • \w β€” this is an abbreviation for "[[:alnum:]_]".
  • \W β€” this is an abbreviation for "[^[:alnum:]_]".
  • \y β€” match the empty string at a word boundary.
  • \B β€” match the empty string between any two either word or non-word characters.

to enable this feature, the environment variable PSEDEXTBRE must be set to a string containing the requested characters, e.g.:

"PSEDEXTBRE='<>wW'"

🌿 ENVIRONMENT

the environment variable "PSEDEXTBRE" may be set to extend BREs. see "Additional Atoms" above. that's the whole section, she's short but she's doing her best. πŸ’š


πŸ’€ DIAGNOSTICS

here's every way psed will yell at you, and exactly what it means. buckle up, bestie, it's a long list but every single one of these is important:

  • ambiguous translation for character '%s' in 'y' command β€” the indicated character appears twice, with different translations.
  • '[' cannot be last in pattern β€” a '[' in a BRE indicates the beginning of a bracket expression.
  • '\' cannot be last in pattern β€” a '\' in a BRE is used to make the subsequent character literal.
  • '\' cannot be last in substitution β€” a '\' in a substitution string is used to make the subsequent character literal.
  • conflicting flags '%s' β€” in an s command, either the 'g' flag and an n-th occurrence flag, or multiple n-th occurrence flags are specified. note that only the digits ^'1' through '9' are permitted.
  • duplicate label %s (first defined at %s)
  • excess address(es) β€” the command has more than the permitted number of addresses.
  • extra characters after command (%s)
  • illegal option '%s'
  • improper delimiter in s command β€” the BRE and substitution may not be delimited with '\' or newline.
  • invalid address after ','
  • invalid backreference (%s) β€” the specified backreference number exceeds the number of backreferences in the BRE.
  • invalid repeat clause '\{%s\}' β€” the repeat clause does not contain a valid integer value, or pair of values.
  • malformed regex, 1st address
  • malformed regex, 2nd address
  • malformed regular expression
  • malformed substitution expression
  • malformed 'y' command argument β€” the first or second string of a y command is syntactically incorrect.
  • maximum less than minimum in '\{%s\}'
  • no script command given β€” there must be at least one -e or one -f option specifying a script or script file.
  • '\' not valid as delimiter in 'y' command
  • option -e requires an argument
  • option -f requires an argument
  • 's' command requires argument
  • start of unterminated '{'
  • string lengths in 'y' command differ β€” the translation table strings in a y command must have equal lengths.
  • undefined label '%s'
  • unexpected '}' β€” a } command without a preceding { command was encountered.
  • unexpected end of script β€” the end of the script was reached although a text line after a a, c or i command indicated another line.
  • unknown command '%s'
  • unterminated '[' β€” a BRE contains an unterminated bracket expression.
  • unterminated '\(' β€” a BRE contains an unterminated backreference.
  • '\{' without closing '\}' β€” a BRE contains an unterminated bounds specification.
  • '\)' without preceding '\('
  • 'y' command requires argument

πŸ”₯ EXAMPLE

the basic material for the preceding section was generated by running the sed script

#no autoprint
s/^.*Warn( *"\([^"]*\)".*$/\1/
t process
b
:process
s/$!/%s/g
s/$[_[:alnum:]]\{1,\}/%s/g
s/\\\\/\\/g
s/^/=item /
p

on the program's own text, and piping the output into "sort -u". iconic behaviour honestly. πŸ‘‘


🌈 SED SCRIPT TRANSLATION

if this program is invoked with the name s2p it will act as a sed-to-Perl translator. after option processing (all other arguments are ignored), a Perl program is printed on standard output, which will process the input stream (as read from all arguments) in the way defined by the sed script and the option setting used for the translation. glow up achieved. ✨


πŸ’œ SEE ALSO

perl(1), re_format(7)


πŸ› BUGS

okay so here's where we keep it real:

  • the l command will show escape characters (ESC) as '"\e"', but a vertical tab (VT) in octal.
  • trailing spaces are truncated from labels in :, t and b commands.
  • the meaning of an empty regular expression ('"//"'), as defined by sed, is "the last pattern used, at run time". this deviates from the Perl interpretation, which will re-use the "last successfully executed regular expression". since keeping track of pattern usage would create terribly cluttered code, and differences would only appear in obscure context (where other sed implementations appear to deviate, too), the Perl semantics was adopted. note that common usage of this feature, such as in "/abc/s//xyz/", will work as expected.
  • collating elements (of bracket expressions in BREs) are not implemented.

⭐ STANDARDS

this sed implementation conforms to the IEEE Std1003.2-1992 ("POSIX.2") definition of sed, and is compatible with the OpenBSD implementation, except where otherwise noted (see "BUGS"). we're standards-compliant AND we're cute. πŸ’…


πŸ‘‘ AUTHOR

this Perl implementation of sed was written by Wolfgang Laun, Wolfgang.Laun@alcatel.at. absolute legend, we love him. 🫢


this program is free and open software. you may use, modify, distribute, and sell this program (and any modified variants) in any way you wish, provided you do not restrict others from doing the same. share the love, bestie. πŸ’–


and that's the whole page!! this one's from perl v5.42.0, dated 2025-07-25, and you're reading PSED(1). go forth and sed responsibly, babe πŸŒΈπŸ¦‹βœ¨

πŸ“¦ where this page comes from