__________________ HOLD AREA ---+ __________________ | | h:Put into hold area |- x: eXchange hold H:Append to hold area | and pattern N:Append next i/p line g:Get from hold area | n:Get next i/p line G:Append from hold area | _________________ __________________ | ________________ INPUT STREAM PATTERN SPACE ---+ OUTPUT STREAM _________________ __________________ ________________ +----------+ c{text}:Change to text i{text}:Insert Before |number | A d:Delete(+restart) a{text}:Append After |first~step| D (GNU Sed) D:Delete only first line =:Line Number |$ | D p:Print r{file}:Text from file |/regex/ | R P:Print only first line w{file}:Write to file |addr,addr | E s/regex/replace:Substitute! |addr,+N | S y/source/dest:Transliterate |addr~N | S (GNU Sed) |----------+-------------------------------------------+--------+-------------+ | b:Branch | t:Branch if any previous search succeeded | q:Quit | l:Debug dump| _______________________________________________________________________________ Command Detail: .----------------.----------------------------------------------------. | Command | Description | '----------------+----------------------------------------------------' | # | Comment Line. | '----------------+----------------------------------------------------' | = | Print current line number. | '----------------+----------------------------------------------------' | a \ | Append text after printing the current pattern. | | text | NB: Affects output stream and NOT pattern space. | '----------------+----------------------------------------------------' | b label | Branch to a given label, if given, or to end of | | | script. Labels are defined by 'colon text'(:label1)| '----------------+----------------------------------------------------' | c \ | Changes the current pattern to the given text. | | text | NB: Unlike i and a, this command modifies pattern | | | space. | '----------------+----------------------------------------------------' | d | The "d" command deletes the current pattern space, | | | reads in the next line, puts the new line into the | | | pattern space, and aborts the current command, and | | | starts execution at the first sed command. | '----------------+----------------------------------------------------' | D | The "D" command deletes the first portion of the | | | pattern space, up to the new line character, | | | leaving the rest of the pattern alone. | '----------------+----------------------------------------------------' | g | Overwrite the pattern space with the hold space. | '----------------+----------------------------------------------------' | G | Append the hold space to the pattern space. | | | NB: A '\n' separates the two. | '----------------+----------------------------------------------------' | h | Overwrite the hold space with the pattern space. | '----------------+----------------------------------------------------' | H | Append the pattern space to the hold space. | | | NB: A '\n' separates the two. | '----------------+----------------------------------------------------' | i \ | Insert text before printing the current pattern. | | text | NB: Affects output stream and NOT pattern space. | '----------------+----------------------------------------------------' | l | The "l" command prints the current pattern space. | | | It is useful in debugging sed scripts. | | | It converts unprintable characters into printable | | | by outputting the value in octal (preceded by '\') | '----------------+----------------------------------------------------' | n | The "n" command will print out the current pattern | | | space (unless the "-n" flag is used), empty the | | | current pattern space, and read in the next | | | line of input. | '----------------+----------------------------------------------------' | N | The "N" command does not print out the current | | | pattern space and does not empty the pattern | | | space. It reads in the next line, but appends a | | | new line character along with the input line | | | itself to the pattern space. | '----------------+----------------------------------------------------' | p | Prints the entire pattern space. | | | NB: Useful when sed is started with the '-n' option| '----------------+----------------------------------------------------' | P | The "P" command only prints the first part of the | | | pattern space, up to the NEWLINE character. | '----------------+----------------------------------------------------' | q | Quits the script immediatly. | '----------------+----------------------------------------------------' | r filename | The "r" command will append text from filename | | | after the range or pattern. | '----------------+----------------------------------------------------' | s/regex/repl/ | The substitute command replaces all occurrences of | | | the regular expression (regex) with repl(acement) | '----------------+----------------------------------------------------' | t label | You can execute a branch if a pattern is found. | | | You may want to execute a branch only if a | | | substitution is made. The command "t label" will | | | branch to the label if a previous substitute | | | command modified the pattern space. | '----------------+----------------------------------------------------' | w filename | With this command, you can specify a filename that | | | will receive the modified data. | '----------------+----------------------------------------------------' | x | The "x" command exchanges the hold buffer and the | | | pattern buffer. | '----------------+----------------------------------------------------' | y/source/dest/ | Transliterate the characters in the pattern space, | | | which appear in source to the corresponding | | | character in dest(ination). | '----------------'----------------------------------------------------' _______________________________________________________________________________ EXAMPLE USAGE ============= +-----------------------------------------------------------------------------+ | # Print lines of text between START and END | | /START/,/END/p | | d | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Right align lines 10 to 30 | | 10,30{ | | :align | | s/^.\{1,78\}$/ &/ | | talign | | } | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Substiture "this" with "that" except on lines that start with "ok" | | /ok/!s/this/that/g | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Reverse order of lines | | 1!G | | h | | $!d | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Reverse each character on a line | | /\n/!G | | s/\(.\)\(.*\n\)/&\2\1/ | | //D | | s/.// | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Join pairs of lines side-by-side | | $!N | | s/\n/ / | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # If a line ends in a backslash, append the next line to it | | :joinlines | | /\\$/N | | s/\\\n// | | tjoinlines | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Delete duplicate lines from a sorted file | | $!N | | /^\(.*\)\n\1$/!P | | D | | | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Print only lines of less that 80 characters | | /^.\{65\}/d | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Get E-mail message header | | /^$/q | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # Get E-mail message body | | 1,/^$/d | +-----------------------------------------------------------------------------+ +-----------------------------------------------------------------------------+ | # UPPERCASE "strings between quotes" | | /".*"/{ | | h | | s/^\(.*\)"\(.*\)"\(.*\)$/\2/ | | y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ | | G | | s/^\(.*\)\n\(.*\)"\(.*\)"\(.*\)$/\2"\1"\4/ | | } | +-----------------------------------------------------------------------------+ http://www.the-brown-dragon.com/