├── readme.txt ├── sed.stream.editor.cheat.sheet.doc ├── sed.stream.editor.cheat.sheet.pdf └── sed.stream.editor.cheat.sheet.txt /readme.txt: -------------------------------------------------------------------------------- 1 | This is sed - Unix Stream Editor - cheat sheet. I created it because I wanted 2 | to master sed inside out. With this cheat sheet I could have the whole sed 3 | reference in front of me and this allowed me to quicker learn all the 4 | commands. 5 | 6 | I explain a lot more about how and why I created it in my article "Sed - UNIX 7 | Stream Editor - Cheat Sheet". This article can be read here: 8 | 9 | http://www.catonmat.net/blog/sed-stream-editor-cheat-sheet/ 10 | 11 | ------------------------------------------------------------------------------ 12 | 13 | This cheat sheet was created by Peteris Krumins (peter@catonmat.net). 14 | His blog is at http://www.catonmat.net -- good coders code, great reuse. 15 | 16 | The cheat sheet is released under GNU Free Documentation License. 17 | 18 | ------------------------------------------------------------------------------ 19 | 20 | The cheat sheet contains the following topics: 21 | 22 | * Command line argument summary. 23 | * Sed command summary: 24 | * Do they take a single address, pattern or a range of addresses. 25 | * What do they modify, is it input stream, output stream, pattern 26 | space or hold buffer. 27 | * All commands with their descriptions. 28 | * GNU sed extensions. 29 | * Address range format. 30 | 31 | 32 | It is available in .pdf, .txt (ascii) and .doc (microsoft word) 33 | formats. The latest versions can always be found at: 34 | 35 | .txt: http://www.catonmat.net/download/sed.stream.editor.cheat.sheet.txt 36 | .pdf: http://www.catonmat.net/download/sed.stream.editor.cheat.sheet.pdf 37 | .doc: http://www.catonmat.net/download/sed.stream.editor.cheat.sheet.doc 38 | 39 | I'm sorry that I used Microsoft Word to create it, but I was not proficient in 40 | LaTeX back then. 41 | 42 | ------------------------------------------------------------------------------ 43 | 44 | Sincerely, 45 | Peteris Krumins 46 | http://www.catonmat.net 47 | 48 | -------------------------------------------------------------------------------- /sed.stream.editor.cheat.sheet.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkrumins/sed-cheat-sheet/652669838e051920266f363ac882eaadb2e55cfe/sed.stream.editor.cheat.sheet.doc -------------------------------------------------------------------------------- /sed.stream.editor.cheat.sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkrumins/sed-cheat-sheet/652669838e051920266f363ac882eaadb2e55cfe/sed.stream.editor.cheat.sheet.pdf -------------------------------------------------------------------------------- /sed.stream.editor.cheat.sheet.txt: -------------------------------------------------------------------------------- 1 | .---------------------------------------------------------------------. 2 | | | 3 | | UNIX Stream Editor | 4 | | Sed Cheat Sheet | 5 | | | 6 | '---------------------------------------------------------------------' 7 | | Peteris Krumins (peter@catonmat.net), 2007.08.22 | 8 | | http://www.catonmat.net - good coders code, great reuse | 9 | '---------------------------------------------------------------------' 10 | 11 | ==================== How Commands Affect Streams ==================== 12 | 13 | .---------.-----------.-----------------------------------------. 14 | | | | Modifications to: | 15 | | | Address '---------.---------.---------.-----------' 16 | | Command | or Range | Input | Output | Pattern | Hold | 17 | | | | Stream | Stream | Space | Buffer | 18 | '---------+-----------+---------+---------+---------+-----------' 19 | | = | - | - | + | - | - | 20 | | a | 1 | - | + | - | - | 21 | | b | 2 | - | - | - | - | 22 | | c | 2 | - | + | - | - | 23 | | d | 2 | + | - | + | - | 24 | | D | 2 | + | - | + | - | 25 | | g | 2 | - | - | + | - | 26 | | G | 2 | - | - | + | - | 27 | | h | 2 | - | - | - | + | 28 | | H | 2 | - | - | - | + | 29 | | i | 1 | - | + | - | - | 30 | | l | 1 | - | + | - | - | 31 | | n | 2 | + | * | - | - | 32 | | N | 2 | + | - | + | - | 33 | | p | 2 | - | + | - | - | 34 | | P | 2 | - | + | - | - | 35 | | q | 1 | - | - | - | - | 36 | | r | 1 | - | + | - | - | 37 | | s | 2 | - | - | + | - | 38 | | t | 2 | - | - | - | - | 39 | | w | 2 | - | + | - | - | 40 | | x | 2 | - | - | + | + | 41 | | y | 2 | - | - | + | - | 42 | '---------'-----------'---------'---------'---------'-----------' 43 | 44 | Modifications to: 45 | 46 | 1 Command takes single address or pattern. 47 | 2 Command takes pair of addresses. 48 | - Command does not modify the buffer. 49 | + Command modifies the buffer. 50 | * The ``n'' command may or may not generate output depending 51 | on the ``-n'' command option. 52 | 53 | 54 | ========================== Command Summary ========================== 55 | 56 | .----------------.----------------------------------------------------. 57 | | | | 58 | | Command | Description | 59 | | | | 60 | '----------------+----------------------------------------------------' 61 | | # | Adds a comment. | 62 | '----------------+----------------------------------------------------' 63 | | = | The "=" command prints the current line number to | 64 | | | standard output. | 65 | '----------------+----------------------------------------------------' 66 | | a \ | The "a" command appends text after the | 67 | | text | range or pattern. | 68 | '----------------+----------------------------------------------------' 69 | | b label | The "b" command branches to the label. You can | 70 | | | specify a label with a text string followed by a | 71 | | | colon. If no label is there, branch to the end of | 72 | | | the script. | 73 | '----------------+----------------------------------------------------' 74 | | c \ | The "c" command changes the current line with | 75 | | text | text. | 76 | '----------------+----------------------------------------------------' 77 | | d | The "d" command deletes the current pattern space, | 78 | | | reads in the next line, puts the new line into the | 79 | | | pattern space, and aborts the current command, and | 80 | | | starts execution at the first sed command. | 81 | '----------------+----------------------------------------------------' 82 | | D | The "D" command deletes the first portion of the | 83 | | | pattern space, up to the new line character, | 84 | | | leaving the rest of the pattern alone. | 85 | '----------------+----------------------------------------------------' 86 | | g | Instead of exchanging (the "x" command) the hold | 87 | | | space with the pattern space, you can copy the | 88 | | | hold space to the pattern space with the "g" | 89 | | | command. | 90 | '----------------+----------------------------------------------------' 91 | | G | If you want to append to the pattern space, use | 92 | | | the "G" command. | 93 | '----------------+----------------------------------------------------' 94 | | h | The "h" command copies the pattern buffer into the | 95 | | | hold buffer. | 96 | '----------------+----------------------------------------------------' 97 | | H | The "H" command allows you to combine several | 98 | | | lines in the hold buffer. It acts like the "N" | 99 | | | command as lines are appended to the buffer, with | 100 | | | a "\n" between the lines. You can save several | 101 | | | lines in the hold buffer, and print them only if a | 102 | | | particular pattern is found later. | 103 | '----------------+----------------------------------------------------' 104 | | i \ | You can insert text before the pattern with | 105 | | text | the "i" command. | 106 | '----------------+----------------------------------------------------' 107 | | l | The "l" command prints the current pattern space. | 108 | | | It is therefore useful in debugging sed scripts. | 109 | | | It also converts unprintable characters into | 110 | | | printing characters by outputting the value in | 111 | | | octal preceded by a "\" character. | 112 | '----------------+----------------------------------------------------' 113 | | n | The "n" command will print out the current pattern | 114 | | | space (unless the "-n" flag is used), empty the | 115 | | | current pattern space, and read in the next | 116 | | | line of input. | 117 | '----------------+----------------------------------------------------' 118 | | N | The "N" command does not print out the current | 119 | | | pattern space and does not empty the pattern | 120 | | | space. It reads in the next line, but appends a | 121 | | | new line character along with the input line | 122 | | | itself to the pattern space. | 123 | '----------------+----------------------------------------------------' 124 | | p | Another useful command is the print command: "p". | 125 | | | If sed wasn't started with an "-n" option, the "p" | 126 | | | command will duplicate the input. The "p" command | 127 | | | prints the entire pattern space. | 128 | '----------------+----------------------------------------------------' 129 | | P | The "P" command only prints the first part of the | 130 | | | pattern space, up to the NEWLINE character. | 131 | '----------------+----------------------------------------------------' 132 | | q | There is one more simple command that can restrict | 133 | | | the changes to a set of lines. It is the "q" | 134 | | | command: quit. This command is most useful when | 135 | | | you wish to abort the editing after some condition | 136 | | | is reached. | 137 | '----------------+----------------------------------------------------' 138 | | r filename | The "r" command will append text from filename | 139 | | | after the range or pattern. | 140 | '----------------+----------------------------------------------------' 141 | | s/regex/repl/ | The substitute command replaces all occurrences of | 142 | | | the regular expression (regex) with repl(acement) | 143 | '----------------+----------------------------------------------------' 144 | | t label | You can execute a branch if a pattern is found. | 145 | | | You may want to execute a branch only if a | 146 | | | substitution is made. The command "t label" will | 147 | | | branch to the label if the last substitute command | 148 | | | modified the pattern space. | 149 | '----------------+----------------------------------------------------' 150 | | w filename | With this command, you can specify a filename that | 151 | | | will receive the modified data. | 152 | '----------------+----------------------------------------------------' 153 | | x | The "x" command exchanges the hold buffer and the | 154 | | | pattern buffer. | 155 | '----------------+----------------------------------------------------' 156 | | y/source/dest/ | Transliterate the characters in the pattern space, | 157 | | | which appear in source to the corresponding | 158 | | | character in dest(ination). | 159 | '----------------'----------------------------------------------------' 160 | 161 | 162 | ======================== Command Extensions ========================= 163 | 164 | .----------------.----------------------------------------------------. 165 | | | | 166 | | Command | Description | 167 | | | | 168 | '----------------+----------------------------------------------------' 169 | | Q | Immediately quit the sed script without processing | 170 | | | any more input. (zero or one address command) | 171 | '----------------+----------------------------------------------------' 172 | | R filename | Append a line read from filename. (zero or one | 173 | | | address command). | 174 | '----------------+----------------------------------------------------' 175 | | T label | If no s/// has done a successful substitution | 176 | | | since the last input line was read and since the | 177 | | | last t or T command, then branch to label; | 178 | | | if label is omitted, branch to end of script. | 179 | | | (accepts address range). | 180 | '----------------+----------------------------------------------------' 181 | | W filename | Write the first line of the current pattern space | 182 | | | to filename. (accepts address range). | 183 | '----------------'----------------------------------------------------' 184 | 185 | 186 | ======================= Address Range Summary ======================= 187 | 188 | .----------------.----------------------------------------------------. 189 | | | | 190 | | Format | Description | 191 | | | | 192 | '----------------+----------------------------------------------------' 193 | | number | Match only the specified line number. | 194 | '----------------+----------------------------------------------------' 195 | | first~step | Match every step'th line starting with line first. | 196 | '----------------+----------------------------------------------------' 197 | | $ | Match the last line. | 198 | '----------------+----------------------------------------------------' 199 | | 0, addr2 | Start out in "matched first address" state, | 200 | | | until addr2 is found. | 201 | '----------------+----------------------------------------------------' 202 | | /regex/ | Match lines matching the regular expression regex. | 203 | '----------------+----------------------------------------------------' 204 | | addr1,+N | Will match addr1 and the N lines following addr1. | 205 | '----------------+----------------------------------------------------' 206 | | \cregexc | Match lines matching the regular expression regex. | 207 | | | The c may be any character. | 208 | '----------------+----------------------------------------------------' 209 | | addr1,~N | Will match addr1 and the lines following addr1 | 210 | | | until the next line whose input line number | 211 | | | is a multiple of N. | 212 | '----------------'----------------------------------------------------' 213 | 214 | 215 | ============== GNU Sed's Command Line Argument Summary ============== 216 | 217 | .---------------------.-----------------------------------------------. 218 | | | | 219 | | Argument | Description | 220 | | | | 221 | '---------------------+-----------------------------------------------' 222 | | -n | | 223 | | --quiet | Suppress automatic printing of pattern space. | 224 | | --silent | | 225 | '---------------------+-----------------------------------------------' 226 | | -e script | | 227 | | --expression=script | Add the script to the commands to be executed.| 228 | | | | 229 | '---------------------+-----------------------------------------------' 230 | | -f script-file | Add the contents of script-file to the | 231 | | --file=script-file | commands to be executed. | 232 | '---------------------+-----------------------------------------------' 233 | | -i[suffix] | Sdit files in place (makes backup if | 234 | | --in-place[=suffix] | extension supplied). | 235 | '---------------------+-----------------------------------------------' 236 | | -l N | Specify the desired line-wrap length for | 237 | | --line-length=N | the `l' command. | 238 | '---------------------+-----------------------------------------------' 239 | | -r | Use extended regular expressions in the | 240 | | --regexp-extended | script. | 241 | '---------------------+-----------------------------------------------' 242 | | -s | Consider files as separate rather than as a | 243 | | --separate | single continuous long stream. | 244 | '---------------------+-----------------------------------------------' 245 | | -u | Load minimal amounts of data from the input | 246 | | --unbuffered | files and flush the output buffers more often.| 247 | '---------------------+-----------------------------------------------' 248 | | --help | Display this help and exit | 249 | '---------------------+-----------------------------------------------' 250 | | -V | Output version information and exit | 251 | | --version | | 252 | '---------------------'-----------------------------------------------' 253 | 254 | ===================================================================== 255 | 256 | .---------------------------------------------------------------------. 257 | | Peteris Krumins (peter@catonmat.net), 2007.08.22 | 258 | | http://www.catonmat.net - good coders code, great reuse | 259 | '---------------------------------------------------------------------' 260 | 261 | --------------------------------------------------------------------------------