| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >A Sed and Awk Micro-Primer</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="PREVIOUS" |
| 14 | TITLE="Reference Cards" |
| 15 | HREF="refcards.html"><LINK |
| 16 | REL="NEXT" |
| 17 | TITLE="Awk" |
| 18 | HREF="awk.html"><META |
| 19 | HTTP-EQUIV="Content-Style-Type" |
| 20 | CONTENT="text/css"><LINK |
| 21 | REL="stylesheet" |
| 22 | HREF="common/kde-common.css" |
| 23 | TYPE="text/css"><META |
| 24 | HTTP-EQUIV="Content-Type" |
| 25 | CONTENT="text/html; charset=iso-8859-1"><META |
| 26 | HTTP-EQUIV="Content-Language" |
| 27 | CONTENT="en"><LINK |
| 28 | REL="stylesheet" |
| 29 | HREF="common/kde-localised.css" |
| 30 | TYPE="text/css" |
| 31 | TITLE="KDE-English"><LINK |
| 32 | REL="stylesheet" |
| 33 | HREF="common/kde-default.css" |
| 34 | TYPE="text/css" |
| 35 | TITLE="KDE-Default"></HEAD |
| 36 | ><BODY |
| 37 | CLASS="APPENDIX" |
| 38 | BGCOLOR="#FFFFFF" |
| 39 | TEXT="#000000" |
| 40 | LINK="#AA0000" |
| 41 | VLINK="#AA0055" |
| 42 | ALINK="#AA0000" |
| 43 | STYLE="font-family: sans-serif;" |
| 44 | ><DIV |
| 45 | CLASS="NAVHEADER" |
| 46 | ><TABLE |
| 47 | SUMMARY="Header navigation table" |
| 48 | WIDTH="100%" |
| 49 | BORDER="0" |
| 50 | CELLPADDING="0" |
| 51 | CELLSPACING="0" |
| 52 | ><TR |
| 53 | ><TH |
| 54 | COLSPAN="3" |
| 55 | ALIGN="center" |
| 56 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 57 | ></TR |
| 58 | ><TR |
| 59 | ><TD |
| 60 | WIDTH="10%" |
| 61 | ALIGN="left" |
| 62 | VALIGN="bottom" |
| 63 | ><A |
| 64 | HREF="refcards.html" |
| 65 | ACCESSKEY="P" |
| 66 | >Prev</A |
| 67 | ></TD |
| 68 | ><TD |
| 69 | WIDTH="80%" |
| 70 | ALIGN="center" |
| 71 | VALIGN="bottom" |
| 72 | ></TD |
| 73 | ><TD |
| 74 | WIDTH="10%" |
| 75 | ALIGN="right" |
| 76 | VALIGN="bottom" |
| 77 | ><A |
| 78 | HREF="awk.html" |
| 79 | ACCESSKEY="N" |
| 80 | >Next</A |
| 81 | ></TD |
| 82 | ></TR |
| 83 | ></TABLE |
| 84 | ><HR |
| 85 | ALIGN="LEFT" |
| 86 | WIDTH="100%"></DIV |
| 87 | ><DIV |
| 88 | CLASS="APPENDIX" |
| 89 | ><H1 |
| 90 | ><A |
| 91 | NAME="SEDAWK" |
| 92 | ></A |
| 93 | >Appendix C. A Sed and Awk Micro-Primer</H1 |
| 94 | ><P |
| 95 | ><A |
| 96 | NAME="SEDREF" |
| 97 | ></A |
| 98 | ></P |
| 99 | ><P |
| 100 | >This is a very brief introduction to the <B |
| 101 | CLASS="COMMAND" |
| 102 | >sed</B |
| 103 | > |
| 104 | and <B |
| 105 | CLASS="COMMAND" |
| 106 | >awk</B |
| 107 | > text processing utilities. We will |
| 108 | deal with only a few basic commands here, but that will suffice |
| 109 | for understanding simple sed and awk constructs within shell |
| 110 | scripts.</P |
| 111 | ><P |
| 112 | ><B |
| 113 | CLASS="COMMAND" |
| 114 | >sed</B |
| 115 | >: a non-interactive |
| 116 | text file editor</P |
| 117 | ><P |
| 118 | ><B |
| 119 | CLASS="COMMAND" |
| 120 | >awk</B |
| 121 | >: a field-oriented pattern processing |
| 122 | language with a <B |
| 123 | CLASS="COMMAND" |
| 124 | >C</B |
| 125 | >-style syntax</P |
| 126 | ><P |
| 127 | >For all their differences, the two utilities share a similar |
| 128 | invocation syntax, use <A |
| 129 | HREF="regexp.html#REGEXREF" |
| 130 | >regular |
| 131 | expressions </A |
| 132 | >, read input by default |
| 133 | from <TT |
| 134 | CLASS="FILENAME" |
| 135 | >stdin</TT |
| 136 | >, and output to |
| 137 | <TT |
| 138 | CLASS="FILENAME" |
| 139 | >stdout</TT |
| 140 | >. These are well-behaved UNIX tools, |
| 141 | and they work together well. The output from one can be piped |
| 142 | to the other, and their combined capabilities give shell scripts |
| 143 | some of the power of <A |
| 144 | HREF="wrapper.html#PERLREF" |
| 145 | >Perl</A |
| 146 | >.</P |
| 147 | ><DIV |
| 148 | CLASS="NOTE" |
| 149 | ><TABLE |
| 150 | CLASS="NOTE" |
| 151 | WIDTH="100%" |
| 152 | BORDER="0" |
| 153 | ><TR |
| 154 | ><TD |
| 155 | WIDTH="25" |
| 156 | ALIGN="CENTER" |
| 157 | VALIGN="TOP" |
| 158 | ><IMG |
| 159 | SRC="common/note.png" |
| 160 | HSPACE="5" |
| 161 | ALT="Note"></TD |
| 162 | ><TD |
| 163 | ALIGN="LEFT" |
| 164 | VALIGN="TOP" |
| 165 | ><P |
| 166 | >One important difference between the utilities is |
| 167 | that while shell scripts can easily pass arguments to sed, it |
| 168 | is more cumbersome for awk (see <A |
| 169 | HREF="wrapper.html#COLTOTALER" |
| 170 | >Example 36-5</A |
| 171 | > |
| 172 | and <A |
| 173 | HREF="ivr.html#COLTOTALER2" |
| 174 | >Example 28-2</A |
| 175 | >). |
| 176 | </P |
| 177 | ></TD |
| 178 | ></TR |
| 179 | ></TABLE |
| 180 | ></DIV |
| 181 | ><DIV |
| 182 | CLASS="SECT1" |
| 183 | ><H1 |
| 184 | CLASS="SECT1" |
| 185 | ><A |
| 186 | NAME="AEN23170" |
| 187 | ></A |
| 188 | >C.1. Sed</H1 |
| 189 | ><P |
| 190 | ><I |
| 191 | CLASS="FIRSTTERM" |
| 192 | >Sed</I |
| 193 | > is a non-interactive |
| 194 | <A |
| 195 | NAME="AEN23174" |
| 196 | HREF="#FTN.AEN23174" |
| 197 | >[1]</A |
| 198 | > |
| 199 | <B |
| 200 | CLASS="COMMAND" |
| 201 | >s</B |
| 202 | >tream <B |
| 203 | CLASS="COMMAND" |
| 204 | >ed</B |
| 205 | >itor. It |
| 206 | receives text input, whether from <TT |
| 207 | CLASS="FILENAME" |
| 208 | >stdin</TT |
| 209 | > |
| 210 | or from a file, performs certain operations on specified lines |
| 211 | of the input, one line at a time, then outputs the result to |
| 212 | <TT |
| 213 | CLASS="FILENAME" |
| 214 | >stdout</TT |
| 215 | > or to a file. Within a shell script, |
| 216 | <I |
| 217 | CLASS="FIRSTTERM" |
| 218 | >sed</I |
| 219 | > is usually one of several tool |
| 220 | components in a pipe.</P |
| 221 | ><P |
| 222 | ><I |
| 223 | CLASS="FIRSTTERM" |
| 224 | >Sed</I |
| 225 | > determines which lines of |
| 226 | its input that it will operate on from the <I |
| 227 | CLASS="FIRSTTERM" |
| 228 | >address |
| 229 | range</I |
| 230 | > passed to it. |
| 231 | <A |
| 232 | NAME="AEN23185" |
| 233 | HREF="#FTN.AEN23185" |
| 234 | >[2]</A |
| 235 | > |
| 236 | Specify this address range either by line number or by a |
| 237 | pattern to match. For example, <TT |
| 238 | CLASS="REPLACEABLE" |
| 239 | ><I |
| 240 | >3d</I |
| 241 | ></TT |
| 242 | > |
| 243 | signals <I |
| 244 | CLASS="FIRSTTERM" |
| 245 | >sed</I |
| 246 | > to delete line 3 of the |
| 247 | input, and <TT |
| 248 | CLASS="REPLACEABLE" |
| 249 | ><I |
| 250 | >/Windows/d</I |
| 251 | ></TT |
| 252 | > tells sed |
| 253 | that you want every line of the input containing a match to |
| 254 | <SPAN |
| 255 | CLASS="QUOTE" |
| 256 | >"Windows"</SPAN |
| 257 | > deleted.</P |
| 258 | ><P |
| 259 | >Of all the operations in the <I |
| 260 | CLASS="FIRSTTERM" |
| 261 | >sed</I |
| 262 | > |
| 263 | toolkit, we will focus primarily on the three most commonly |
| 264 | used ones. These are <B |
| 265 | CLASS="COMMAND" |
| 266 | >p</B |
| 267 | >rinting (to |
| 268 | <TT |
| 269 | CLASS="FILENAME" |
| 270 | >stdout</TT |
| 271 | >), <B |
| 272 | CLASS="COMMAND" |
| 273 | >d</B |
| 274 | >eletion, |
| 275 | and <B |
| 276 | CLASS="COMMAND" |
| 277 | >s</B |
| 278 | >ubstitution.</P |
| 279 | ><P |
| 280 | ><A |
| 281 | NAME="SEDBASICTABLE" |
| 282 | ></A |
| 283 | ></P |
| 284 | ><DIV |
| 285 | CLASS="TABLE" |
| 286 | ><HR><A |
| 287 | NAME="AEN23200" |
| 288 | ></A |
| 289 | ><P |
| 290 | ><B |
| 291 | >Table C-1. Basic sed operators</B |
| 292 | ></P |
| 293 | ><TABLE |
| 294 | BORDER="1" |
| 295 | CLASS="CALSTABLE" |
| 296 | ><THEAD |
| 297 | ><TR |
| 298 | ><TH |
| 299 | ALIGN="LEFT" |
| 300 | VALIGN="TOP" |
| 301 | >Operator</TH |
| 302 | ><TH |
| 303 | ALIGN="LEFT" |
| 304 | VALIGN="TOP" |
| 305 | >Name</TH |
| 306 | ><TH |
| 307 | ALIGN="LEFT" |
| 308 | VALIGN="TOP" |
| 309 | >Effect</TH |
| 310 | ></TR |
| 311 | ></THEAD |
| 312 | ><TBODY |
| 313 | ><TR |
| 314 | ><TD |
| 315 | ALIGN="LEFT" |
| 316 | VALIGN="TOP" |
| 317 | ><TT |
| 318 | CLASS="OPTION" |
| 319 | >[address-range]/p</TT |
| 320 | ></TD |
| 321 | ><TD |
| 322 | ALIGN="LEFT" |
| 323 | VALIGN="TOP" |
| 324 | >print</TD |
| 325 | ><TD |
| 326 | ALIGN="LEFT" |
| 327 | VALIGN="TOP" |
| 328 | >Print [specified address range]</TD |
| 329 | ></TR |
| 330 | ><TR |
| 331 | ><TD |
| 332 | ALIGN="LEFT" |
| 333 | VALIGN="TOP" |
| 334 | ><TT |
| 335 | CLASS="OPTION" |
| 336 | >[address-range]/d</TT |
| 337 | ></TD |
| 338 | ><TD |
| 339 | ALIGN="LEFT" |
| 340 | VALIGN="TOP" |
| 341 | >delete</TD |
| 342 | ><TD |
| 343 | ALIGN="LEFT" |
| 344 | VALIGN="TOP" |
| 345 | >Delete [specified address range]</TD |
| 346 | ></TR |
| 347 | ><TR |
| 348 | ><TD |
| 349 | ALIGN="LEFT" |
| 350 | VALIGN="TOP" |
| 351 | ><TT |
| 352 | CLASS="OPTION" |
| 353 | >s/pattern1/pattern2/</TT |
| 354 | ></TD |
| 355 | ><TD |
| 356 | ALIGN="LEFT" |
| 357 | VALIGN="TOP" |
| 358 | >substitute</TD |
| 359 | ><TD |
| 360 | ALIGN="LEFT" |
| 361 | VALIGN="TOP" |
| 362 | >Substitute pattern2 for first instance of pattern1 in a line</TD |
| 363 | ></TR |
| 364 | ><TR |
| 365 | ><TD |
| 366 | ALIGN="LEFT" |
| 367 | VALIGN="TOP" |
| 368 | ><TT |
| 369 | CLASS="OPTION" |
| 370 | >[address-range]/s/pattern1/pattern2/</TT |
| 371 | ></TD |
| 372 | ><TD |
| 373 | ALIGN="LEFT" |
| 374 | VALIGN="TOP" |
| 375 | >substitute</TD |
| 376 | ><TD |
| 377 | ALIGN="LEFT" |
| 378 | VALIGN="TOP" |
| 379 | >Substitute pattern2 for first instance of pattern1 in a |
| 380 | line, over <TT |
| 381 | CLASS="REPLACEABLE" |
| 382 | ><I |
| 383 | >address-range</I |
| 384 | ></TT |
| 385 | ></TD |
| 386 | ></TR |
| 387 | ><TR |
| 388 | ><TD |
| 389 | ALIGN="LEFT" |
| 390 | VALIGN="TOP" |
| 391 | ><TT |
| 392 | CLASS="OPTION" |
| 393 | >[address-range]/y/pattern1/pattern2/</TT |
| 394 | ></TD |
| 395 | ><TD |
| 396 | ALIGN="LEFT" |
| 397 | VALIGN="TOP" |
| 398 | >transform</TD |
| 399 | ><TD |
| 400 | ALIGN="LEFT" |
| 401 | VALIGN="TOP" |
| 402 | >replace any character in pattern1 with the |
| 403 | corresponding character in pattern2, over |
| 404 | <TT |
| 405 | CLASS="REPLACEABLE" |
| 406 | ><I |
| 407 | >address-range</I |
| 408 | ></TT |
| 409 | > (equivalent of |
| 410 | <B |
| 411 | CLASS="COMMAND" |
| 412 | >tr</B |
| 413 | >)</TD |
| 414 | ></TR |
| 415 | ><TR |
| 416 | ><TD |
| 417 | ALIGN="LEFT" |
| 418 | VALIGN="TOP" |
| 419 | ><TT |
| 420 | CLASS="OPTION" |
| 421 | >[address] i pattern Filename</TT |
| 422 | ></TD |
| 423 | ><TD |
| 424 | ALIGN="LEFT" |
| 425 | VALIGN="TOP" |
| 426 | >insert</TD |
| 427 | ><TD |
| 428 | ALIGN="LEFT" |
| 429 | VALIGN="TOP" |
| 430 | >Insert pattern at address indicated in file Filename. |
| 431 | Usually used with <TT |
| 432 | CLASS="OPTION" |
| 433 | >-i</TT |
| 434 | > |
| 435 | <TT |
| 436 | CLASS="REPLACEABLE" |
| 437 | ><I |
| 438 | >in-place</I |
| 439 | ></TT |
| 440 | > option.</TD |
| 441 | ></TR |
| 442 | ><TR |
| 443 | ><TD |
| 444 | ALIGN="LEFT" |
| 445 | VALIGN="TOP" |
| 446 | ><TT |
| 447 | CLASS="OPTION" |
| 448 | >g</TT |
| 449 | ></TD |
| 450 | ><TD |
| 451 | ALIGN="LEFT" |
| 452 | VALIGN="TOP" |
| 453 | >global</TD |
| 454 | ><TD |
| 455 | ALIGN="LEFT" |
| 456 | VALIGN="TOP" |
| 457 | >Operate on <SPAN |
| 458 | CLASS="emphasis" |
| 459 | ><I |
| 460 | CLASS="EMPHASIS" |
| 461 | >every</I |
| 462 | ></SPAN |
| 463 | > pattern match |
| 464 | within each matched line of input</TD |
| 465 | ></TR |
| 466 | ></TBODY |
| 467 | ></TABLE |
| 468 | ><HR></DIV |
| 469 | ><DIV |
| 470 | CLASS="NOTE" |
| 471 | ><TABLE |
| 472 | CLASS="NOTE" |
| 473 | WIDTH="100%" |
| 474 | BORDER="0" |
| 475 | ><TR |
| 476 | ><TD |
| 477 | WIDTH="25" |
| 478 | ALIGN="CENTER" |
| 479 | VALIGN="TOP" |
| 480 | ><IMG |
| 481 | SRC="common/note.png" |
| 482 | HSPACE="5" |
| 483 | ALT="Note"></TD |
| 484 | ><TD |
| 485 | ALIGN="LEFT" |
| 486 | VALIGN="TOP" |
| 487 | ><P |
| 488 | >Unless the <TT |
| 489 | CLASS="OPTION" |
| 490 | >g</TT |
| 491 | > |
| 492 | (<I |
| 493 | CLASS="FIRSTTERM" |
| 494 | >global</I |
| 495 | >) operator is appended to a |
| 496 | <I |
| 497 | CLASS="FIRSTTERM" |
| 498 | >substitute</I |
| 499 | > command, the substitution |
| 500 | operates only on the <SPAN |
| 501 | CLASS="emphasis" |
| 502 | ><I |
| 503 | CLASS="EMPHASIS" |
| 504 | >first</I |
| 505 | ></SPAN |
| 506 | > instance of a |
| 507 | pattern match within each line.</P |
| 508 | ></TD |
| 509 | ></TR |
| 510 | ></TABLE |
| 511 | ></DIV |
| 512 | ><P |
| 513 | >From the command-line and in a shell script, a sed operation may |
| 514 | require quoting and certain options.</P |
| 515 | ><P |
| 516 | ><TABLE |
| 517 | BORDER="0" |
| 518 | BGCOLOR="#E0E0E0" |
| 519 | WIDTH="100%" |
| 520 | ><TR |
| 521 | ><TD |
| 522 | ><PRE |
| 523 | CLASS="PROGRAMLISTING" |
| 524 | > 1 sed -e '/^$/d' $filename |
| 525 | 2 # The -e option causes the next string to be interpreted as an editing instruction. |
| 526 | 3 # (If passing only a single instruction to sed, the "-e" is optional.) |
| 527 | 4 # The "strong" quotes ('') protect the RE characters in the instruction |
| 528 | 5 #+ from reinterpretation as special characters by the body of the script. |
| 529 | 6 # (This reserves RE expansion of the instruction for sed.) |
| 530 | 7 # |
| 531 | 8 # Operates on the text contained in file $filename.</PRE |
| 532 | ></TD |
| 533 | ></TR |
| 534 | ></TABLE |
| 535 | ></P |
| 536 | ><P |
| 537 | >In certain cases, a <I |
| 538 | CLASS="FIRSTTERM" |
| 539 | >sed</I |
| 540 | > editing command will |
| 541 | not work with single quotes.</P |
| 542 | ><P |
| 543 | ><TABLE |
| 544 | BORDER="0" |
| 545 | BGCOLOR="#E0E0E0" |
| 546 | WIDTH="100%" |
| 547 | ><TR |
| 548 | ><TD |
| 549 | ><PRE |
| 550 | CLASS="PROGRAMLISTING" |
| 551 | > 1 filename=file1.txt |
| 552 | 2 pattern=BEGIN |
| 553 | 3 |
| 554 | 4 sed "/^$pattern/d" "$filename" # Works as specified. |
| 555 | 5 # sed '/^$pattern/d' "$filename" has unexpected results. |
| 556 | 6 # In this instance, with strong quoting (' ... '), |
| 557 | 7 #+ "$pattern" will not expand to "BEGIN".</PRE |
| 558 | ></TD |
| 559 | ></TR |
| 560 | ></TABLE |
| 561 | ></P |
| 562 | ><DIV |
| 563 | CLASS="NOTE" |
| 564 | ><TABLE |
| 565 | CLASS="NOTE" |
| 566 | WIDTH="100%" |
| 567 | BORDER="0" |
| 568 | ><TR |
| 569 | ><TD |
| 570 | WIDTH="25" |
| 571 | ALIGN="CENTER" |
| 572 | VALIGN="TOP" |
| 573 | ><IMG |
| 574 | SRC="common/note.png" |
| 575 | HSPACE="5" |
| 576 | ALT="Note"></TD |
| 577 | ><TD |
| 578 | ALIGN="LEFT" |
| 579 | VALIGN="TOP" |
| 580 | ><P |
| 581 | ><I |
| 582 | CLASS="FIRSTTERM" |
| 583 | >Sed</I |
| 584 | > uses the <TT |
| 585 | CLASS="OPTION" |
| 586 | >-e</TT |
| 587 | > |
| 588 | option to specify that the following string is an instruction |
| 589 | or set of instructions. If there is only a single instruction |
| 590 | contained in the string, then this may be omitted.</P |
| 591 | ></TD |
| 592 | ></TR |
| 593 | ></TABLE |
| 594 | ></DIV |
| 595 | ><P |
| 596 | ><TABLE |
| 597 | BORDER="0" |
| 598 | BGCOLOR="#E0E0E0" |
| 599 | WIDTH="100%" |
| 600 | ><TR |
| 601 | ><TD |
| 602 | ><PRE |
| 603 | CLASS="PROGRAMLISTING" |
| 604 | > 1 sed -n '/xzy/p' $filename |
| 605 | 2 # The -n option tells sed to print only those lines matching the pattern. |
| 606 | 3 # Otherwise all input lines would print. |
| 607 | 4 # The -e option not necessary here since there is only a single editing instruction.</PRE |
| 608 | ></TD |
| 609 | ></TR |
| 610 | ></TABLE |
| 611 | ></P |
| 612 | ><P |
| 613 | ><A |
| 614 | NAME="SEDOPTABLE" |
| 615 | ></A |
| 616 | ></P |
| 617 | ><DIV |
| 618 | CLASS="TABLE" |
| 619 | ><HR><A |
| 620 | NAME="AEN23271" |
| 621 | ></A |
| 622 | ><P |
| 623 | ><B |
| 624 | >Table C-2. Examples of sed operators</B |
| 625 | ></P |
| 626 | ><TABLE |
| 627 | BORDER="1" |
| 628 | CLASS="CALSTABLE" |
| 629 | ><THEAD |
| 630 | ><TR |
| 631 | ><TH |
| 632 | ALIGN="LEFT" |
| 633 | VALIGN="TOP" |
| 634 | >Notation</TH |
| 635 | ><TH |
| 636 | ALIGN="LEFT" |
| 637 | VALIGN="TOP" |
| 638 | >Effect</TH |
| 639 | ></TR |
| 640 | ></THEAD |
| 641 | ><TBODY |
| 642 | ><TR |
| 643 | ><TD |
| 644 | ALIGN="LEFT" |
| 645 | VALIGN="TOP" |
| 646 | ><TT |
| 647 | CLASS="OPTION" |
| 648 | >8d</TT |
| 649 | ></TD |
| 650 | ><TD |
| 651 | ALIGN="LEFT" |
| 652 | VALIGN="TOP" |
| 653 | >Delete 8th line of input.</TD |
| 654 | ></TR |
| 655 | ><TR |
| 656 | ><TD |
| 657 | ALIGN="LEFT" |
| 658 | VALIGN="TOP" |
| 659 | ><TT |
| 660 | CLASS="OPTION" |
| 661 | >/^$/d</TT |
| 662 | ></TD |
| 663 | ><TD |
| 664 | ALIGN="LEFT" |
| 665 | VALIGN="TOP" |
| 666 | >Delete all blank lines.</TD |
| 667 | ></TR |
| 668 | ><TR |
| 669 | ><TD |
| 670 | ALIGN="LEFT" |
| 671 | VALIGN="TOP" |
| 672 | ><TT |
| 673 | CLASS="OPTION" |
| 674 | >1,/^$/d</TT |
| 675 | ></TD |
| 676 | ><TD |
| 677 | ALIGN="LEFT" |
| 678 | VALIGN="TOP" |
| 679 | >Delete from beginning of input up to, and including |
| 680 | first blank line.</TD |
| 681 | ></TR |
| 682 | ><TR |
| 683 | ><TD |
| 684 | ALIGN="LEFT" |
| 685 | VALIGN="TOP" |
| 686 | ><TT |
| 687 | CLASS="OPTION" |
| 688 | >/Jones/p</TT |
| 689 | ></TD |
| 690 | ><TD |
| 691 | ALIGN="LEFT" |
| 692 | VALIGN="TOP" |
| 693 | >Print only lines containing <SPAN |
| 694 | CLASS="QUOTE" |
| 695 | >"Jones"</SPAN |
| 696 | > (with |
| 697 | <SPAN |
| 698 | CLASS="TOKEN" |
| 699 | >-n</SPAN |
| 700 | > option).</TD |
| 701 | ></TR |
| 702 | ><TR |
| 703 | ><TD |
| 704 | ALIGN="LEFT" |
| 705 | VALIGN="TOP" |
| 706 | ><TT |
| 707 | CLASS="OPTION" |
| 708 | >s/Windows/Linux/</TT |
| 709 | ></TD |
| 710 | ><TD |
| 711 | ALIGN="LEFT" |
| 712 | VALIGN="TOP" |
| 713 | >Substitute <SPAN |
| 714 | CLASS="QUOTE" |
| 715 | >"Linux"</SPAN |
| 716 | > for first instance |
| 717 | of <SPAN |
| 718 | CLASS="QUOTE" |
| 719 | >"Windows"</SPAN |
| 720 | > found in each input line.</TD |
| 721 | ></TR |
| 722 | ><TR |
| 723 | ><TD |
| 724 | ALIGN="LEFT" |
| 725 | VALIGN="TOP" |
| 726 | ><TT |
| 727 | CLASS="OPTION" |
| 728 | >s/BSOD/stability/g</TT |
| 729 | ></TD |
| 730 | ><TD |
| 731 | ALIGN="LEFT" |
| 732 | VALIGN="TOP" |
| 733 | >Substitute <SPAN |
| 734 | CLASS="QUOTE" |
| 735 | >"stability"</SPAN |
| 736 | > for every instance |
| 737 | of <SPAN |
| 738 | CLASS="QUOTE" |
| 739 | >"BSOD"</SPAN |
| 740 | > found in each input line.</TD |
| 741 | ></TR |
| 742 | ><TR |
| 743 | ><TD |
| 744 | ALIGN="LEFT" |
| 745 | VALIGN="TOP" |
| 746 | ><TT |
| 747 | CLASS="OPTION" |
| 748 | >s/ *$//</TT |
| 749 | ></TD |
| 750 | ><TD |
| 751 | ALIGN="LEFT" |
| 752 | VALIGN="TOP" |
| 753 | >Delete all spaces at the end of every line.</TD |
| 754 | ></TR |
| 755 | ><TR |
| 756 | ><TD |
| 757 | ALIGN="LEFT" |
| 758 | VALIGN="TOP" |
| 759 | ><TT |
| 760 | CLASS="OPTION" |
| 761 | >s/00*/0/g</TT |
| 762 | ></TD |
| 763 | ><TD |
| 764 | ALIGN="LEFT" |
| 765 | VALIGN="TOP" |
| 766 | >Compress all consecutive sequences of zeroes into |
| 767 | a single zero.</TD |
| 768 | ></TR |
| 769 | ><TR |
| 770 | ><TD |
| 771 | ALIGN="LEFT" |
| 772 | VALIGN="TOP" |
| 773 | ><TT |
| 774 | CLASS="OPTION" |
| 775 | >echo "Working on it." | sed -e '1i How far are you along?'</TT |
| 776 | ></TD |
| 777 | ><TD |
| 778 | ALIGN="LEFT" |
| 779 | VALIGN="TOP" |
| 780 | >Prints "How far are you along?" as first line, |
| 781 | "Working on it" as second.</TD |
| 782 | ></TR |
| 783 | ><TR |
| 784 | ><TD |
| 785 | ALIGN="LEFT" |
| 786 | VALIGN="TOP" |
| 787 | ><TT |
| 788 | CLASS="OPTION" |
| 789 | >5i 'Linux is great.' file.txt</TT |
| 790 | ></TD |
| 791 | ><TD |
| 792 | ALIGN="LEFT" |
| 793 | VALIGN="TOP" |
| 794 | >Inserts 'Linux is great.' at line 5 of the file |
| 795 | file.txt.</TD |
| 796 | ></TR |
| 797 | ><TR |
| 798 | ><TD |
| 799 | ALIGN="LEFT" |
| 800 | VALIGN="TOP" |
| 801 | ><TT |
| 802 | CLASS="OPTION" |
| 803 | >/GUI/d</TT |
| 804 | ></TD |
| 805 | ><TD |
| 806 | ALIGN="LEFT" |
| 807 | VALIGN="TOP" |
| 808 | >Delete all lines containing <SPAN |
| 809 | CLASS="QUOTE" |
| 810 | >"GUI"</SPAN |
| 811 | >.</TD |
| 812 | ></TR |
| 813 | ><TR |
| 814 | ><TD |
| 815 | ALIGN="LEFT" |
| 816 | VALIGN="TOP" |
| 817 | ><TT |
| 818 | CLASS="OPTION" |
| 819 | >s/GUI//g</TT |
| 820 | ></TD |
| 821 | ><TD |
| 822 | ALIGN="LEFT" |
| 823 | VALIGN="TOP" |
| 824 | >Delete all instances of <SPAN |
| 825 | CLASS="QUOTE" |
| 826 | >"GUI"</SPAN |
| 827 | >, leaving the |
| 828 | remainder of each line intact.</TD |
| 829 | ></TR |
| 830 | ></TBODY |
| 831 | ></TABLE |
| 832 | ><HR></DIV |
| 833 | ><P |
| 834 | >Substituting a zero-length string for another is equivalent |
| 835 | to deleting that string within a line of input. This leaves the |
| 836 | remainder of the line intact. Applying <TT |
| 837 | CLASS="USERINPUT" |
| 838 | ><B |
| 839 | >s/GUI//</B |
| 840 | ></TT |
| 841 | > |
| 842 | to the line |
| 843 | <TABLE |
| 844 | BORDER="0" |
| 845 | BGCOLOR="#E0E0E0" |
| 846 | WIDTH="100%" |
| 847 | ><TR |
| 848 | ><TD |
| 849 | ><PRE |
| 850 | CLASS="SCREEN" |
| 851 | > <TT |
| 852 | CLASS="USERINPUT" |
| 853 | ><B |
| 854 | >The most important parts of any application are its GUI and sound effects</B |
| 855 | ></TT |
| 856 | ></PRE |
| 857 | ></TD |
| 858 | ></TR |
| 859 | ></TABLE |
| 860 | > |
| 861 | results in |
| 862 | <TABLE |
| 863 | BORDER="0" |
| 864 | BGCOLOR="#E0E0E0" |
| 865 | WIDTH="100%" |
| 866 | ><TR |
| 867 | ><TD |
| 868 | ><PRE |
| 869 | CLASS="SCREEN" |
| 870 | > <TT |
| 871 | CLASS="COMPUTEROUTPUT" |
| 872 | >The most important parts of any application are its and sound effects</TT |
| 873 | ></PRE |
| 874 | ></TD |
| 875 | ></TR |
| 876 | ></TABLE |
| 877 | ></P |
| 878 | ><P |
| 879 | >A backslash forces the <B |
| 880 | CLASS="COMMAND" |
| 881 | >sed</B |
| 882 | > replacement |
| 883 | command to continue on to the next line. This has the effect of |
| 884 | using the <I |
| 885 | CLASS="FIRSTTERM" |
| 886 | >newline</I |
| 887 | > at the end of the first |
| 888 | line as the <I |
| 889 | CLASS="FIRSTTERM" |
| 890 | >replacement string</I |
| 891 | >. |
| 892 | |
| 893 | <TABLE |
| 894 | BORDER="0" |
| 895 | BGCOLOR="#E0E0E0" |
| 896 | WIDTH="100%" |
| 897 | ><TR |
| 898 | ><TD |
| 899 | ><PRE |
| 900 | CLASS="PROGRAMLISTING" |
| 901 | > 1 s/^ */\ |
| 902 | 2 /g</PRE |
| 903 | ></TD |
| 904 | ></TR |
| 905 | ></TABLE |
| 906 | > |
| 907 | |
| 908 | This substitution replaces line-beginning spaces with a |
| 909 | newline. The net result is to replace paragraph indents with a |
| 910 | blank line between paragraphs.</P |
| 911 | ><P |
| 912 | >An address range followed by one or more operations may require |
| 913 | open and closed curly brackets, with appropriate newlines. |
| 914 | <TABLE |
| 915 | BORDER="0" |
| 916 | BGCOLOR="#E0E0E0" |
| 917 | WIDTH="100%" |
| 918 | ><TR |
| 919 | ><TD |
| 920 | ><PRE |
| 921 | CLASS="PROGRAMLISTING" |
| 922 | > 1 /[0-9A-Za-z]/,/^$/{ |
| 923 | 2 /^$/d |
| 924 | 3 }</PRE |
| 925 | ></TD |
| 926 | ></TR |
| 927 | ></TABLE |
| 928 | > |
| 929 | This deletes only the first of each set of consecutive blank |
| 930 | lines. That might be useful for single-spacing a text file, |
| 931 | but retaining the blank line(s) between paragraphs.</P |
| 932 | ><DIV |
| 933 | CLASS="NOTE" |
| 934 | ><TABLE |
| 935 | CLASS="NOTE" |
| 936 | WIDTH="100%" |
| 937 | BORDER="0" |
| 938 | ><TR |
| 939 | ><TD |
| 940 | WIDTH="25" |
| 941 | ALIGN="CENTER" |
| 942 | VALIGN="TOP" |
| 943 | ><IMG |
| 944 | SRC="common/note.png" |
| 945 | HSPACE="5" |
| 946 | ALT="Note"></TD |
| 947 | ><TD |
| 948 | ALIGN="LEFT" |
| 949 | VALIGN="TOP" |
| 950 | ><P |
| 951 | >The usual delimiter that <I |
| 952 | CLASS="FIRSTTERM" |
| 953 | >sed</I |
| 954 | > uses is |
| 955 | <SPAN |
| 956 | CLASS="TOKEN" |
| 957 | >/</SPAN |
| 958 | >. However, <SPAN |
| 959 | CLASS="emphasis" |
| 960 | ><I |
| 961 | CLASS="EMPHASIS" |
| 962 | >sed</I |
| 963 | ></SPAN |
| 964 | > allows other |
| 965 | delimiters, such as <SPAN |
| 966 | CLASS="TOKEN" |
| 967 | >%</SPAN |
| 968 | >. This is useful when |
| 969 | <SPAN |
| 970 | CLASS="TOKEN" |
| 971 | >/</SPAN |
| 972 | > is part of a replacement string, as in a file pathname. |
| 973 | See <A |
| 974 | HREF="loops.html#FINDSTRING" |
| 975 | >Example 11-10</A |
| 976 | > and <A |
| 977 | HREF="filearchiv.html#STRIPC" |
| 978 | >Example 16-32</A |
| 979 | >.</P |
| 980 | ></TD |
| 981 | ></TR |
| 982 | ></TABLE |
| 983 | ></DIV |
| 984 | ><P |
| 985 | ><A |
| 986 | NAME="DOUBLESPACE" |
| 987 | ></A |
| 988 | ></P |
| 989 | ><DIV |
| 990 | CLASS="TIP" |
| 991 | ><TABLE |
| 992 | CLASS="TIP" |
| 993 | WIDTH="100%" |
| 994 | BORDER="0" |
| 995 | ><TR |
| 996 | ><TD |
| 997 | WIDTH="25" |
| 998 | ALIGN="CENTER" |
| 999 | VALIGN="TOP" |
| 1000 | ><IMG |
| 1001 | SRC="common/tip.png" |
| 1002 | HSPACE="5" |
| 1003 | ALT="Tip"></TD |
| 1004 | ><TD |
| 1005 | ALIGN="LEFT" |
| 1006 | VALIGN="TOP" |
| 1007 | ><P |
| 1008 | >A quick way to double-space a text file is <TT |
| 1009 | CLASS="USERINPUT" |
| 1010 | ><B |
| 1011 | >sed G |
| 1012 | filename</B |
| 1013 | ></TT |
| 1014 | >.</P |
| 1015 | ></TD |
| 1016 | ></TR |
| 1017 | ></TABLE |
| 1018 | ></DIV |
| 1019 | ><P |
| 1020 | >For illustrative examples of sed within shell scripts, see: |
| 1021 | <OL |
| 1022 | TYPE="1" |
| 1023 | ><LI |
| 1024 | ><P |
| 1025 | ><A |
| 1026 | HREF="wrapper.html#EX3" |
| 1027 | >Example 36-1</A |
| 1028 | ></P |
| 1029 | ></LI |
| 1030 | ><LI |
| 1031 | ><P |
| 1032 | ><A |
| 1033 | HREF="wrapper.html#EX4" |
| 1034 | >Example 36-2</A |
| 1035 | ></P |
| 1036 | ></LI |
| 1037 | ><LI |
| 1038 | ><P |
| 1039 | ><A |
| 1040 | HREF="moreadv.html#EX57" |
| 1041 | >Example 16-3</A |
| 1042 | ></P |
| 1043 | ></LI |
| 1044 | ><LI |
| 1045 | ><P |
| 1046 | ><A |
| 1047 | HREF="contributed-scripts.html#RN" |
| 1048 | >Example A-2</A |
| 1049 | ></P |
| 1050 | ></LI |
| 1051 | ><LI |
| 1052 | ><P |
| 1053 | ><A |
| 1054 | HREF="textproc.html#GRP" |
| 1055 | >Example 16-17</A |
| 1056 | ></P |
| 1057 | ></LI |
| 1058 | ><LI |
| 1059 | ><P |
| 1060 | ><A |
| 1061 | HREF="textproc.html#COL" |
| 1062 | >Example 16-27</A |
| 1063 | ></P |
| 1064 | ></LI |
| 1065 | ><LI |
| 1066 | ><P |
| 1067 | ><A |
| 1068 | HREF="contributed-scripts.html#BEHEAD" |
| 1069 | >Example A-12</A |
| 1070 | ></P |
| 1071 | ></LI |
| 1072 | ><LI |
| 1073 | ><P |
| 1074 | ><A |
| 1075 | HREF="contributed-scripts.html#TREE" |
| 1076 | >Example A-16</A |
| 1077 | ></P |
| 1078 | ></LI |
| 1079 | ><LI |
| 1080 | ><P |
| 1081 | ><A |
| 1082 | HREF="contributed-scripts.html#TREE2" |
| 1083 | >Example A-17</A |
| 1084 | ></P |
| 1085 | ></LI |
| 1086 | ><LI |
| 1087 | ><P |
| 1088 | ><A |
| 1089 | HREF="filearchiv.html#STRIPC" |
| 1090 | >Example 16-32</A |
| 1091 | ></P |
| 1092 | ></LI |
| 1093 | ><LI |
| 1094 | ><P |
| 1095 | ><A |
| 1096 | HREF="loops.html#FINDSTRING" |
| 1097 | >Example 11-10</A |
| 1098 | ></P |
| 1099 | ></LI |
| 1100 | ><LI |
| 1101 | ><P |
| 1102 | ><A |
| 1103 | HREF="mathc.html#BASE" |
| 1104 | >Example 16-48</A |
| 1105 | ></P |
| 1106 | ></LI |
| 1107 | ><LI |
| 1108 | ><P |
| 1109 | ><A |
| 1110 | HREF="contributed-scripts.html#MAILFORMAT" |
| 1111 | >Example A-1</A |
| 1112 | ></P |
| 1113 | ></LI |
| 1114 | ><LI |
| 1115 | ><P |
| 1116 | ><A |
| 1117 | HREF="textproc.html#RND" |
| 1118 | >Example 16-14</A |
| 1119 | ></P |
| 1120 | ></LI |
| 1121 | ><LI |
| 1122 | ><P |
| 1123 | ><A |
| 1124 | HREF="textproc.html#WF" |
| 1125 | >Example 16-12</A |
| 1126 | ></P |
| 1127 | ></LI |
| 1128 | ><LI |
| 1129 | ><P |
| 1130 | ><A |
| 1131 | HREF="contributed-scripts.html#LIFESLOW" |
| 1132 | >Example A-10</A |
| 1133 | ></P |
| 1134 | ></LI |
| 1135 | ><LI |
| 1136 | ><P |
| 1137 | ><A |
| 1138 | HREF="here-docs.html#SELFDOCUMENT" |
| 1139 | >Example 19-12</A |
| 1140 | ></P |
| 1141 | ></LI |
| 1142 | ><LI |
| 1143 | ><P |
| 1144 | ><A |
| 1145 | HREF="textproc.html#DICTLOOKUP" |
| 1146 | >Example 16-19</A |
| 1147 | ></P |
| 1148 | ></LI |
| 1149 | ><LI |
| 1150 | ><P |
| 1151 | ><A |
| 1152 | HREF="contributed-scripts.html#WHX" |
| 1153 | >Example A-29</A |
| 1154 | ></P |
| 1155 | ></LI |
| 1156 | ><LI |
| 1157 | ><P |
| 1158 | ><A |
| 1159 | HREF="contributed-scripts.html#BASHPODDER" |
| 1160 | >Example A-31</A |
| 1161 | ></P |
| 1162 | ></LI |
| 1163 | ><LI |
| 1164 | ><P |
| 1165 | ><A |
| 1166 | HREF="contributed-scripts.html#TOHTML" |
| 1167 | >Example A-24</A |
| 1168 | ></P |
| 1169 | ></LI |
| 1170 | ><LI |
| 1171 | ><P |
| 1172 | ><A |
| 1173 | HREF="contributed-scripts.html#STOPWATCH" |
| 1174 | >Example A-43</A |
| 1175 | ></P |
| 1176 | ></LI |
| 1177 | ><LI |
| 1178 | ><P |
| 1179 | ><A |
| 1180 | HREF="contributed-scripts.html#SEDAPPEND" |
| 1181 | >Example A-55</A |
| 1182 | ></P |
| 1183 | ></LI |
| 1184 | ></OL |
| 1185 | > |
| 1186 | </P |
| 1187 | ><P |
| 1188 | >For a more extensive treatment of <I |
| 1189 | CLASS="FIRSTTERM" |
| 1190 | >sed</I |
| 1191 | >, |
| 1192 | refer to the <A |
| 1193 | HREF="biblio.html#DGSEDREF" |
| 1194 | >pertinent references</A |
| 1195 | > |
| 1196 | in the <A |
| 1197 | HREF="biblio.html" |
| 1198 | ><I |
| 1199 | >Bibliography</I |
| 1200 | ></A |
| 1201 | >.</P |
| 1202 | ></DIV |
| 1203 | ></DIV |
| 1204 | ><H3 |
| 1205 | CLASS="FOOTNOTES" |
| 1206 | >Notes</H3 |
| 1207 | ><TABLE |
| 1208 | BORDER="0" |
| 1209 | CLASS="FOOTNOTES" |
| 1210 | WIDTH="100%" |
| 1211 | ><TR |
| 1212 | ><TD |
| 1213 | ALIGN="LEFT" |
| 1214 | VALIGN="TOP" |
| 1215 | WIDTH="5%" |
| 1216 | ><A |
| 1217 | NAME="FTN.AEN23174" |
| 1218 | HREF="sedawk.html#AEN23174" |
| 1219 | >[1]</A |
| 1220 | ></TD |
| 1221 | ><TD |
| 1222 | ALIGN="LEFT" |
| 1223 | VALIGN="TOP" |
| 1224 | WIDTH="95%" |
| 1225 | ><P |
| 1226 | ><I |
| 1227 | CLASS="FIRSTTERM" |
| 1228 | >Sed</I |
| 1229 | > executes without |
| 1230 | user intervention.</P |
| 1231 | ></TD |
| 1232 | ></TR |
| 1233 | ><TR |
| 1234 | ><TD |
| 1235 | ALIGN="LEFT" |
| 1236 | VALIGN="TOP" |
| 1237 | WIDTH="5%" |
| 1238 | ><A |
| 1239 | NAME="FTN.AEN23185" |
| 1240 | HREF="sedawk.html#AEN23185" |
| 1241 | >[2]</A |
| 1242 | ></TD |
| 1243 | ><TD |
| 1244 | ALIGN="LEFT" |
| 1245 | VALIGN="TOP" |
| 1246 | WIDTH="95%" |
| 1247 | ><P |
| 1248 | >If no address range is specified, the default |
| 1249 | is <SPAN |
| 1250 | CLASS="emphasis" |
| 1251 | ><I |
| 1252 | CLASS="EMPHASIS" |
| 1253 | >all</I |
| 1254 | ></SPAN |
| 1255 | > lines.</P |
| 1256 | ></TD |
| 1257 | ></TR |
| 1258 | ></TABLE |
| 1259 | ><DIV |
| 1260 | CLASS="NAVFOOTER" |
| 1261 | ><HR |
| 1262 | ALIGN="LEFT" |
| 1263 | WIDTH="100%"><TABLE |
| 1264 | SUMMARY="Footer navigation table" |
| 1265 | WIDTH="100%" |
| 1266 | BORDER="0" |
| 1267 | CELLPADDING="0" |
| 1268 | CELLSPACING="0" |
| 1269 | ><TR |
| 1270 | ><TD |
| 1271 | WIDTH="33%" |
| 1272 | ALIGN="left" |
| 1273 | VALIGN="top" |
| 1274 | ><A |
| 1275 | HREF="refcards.html" |
| 1276 | ACCESSKEY="P" |
| 1277 | >Prev</A |
| 1278 | ></TD |
| 1279 | ><TD |
| 1280 | WIDTH="34%" |
| 1281 | ALIGN="center" |
| 1282 | VALIGN="top" |
| 1283 | ><A |
| 1284 | HREF="index.html" |
| 1285 | ACCESSKEY="H" |
| 1286 | >Home</A |
| 1287 | ></TD |
| 1288 | ><TD |
| 1289 | WIDTH="33%" |
| 1290 | ALIGN="right" |
| 1291 | VALIGN="top" |
| 1292 | ><A |
| 1293 | HREF="awk.html" |
| 1294 | ACCESSKEY="N" |
| 1295 | >Next</A |
| 1296 | ></TD |
| 1297 | ></TR |
| 1298 | ><TR |
| 1299 | ><TD |
| 1300 | WIDTH="33%" |
| 1301 | ALIGN="left" |
| 1302 | VALIGN="top" |
| 1303 | >Reference Cards</TD |
| 1304 | ><TD |
| 1305 | WIDTH="34%" |
| 1306 | ALIGN="center" |
| 1307 | VALIGN="top" |
| 1308 | > </TD |
| 1309 | ><TD |
| 1310 | WIDTH="33%" |
| 1311 | ALIGN="right" |
| 1312 | VALIGN="top" |
| 1313 | >Awk</TD |
| 1314 | ></TR |
| 1315 | ></TABLE |
| 1316 | ></DIV |
| 1317 | ></BODY |
| 1318 | ></HTML |
| 1319 | > |