Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Program that diffs an extracte ...Print or Check SHA Checksums (Berikutnya)
Utilities

A stream editor

Daftar Isi

NAME

psed - a stream editor

SYNOPSIS

  1. psed [-an] script [file ...]
  2. psed [-an] [-e script] [-f script-file] [file ...]
  3. s2p [-an] [-e script] [-f script-file]

DESCRIPTION

A stream editor reads the input stream consisting of the specified files(or standard input, if none are given), processes is line by line byapplying a script consisting of edit commands, and writes resulting linesto standard output. The filename '-' may be used to read standard input.

The edit script is composed from arguments of -e options andscript-files, in the given order. A single script argument may be specifiedas the first parameter.

If this program is invoked with the name s2p, it will act as ased-to-Perl translator. See SED SCRIPT TRANSLATION.

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

OPTIONS

  • -a

    A file specified as argument to the w edit command is by defaultopened before input processing starts. Using -a, opening of suchfiles is delayed until the first line is actually written to the file.

  • -e script

    The editing commands defined by script are appended to the script.Multiple commands must be separated by newlines.

  • -f script-file

    Editing commands from the specified script-file are read and appendedto the script.

  • -n

    By default, a line is written to standard output after the editing scripthas been applied to it. The -n option suppresses automatic printing.

COMMANDS

sed command syntax is defined as

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

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

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

The edit cycle performed on each input line consist of reading the line(without its trailing newline character) into the pattern space,applying the applicable commands of the edit script, writing the finalcontents of the pattern space and a newline to the standard output.A hold space is provided for saving the contents of thepattern space for later use.

Addresses

A sed address is either a line number or a pattern, which may be combinedarbitrarily to construct ranges. Lines are numbered across all input files.

Any address may be followed by an exclamation mark ('!'), selectingall lines not matching that address.

  • number

    The line with the given number is selected.

  • $

    A dollar sign ($) is the line number of the last line of the input stream.

  • /regular expression/

    A pattern address is a basic regular expression (see BASIC REGULAR EXPRESSIONS), between the delimiting character /.Any other character except \ or newline may be used to delimit apattern address when the initial delimiter is prefixed with abackslash ('\').

If no address is given, the command selects every line.

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

Two addresses select a range that begins whenever the first addressmatches, and ends (including that line) when the second address matches.If the first (second) address is a matching pattern, the second address is not applied to the very same line to determine the end ofthe range. Likewise, if the second address is a matching pattern, thefirst address is not applied to the very same line to determine thebegin of another range. If both addresses are line numbers,and the second line number is less than the first line number, thenonly the first line is selected.

Functions

The maximum permitted number of addresses is indicated with eachfunction synopsis below.

The argument text consists of one or more lines following the command.Embedded newlines in text must be preceded with a backslash. Otherbackslashes in text are deleted and the following character is takenliterally.

  • [1addr]a\ text

    Write text (which must start on the line following the command)to standard output immediately before reading the next lineof input, either by executing the N function or by beginning a new cycle.

  • [2addr]b [label]

    Branch to the : function with the specified label. If no labelis given, branch to the end of the script.

  • [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 atthe end of the range.

  • [2addr]d

    Deletes the pattern space and starts the next cycle.

  • [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, otherwiseexecution of the script is restarted.

  • [2addr]g

    Replace the contents of the pattern space with the hold space.

  • [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.

  • [2addr]l

    Print the contents of the pattern space: non-printable characters areshown 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 witha '$'. Escapes are: '\a', '\t', '\n', '\f', '\r', '\e' forBEL, HT, LF, FF, CR, ESC, respectively, and '\' followed by a three-digitoctal number for all other non-printable characters.

  • [2addr]n

    If automatic printing is enabled, write the pattern space to the standardoutput. Replace the pattern space with the next line of input. Ifthere is no more input, processing is terminated.

  • [2addr]N

    Append a newline and the next line of input to the pattern space. Ifthere is no more input, processing is terminated.

  • [2addr]p

    Print the pattern space to the standard output. (Use the -n optionto suppress automatic printing at the end of a cycle if you want toavoid double printing of lines.)

  • [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.

  • [1addr]r file

    Copy the contents of the file to standard output immediately beforethe next attempt to read a line of input. Any error encountered whilereading file is silently ignored.

  • [2addr]s/regular expression/replacement/flags

    Substitute the replacement string for the first substring inthe 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 expressionand the replacement, precede the character by a backslash ('\').

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

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

    The following flags modify the behaviour of the s command:

    • g

      The replacement is performed for all matching, non-overlapping substringsof the pattern space.

    • 1..9

      Replace only the n-th matching substring of the pattern space.

    • 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 spaceto the specified file.

  • [2addr]t [label]

    Branch to the : function with the specified label if any ssubstitutions have been made since the most recent reading of an input lineor execution of a t function. If no label is given, branch to the end ofthe script.

  • [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.

  • [2addr]y/string1/string2/

    In the pattern space, replace all characters occurring in string1 by thecharacter at the corresponding position in string2. It is possibleto use any character (other than a backslash or newline) instead of aslash to delimit the strings. Within string1 and string2, abackslash followed by any character other than a newline is that literalcharacter, and a backslash followed by an 'n' is replaced by a newlinecharacter.

  • [1addr]=

    Prints the current line number on the standard output.

  • [0addr]: [label]

    The command specifies the position of the label. It has no other effect.

  • [2addr]{ [command]
  • [0addr]}

    These two commands begin and end a command list. The first command maybe given on the same line as the opening { command. The commandswithin the list are jointly selected by the address(es) given on the{ command (but may still have individual addresses).

  • [0addr]# [comment]

    The entire line is ignored (treated as a comment). If, however, the firsttwo characters in the script are '#n', automatic printing of output issuppressed, as if the -n option were given on the command line.

BASIC REGULAR EXPRESSIONS

A Basic Regular Expression (BRE), as defined in POSIX 1003.2, consistsof atoms, for matching parts of a string, and bounds, specifyingrepetitions of a preceding atom.

Atoms

The possible atoms of a BRE are: ., matching any single character;^ and $, matching the null string at the beginning or endof a string, respectively; a bracket expressions, enclosedin [ and ] (see below); and any single character with noother significance (matching that character). A \ before oneof: ., ^, $, [, *, \, matching the characterafter the backslash. A sequence of atoms enclosed in \( and \)becomes an atom and establishes the target for a backreference,consisting of the substring that actually matches the enclosed atoms.Finally, \ followed by one of the digits 0 through 9 is abackreference.

A ^ that is not first, or a $ that is not last does not havea special significance and need not be preceded by a backslash tobecome literal. The same is true for a ], that does not terminatea bracket expression.

An unescaped backslash cannot be last in a BRE.

Bounds

The BRE bounds are: *, specifying 0 or more matches of the precedingatom; \{count\}, specifying that many repetitions;\{minimum,\}, giving a lower limit; and\{minimum,maximum\} finally defines a lower and upperbound.

A bound appearing as the first item in a BRE is taken literally.

Bracket Expressions

A bracket expression is a list of characters, character rangesand character classes enclosed in [ and ] and matches anysingle character from the represented set of characters.

A character range is written as two characters separated by - andrepresents all characters (according to the character collating sequence)that are not less than the first and not greater than the second.(Ranges are very collating-sequence-dependent, and portable programsshould avoid relying on them.)

A character class is one of the class names

  1. alnum digit punct
  2. alpha graph space
  3. blank lower upper
  4. cntrl print xdigit

enclosed in [: and :] and represents the set of charactersas defined in ctype(3).

If the first character after [ is ^, the sense of matching isinverted.

To include a literal '^', place it anywhere else but first. Toinclude a literal ']' place it first or immediately after aninitial ^. To include a literal '-' make it the first (orsecond after ^) or last character, or the second endpoint ofa 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.)

Additional Atoms

Since some sed implementations provide additional regular expressionatoms (not defined in POSIX 1003.2), psed is capable of translatingthe 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 setto a string containing the requested characters, e.g.:PSEDEXTBRE='<>wW'.

ENVIRONMENT

The environment variable PSEDEXTBRE may be set to extend BREs.See Additional Atoms.

DIAGNOSTICS

  • 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, ormultiple 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 backreferencesin the BRE.

  • invalid repeat clause '\{%s\}'

    The repeat clause does not contain a valid integer value, or pair ofvalues.

  • 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 ascript 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 aa, 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 runningthe sed script

  1. #no autoprint
  2. s/^.*Warn( *"\([^"]*\)".*$/\1/
  3. t process
  4. b
  5. :process
  6. s/$!/%s/g
  7. s/$[_[:alnum:]]\{1,\}/%s/g
  8. s/\/\/g
  9. s/^/=item /
  10. p

on the program's own text, and piping the output into sort -u.

SED SCRIPT TRANSLATION

If this program is invoked with the name s2p it will act as ased-to-Perl translator. After option processing (all otherarguments are ignored), a Perl program is printed on standardoutput, which will process the input stream (as read from allarguments) in the way defined by the sed script and the option settingused for the translation.

SEE ALSO

perl(1), re_format(7)

BUGS

The l command will show escape characters (ESC) as '\e', buta 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 Perlinterpretation, which will re-use the "last last successfully executedregular expression". Since keeping track of pattern usage would createterribly cluttered code, and differences would only appear in obscurecontext (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 OpenBSDimplementation, except where otherwise noted (see BUGS).

AUTHOR

This Perl implementation of sed was written by Wolfgang Laun,[email protected].

COPYRIGHT and LICENSE

This program is free and open software. You may use, modify,distribute, and sell this program (and any modified variants) in anyway you wish, provided you do not restrict others from doing the same.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Program that diffs an extracte ...Print or Check SHA Checksums (Berikutnya)