sed metacharacters

Upload: chaitucvs

Post on 14-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 SED Metacharacters

    1/3

    SED Metacharacters

    Description CommandFormat

    Example

    ^ Beginning-of-line /^love/ Matches all lines beginning withlove

    $ End-of-line anchor /love$/ Matches all lines ending with love

    . Matches one character butnot the newline character /l..e/ Matches lines containing an l,followed by 2 characters,followed by an e.

    * Matches zero or more /*love/ Matches lines with zero or morespaces, followed by the patternlove.

    [] Matches a particular set ofcharacter.

    /[Ll]ove/ Matches lines containing love orLove.

    [^] Matches one character notin the set

    /[^A-KM-Z]ove/ Matches lines not containing Athrough K or M through Zfollowed by love.

    \< Beginning-of-word anchor ^ End-of-Word anchor /love\>/ Matches lines containing a wordthat ends with love (supported byvi and grep)

    & Saves search string so itcan be remembered

    s/love/**&**/ & represents the search string.The string love will be replacedwith itself surrounded byasterisks.

    \(..\) Tags match characters s^(love\)able^1er/ Tags marked portion in a register

    to be remembered later as number1. To reference later, use \1 torepeat the pattern. May use up tonine tags, starting with the firsttag at the left-most part of thepattern. ie, the pattern love issaved as tag l, to be referencedlater as \1; in this example, thesearch pattern consists of lovablefollowed by lover (supported bysed, vi andgrep)

    x\{m\}orx\{m,\} orx\{m,n\}a

    Repetition of character x,m times,at least m times,at least m and not morethan n times

    o\{5,10\}

    o\{5,\}o\{5,10\}

    Matches if line contains between5 and 10 consecutive occurrencesof the letter o (supported by viand grep)

    aNot dependable on all versions of UNIX or all pattern-matching utilities; usually workswith vi and grep

  • 7/27/2019 SED Metacharacters

    2/3

    SED Commands

    Command

    a\ Appends one or more lines of text to the current linec\ Changes (replaces) text in the current line with new textd deletes linesi\ Inserts text above the current lineh Copies the contents of the pattern space to a holding buffer

    H Appends the contents of the pattern space to a holding bufferg Gets what is in the holding buffer and copies it into the pattern bugger,

    overwriting what was there.G Gets what is in the holding buffer and copies it into the pattern buffer, appending

    to what was therel lists nonprinting charactersp print linesn Reads the next input line and starts processing the newline with the next

    command rather than the first commandq Quits or exitssedr Reads lines from a file

    ! Applies the command to all lines exceptthe selected ones.s Substitutes one string for another

    Substitution Flags:

    g Globally substitutes on a linep Prints linesw Writes lines out to a filex Exchanges contents of the holding buffer with the pattern spacey Translates one character to another (cannot use regular expression metacharacters

    with y)

    Options-e Allows multiple edits-f Precedes asedscript filename-n Suppresses default output

    Syntax usually consists of /pattern/action - The slash(/) are delimiters. Pattern ofexecution:

    Each pattern is sequentially searched until a match is found. When a match is found, the corresponding action is performed When the action is complete, the next pattern is selected and step 1 is repeated. SED automatically outputs the modified record When all patterns have been exhausted, the next line is read and step 1 is

    repeated.

    Useful Regular Expressionsblank lines /^$/an entire line /^.$/one or more spaces / */valid URLs /[a-zA-Z][a-zA-Z]*:\/\/[a-z0-9][a-z0-9\.]*.*/formatted dollar /\$[0-9]*\.[0-9][0-9]/

  • 7/27/2019 SED Metacharacters

    3/3