| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Regular Expressions</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="UP" |
| 14 | TITLE="Advanced Topics" |
| 15 | HREF="part5.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Advanced Topics" |
| 18 | HREF="part5.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Globbing" |
| 21 | HREF="globbingref.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="CHAPTER" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="part5.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | ></TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="globbingref.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="CHAPTER" |
| 92 | ><H1 |
| 93 | ><A |
| 94 | NAME="REGEXP" |
| 95 | ></A |
| 96 | >Chapter 18. Regular Expressions</H1 |
| 97 | ><TABLE |
| 98 | BORDER="0" |
| 99 | WIDTH="100%" |
| 100 | CELLSPACING="0" |
| 101 | CELLPADDING="0" |
| 102 | CLASS="EPIGRAPH" |
| 103 | ><TR |
| 104 | ><TD |
| 105 | WIDTH="45%" |
| 106 | > </TD |
| 107 | ><TD |
| 108 | WIDTH="45%" |
| 109 | ALIGN="LEFT" |
| 110 | VALIGN="TOP" |
| 111 | ><I |
| 112 | ><P |
| 113 | ><I |
| 114 | >. . . the intellectual activity associated with software |
| 115 | development is largely one of gaining insight.</I |
| 116 | ></P |
| 117 | ><P |
| 118 | ><I |
| 119 | >--Stowe Boyd</I |
| 120 | ></P |
| 121 | ></I |
| 122 | ></TD |
| 123 | ></TR |
| 124 | ></TABLE |
| 125 | ><P |
| 126 | ><A |
| 127 | NAME="REGEXREF" |
| 128 | ></A |
| 129 | ></P |
| 130 | ><P |
| 131 | >To fully utilize the power of shell scripting, you need to |
| 132 | master Regular Expressions. Certain commands |
| 133 | and utilities commonly used in scripts, such |
| 134 | as <A |
| 135 | HREF="textproc.html#GREPREF" |
| 136 | >grep</A |
| 137 | >, <A |
| 138 | HREF="moreadv.html#EXPRREF" |
| 139 | >expr</A |
| 140 | >, <A |
| 141 | HREF="sedawk.html#SEDREF" |
| 142 | >sed</A |
| 143 | > |
| 144 | and <A |
| 145 | HREF="awk.html#AWKREF" |
| 146 | >awk</A |
| 147 | >, interpret and use REs. As of |
| 148 | <A |
| 149 | HREF="bashver3.html#BASH3REF" |
| 150 | >version 3</A |
| 151 | >, Bash has acquired its |
| 152 | own <A |
| 153 | HREF="bashver3.html#REGEXMATCHREF" |
| 154 | >RE-match operator</A |
| 155 | >: |
| 156 | <B |
| 157 | CLASS="COMMAND" |
| 158 | >=~</B |
| 159 | >.</P |
| 160 | ><DIV |
| 161 | CLASS="SECT1" |
| 162 | ><H1 |
| 163 | CLASS="SECT1" |
| 164 | ><A |
| 165 | NAME="AEN17129" |
| 166 | ></A |
| 167 | >18.1. A Brief Introduction to Regular Expressions</H1 |
| 168 | ><P |
| 169 | >An expression is a string of characters. Those characters |
| 170 | having an interpretation above and beyond their literal |
| 171 | meaning are called <I |
| 172 | CLASS="FIRSTTERM" |
| 173 | >metacharacters</I |
| 174 | >. |
| 175 | A quote symbol, for example, may denote speech by a person, |
| 176 | <I |
| 177 | CLASS="FIRSTTERM" |
| 178 | >ditto</I |
| 179 | >, or a meta-meaning |
| 180 | |
| 181 | <A |
| 182 | NAME="AEN17134" |
| 183 | HREF="#FTN.AEN17134" |
| 184 | >[1]</A |
| 185 | > |
| 186 | |
| 187 | for the symbols that follow. Regular Expressions are sets |
| 188 | of characters and/or metacharacters that match (or specify) |
| 189 | patterns.</P |
| 190 | ><P |
| 191 | >A Regular Expression contains one or more of the |
| 192 | following:</P |
| 193 | ><UL |
| 194 | ><LI |
| 195 | ><P |
| 196 | ><I |
| 197 | CLASS="FIRSTTERM" |
| 198 | >A character set</I |
| 199 | >. These are the |
| 200 | characters retaining their literal meaning. The |
| 201 | simplest type of Regular Expression consists |
| 202 | <SPAN |
| 203 | CLASS="emphasis" |
| 204 | ><I |
| 205 | CLASS="EMPHASIS" |
| 206 | >only</I |
| 207 | ></SPAN |
| 208 | > of a character set, with no |
| 209 | metacharacters.</P |
| 210 | ></LI |
| 211 | ><LI |
| 212 | ><P |
| 213 | ><A |
| 214 | NAME="ANCHORREF" |
| 215 | ></A |
| 216 | ></P |
| 217 | ><P |
| 218 | ><I |
| 219 | CLASS="FIRSTTERM" |
| 220 | >An anchor</I |
| 221 | >. These designate |
| 222 | (<I |
| 223 | CLASS="FIRSTTERM" |
| 224 | >anchor</I |
| 225 | >) the position in the line of |
| 226 | text that the RE is to match. For example, <SPAN |
| 227 | CLASS="TOKEN" |
| 228 | >^</SPAN |
| 229 | >, |
| 230 | and <SPAN |
| 231 | CLASS="TOKEN" |
| 232 | >$</SPAN |
| 233 | > are anchors.</P |
| 234 | ></LI |
| 235 | ><LI |
| 236 | ><P |
| 237 | ><I |
| 238 | CLASS="FIRSTTERM" |
| 239 | >Modifiers</I |
| 240 | >. These expand or narrow |
| 241 | (<I |
| 242 | CLASS="FIRSTTERM" |
| 243 | >modify</I |
| 244 | >) the range of text the RE is |
| 245 | to match. Modifiers include the asterisk, brackets, and |
| 246 | the backslash.</P |
| 247 | ></LI |
| 248 | ></UL |
| 249 | ><P |
| 250 | >The main uses for Regular Expressions |
| 251 | (<I |
| 252 | CLASS="FIRSTTERM" |
| 253 | >RE</I |
| 254 | >s) are text searches and string |
| 255 | manipulation. An RE <I |
| 256 | CLASS="FIRSTTERM" |
| 257 | >matches</I |
| 258 | > a single |
| 259 | character or a set of characters -- a string or a part of |
| 260 | a string.</P |
| 261 | ><UL |
| 262 | ><LI |
| 263 | ><P |
| 264 | ><A |
| 265 | NAME="ASTERISKREG" |
| 266 | ></A |
| 267 | >The asterisk -- |
| 268 | <SPAN |
| 269 | CLASS="TOKEN" |
| 270 | >*</SPAN |
| 271 | > -- matches any number of |
| 272 | repeats of the character string or RE preceding it, |
| 273 | including <SPAN |
| 274 | CLASS="emphasis" |
| 275 | ><I |
| 276 | CLASS="EMPHASIS" |
| 277 | >zero</I |
| 278 | ></SPAN |
| 279 | > instances.</P |
| 280 | ><P |
| 281 | ><SPAN |
| 282 | CLASS="QUOTE" |
| 283 | >"1133*"</SPAN |
| 284 | > matches <TT |
| 285 | CLASS="REPLACEABLE" |
| 286 | ><I |
| 287 | >11 + |
| 288 | one or more 3's</I |
| 289 | ></TT |
| 290 | >: |
| 291 | <TT |
| 292 | CLASS="REPLACEABLE" |
| 293 | ><I |
| 294 | >113</I |
| 295 | ></TT |
| 296 | >, <TT |
| 297 | CLASS="REPLACEABLE" |
| 298 | ><I |
| 299 | >1133</I |
| 300 | ></TT |
| 301 | >, |
| 302 | <TT |
| 303 | CLASS="REPLACEABLE" |
| 304 | ><I |
| 305 | >1133333</I |
| 306 | ></TT |
| 307 | >, and so forth.</P |
| 308 | ></LI |
| 309 | ><LI |
| 310 | ><P |
| 311 | ><A |
| 312 | NAME="REGEXDOT" |
| 313 | ></A |
| 314 | >The <I |
| 315 | CLASS="FIRSTTERM" |
| 316 | >dot</I |
| 317 | > |
| 318 | -- <SPAN |
| 319 | CLASS="TOKEN" |
| 320 | >.</SPAN |
| 321 | > -- matches |
| 322 | any one character, except a newline. |
| 323 | <A |
| 324 | NAME="AEN17189" |
| 325 | HREF="#FTN.AEN17189" |
| 326 | >[2]</A |
| 327 | > |
| 328 | </P |
| 329 | ><P |
| 330 | ><SPAN |
| 331 | CLASS="QUOTE" |
| 332 | >"13."</SPAN |
| 333 | > matches <TT |
| 334 | CLASS="REPLACEABLE" |
| 335 | ><I |
| 336 | >13 + at |
| 337 | least one of any character (including a |
| 338 | space)</I |
| 339 | ></TT |
| 340 | >: <TT |
| 341 | CLASS="REPLACEABLE" |
| 342 | ><I |
| 343 | >1133</I |
| 344 | ></TT |
| 345 | >, |
| 346 | <TT |
| 347 | CLASS="REPLACEABLE" |
| 348 | ><I |
| 349 | >11333</I |
| 350 | ></TT |
| 351 | >, but not |
| 352 | <TT |
| 353 | CLASS="REPLACEABLE" |
| 354 | ><I |
| 355 | >13</I |
| 356 | ></TT |
| 357 | > (additional character |
| 358 | missing).</P |
| 359 | ><P |
| 360 | >See <A |
| 361 | HREF="textproc.html#CWSOLVER" |
| 362 | >Example 16-18</A |
| 363 | > for a demonstration |
| 364 | of <I |
| 365 | CLASS="FIRSTTERM" |
| 366 | >dot single-character</I |
| 367 | > |
| 368 | matching.</P |
| 369 | ></LI |
| 370 | ><LI |
| 371 | ><P |
| 372 | ><A |
| 373 | NAME="CARETREF" |
| 374 | ></A |
| 375 | >The caret -- <SPAN |
| 376 | CLASS="TOKEN" |
| 377 | >^</SPAN |
| 378 | > |
| 379 | -- matches the beginning of a line, but sometimes, depending |
| 380 | on context, negates the meaning of a set of characters in |
| 381 | an RE.</P |
| 382 | ></LI |
| 383 | ><LI |
| 384 | ><P |
| 385 | ><A |
| 386 | NAME="DOLLARSIGNREF" |
| 387 | ></A |
| 388 | ></P |
| 389 | ><P |
| 390 | >The dollar sign -- <SPAN |
| 391 | CLASS="TOKEN" |
| 392 | >$</SPAN |
| 393 | > -- at the end of an |
| 394 | RE matches the end of a line.</P |
| 395 | ><P |
| 396 | ><SPAN |
| 397 | CLASS="QUOTE" |
| 398 | >"XXX$"</SPAN |
| 399 | > matches <SPAN |
| 400 | CLASS="TOKEN" |
| 401 | >XXX</SPAN |
| 402 | > at the |
| 403 | end of a line.</P |
| 404 | ><P |
| 405 | ><SPAN |
| 406 | CLASS="QUOTE" |
| 407 | >"^$"</SPAN |
| 408 | > matches blank lines.</P |
| 409 | ></LI |
| 410 | ><LI |
| 411 | ><P |
| 412 | ><A |
| 413 | NAME="BRACKETSREF" |
| 414 | ></A |
| 415 | ></P |
| 416 | ><P |
| 417 | >Brackets -- <SPAN |
| 418 | CLASS="TOKEN" |
| 419 | >[...]</SPAN |
| 420 | > -- enclose a set of characters |
| 421 | to match in a single RE.</P |
| 422 | ><P |
| 423 | ><SPAN |
| 424 | CLASS="QUOTE" |
| 425 | >"[xyz]"</SPAN |
| 426 | > matches any one of the characters |
| 427 | <TT |
| 428 | CLASS="REPLACEABLE" |
| 429 | ><I |
| 430 | >x</I |
| 431 | ></TT |
| 432 | >, <TT |
| 433 | CLASS="REPLACEABLE" |
| 434 | ><I |
| 435 | >y</I |
| 436 | ></TT |
| 437 | >, |
| 438 | or <TT |
| 439 | CLASS="REPLACEABLE" |
| 440 | ><I |
| 441 | >z</I |
| 442 | ></TT |
| 443 | >.</P |
| 444 | ><P |
| 445 | ><SPAN |
| 446 | CLASS="QUOTE" |
| 447 | >"[c-n]"</SPAN |
| 448 | > matches any one of the |
| 449 | characters in the range <TT |
| 450 | CLASS="REPLACEABLE" |
| 451 | ><I |
| 452 | >c</I |
| 453 | ></TT |
| 454 | > |
| 455 | to <TT |
| 456 | CLASS="REPLACEABLE" |
| 457 | ><I |
| 458 | >n</I |
| 459 | ></TT |
| 460 | >.</P |
| 461 | ><P |
| 462 | ><SPAN |
| 463 | CLASS="QUOTE" |
| 464 | >"[B-Pk-y]"</SPAN |
| 465 | > matches any one of the |
| 466 | characters in the ranges <TT |
| 467 | CLASS="REPLACEABLE" |
| 468 | ><I |
| 469 | >B</I |
| 470 | ></TT |
| 471 | > |
| 472 | to <TT |
| 473 | CLASS="REPLACEABLE" |
| 474 | ><I |
| 475 | >P</I |
| 476 | ></TT |
| 477 | > |
| 478 | and <TT |
| 479 | CLASS="REPLACEABLE" |
| 480 | ><I |
| 481 | >k</I |
| 482 | ></TT |
| 483 | > to |
| 484 | <TT |
| 485 | CLASS="REPLACEABLE" |
| 486 | ><I |
| 487 | >y</I |
| 488 | ></TT |
| 489 | >.</P |
| 490 | ><P |
| 491 | ><SPAN |
| 492 | CLASS="QUOTE" |
| 493 | >"[a-z0-9]"</SPAN |
| 494 | > matches any single lowercase |
| 495 | letter or any digit.</P |
| 496 | ><P |
| 497 | ><SPAN |
| 498 | CLASS="QUOTE" |
| 499 | >"[^b-d]"</SPAN |
| 500 | > matches any character |
| 501 | <SPAN |
| 502 | CLASS="emphasis" |
| 503 | ><I |
| 504 | CLASS="EMPHASIS" |
| 505 | >except</I |
| 506 | ></SPAN |
| 507 | > those in |
| 508 | the range <TT |
| 509 | CLASS="REPLACEABLE" |
| 510 | ><I |
| 511 | >b</I |
| 512 | ></TT |
| 513 | > to |
| 514 | <TT |
| 515 | CLASS="REPLACEABLE" |
| 516 | ><I |
| 517 | >d</I |
| 518 | ></TT |
| 519 | >. This is an instance of |
| 520 | <SPAN |
| 521 | CLASS="TOKEN" |
| 522 | >^</SPAN |
| 523 | > negating or inverting the meaning |
| 524 | of the following RE (taking on a role similar to |
| 525 | <SPAN |
| 526 | CLASS="TOKEN" |
| 527 | >!</SPAN |
| 528 | > in a different context).</P |
| 529 | ><P |
| 530 | >Combined sequences of bracketed characters match |
| 531 | common word patterns. <SPAN |
| 532 | CLASS="QUOTE" |
| 533 | >"[Yy][Ee][Ss]"</SPAN |
| 534 | > matches |
| 535 | <TT |
| 536 | CLASS="REPLACEABLE" |
| 537 | ><I |
| 538 | >yes</I |
| 539 | ></TT |
| 540 | >, <TT |
| 541 | CLASS="REPLACEABLE" |
| 542 | ><I |
| 543 | >Yes</I |
| 544 | ></TT |
| 545 | >, |
| 546 | <TT |
| 547 | CLASS="REPLACEABLE" |
| 548 | ><I |
| 549 | >YES</I |
| 550 | ></TT |
| 551 | >, <TT |
| 552 | CLASS="REPLACEABLE" |
| 553 | ><I |
| 554 | >yEs</I |
| 555 | ></TT |
| 556 | >, |
| 557 | and so forth. |
| 558 | <SPAN |
| 559 | CLASS="QUOTE" |
| 560 | >"[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]"</SPAN |
| 561 | > |
| 562 | matches any Social Security number.</P |
| 563 | ></LI |
| 564 | ><LI |
| 565 | ><P |
| 566 | ><A |
| 567 | NAME="REGEXBS" |
| 568 | ></A |
| 569 | ></P |
| 570 | ><P |
| 571 | >The backslash -- <SPAN |
| 572 | CLASS="TOKEN" |
| 573 | >\</SPAN |
| 574 | > -- <A |
| 575 | HREF="escapingsection.html#ESCP" |
| 576 | >escapes</A |
| 577 | > a special character, which |
| 578 | means that character gets interpreted literally (and is |
| 579 | therefore no longer <I |
| 580 | CLASS="FIRSTTERM" |
| 581 | >special</I |
| 582 | >).</P |
| 583 | ><P |
| 584 | >A <SPAN |
| 585 | CLASS="QUOTE" |
| 586 | >"\$"</SPAN |
| 587 | > reverts back to its |
| 588 | literal meaning of <SPAN |
| 589 | CLASS="QUOTE" |
| 590 | >"$"</SPAN |
| 591 | >, rather than its |
| 592 | RE meaning of end-of-line. Likewise a <SPAN |
| 593 | CLASS="QUOTE" |
| 594 | >"\\"</SPAN |
| 595 | > |
| 596 | has the literal meaning of <SPAN |
| 597 | CLASS="QUOTE" |
| 598 | >"\"</SPAN |
| 599 | >.</P |
| 600 | ></LI |
| 601 | ><LI |
| 602 | ><P |
| 603 | ><A |
| 604 | NAME="ANGLEBRAC" |
| 605 | ></A |
| 606 | ></P |
| 607 | ><P |
| 608 | ><A |
| 609 | HREF="escapingsection.html#ESCP" |
| 610 | >Escaped</A |
| 611 | > <SPAN |
| 612 | CLASS="QUOTE" |
| 613 | >"angle |
| 614 | brackets"</SPAN |
| 615 | > -- <SPAN |
| 616 | CLASS="TOKEN" |
| 617 | >\<...\></SPAN |
| 618 | > -- mark word |
| 619 | boundaries.</P |
| 620 | ><P |
| 621 | >The angle brackets must be escaped, since otherwise |
| 622 | they have only their literal character meaning.</P |
| 623 | ><P |
| 624 | ><SPAN |
| 625 | CLASS="QUOTE" |
| 626 | >"\<the\>"</SPAN |
| 627 | > matches the word |
| 628 | <SPAN |
| 629 | CLASS="QUOTE" |
| 630 | >"the,"</SPAN |
| 631 | > but not the words <SPAN |
| 632 | CLASS="QUOTE" |
| 633 | >"them,"</SPAN |
| 634 | > |
| 635 | <SPAN |
| 636 | CLASS="QUOTE" |
| 637 | >"there,"</SPAN |
| 638 | > <SPAN |
| 639 | CLASS="QUOTE" |
| 640 | >"other,"</SPAN |
| 641 | > etc.</P |
| 642 | ><P |
| 643 | > <TABLE |
| 644 | BORDER="0" |
| 645 | BGCOLOR="#E0E0E0" |
| 646 | WIDTH="90%" |
| 647 | ><TR |
| 648 | ><TD |
| 649 | ><PRE |
| 650 | CLASS="SCREEN" |
| 651 | > <TT |
| 652 | CLASS="PROMPT" |
| 653 | >bash$ </TT |
| 654 | ><TT |
| 655 | CLASS="USERINPUT" |
| 656 | ><B |
| 657 | >cat textfile</B |
| 658 | ></TT |
| 659 | > |
| 660 | <TT |
| 661 | CLASS="COMPUTEROUTPUT" |
| 662 | >This is line 1, of which there is only one instance. |
| 663 | This is the only instance of line 2. |
| 664 | This is line 3, another line. |
| 665 | This is line 4.</TT |
| 666 | > |
| 667 | |
| 668 | |
| 669 | <TT |
| 670 | CLASS="PROMPT" |
| 671 | >bash$ </TT |
| 672 | ><TT |
| 673 | CLASS="USERINPUT" |
| 674 | ><B |
| 675 | >grep 'the' textfile</B |
| 676 | ></TT |
| 677 | > |
| 678 | <TT |
| 679 | CLASS="COMPUTEROUTPUT" |
| 680 | >This is line 1, of which there is only one instance. |
| 681 | This is the only instance of line 2. |
| 682 | This is line 3, another line.</TT |
| 683 | > |
| 684 | |
| 685 | |
| 686 | <TT |
| 687 | CLASS="PROMPT" |
| 688 | >bash$ </TT |
| 689 | ><TT |
| 690 | CLASS="USERINPUT" |
| 691 | ><B |
| 692 | >grep '\<the\>' textfile</B |
| 693 | ></TT |
| 694 | > |
| 695 | <TT |
| 696 | CLASS="COMPUTEROUTPUT" |
| 697 | >This is the only instance of line 2.</TT |
| 698 | > |
| 699 | </PRE |
| 700 | ></TD |
| 701 | ></TR |
| 702 | ></TABLE |
| 703 | > |
| 704 | </P |
| 705 | ></LI |
| 706 | ></UL |
| 707 | ><TABLE |
| 708 | CLASS="SIDEBAR" |
| 709 | BORDER="1" |
| 710 | CELLPADDING="5" |
| 711 | ><TR |
| 712 | ><TD |
| 713 | ><DIV |
| 714 | CLASS="SIDEBAR" |
| 715 | ><A |
| 716 | NAME="AEN17316" |
| 717 | ></A |
| 718 | ><P |
| 719 | >The only way to be certain that a particular RE works is to |
| 720 | test it.</P |
| 721 | ><P |
| 722 | ><TABLE |
| 723 | BORDER="0" |
| 724 | BGCOLOR="#E0E0E0" |
| 725 | WIDTH="100%" |
| 726 | ><TR |
| 727 | ><TD |
| 728 | ><PRE |
| 729 | CLASS="PROGRAMLISTING" |
| 730 | > 1 TEST FILE: tstfile # No match. |
| 731 | 2 # No match. |
| 732 | 3 Run grep "1133*" on this file. # Match. |
| 733 | 4 # No match. |
| 734 | 5 # No match. |
| 735 | 6 This line contains the number 113. # Match. |
| 736 | 7 This line contains the number 13. # No match. |
| 737 | 8 This line contains the number 133. # No match. |
| 738 | 9 This line contains the number 1133. # Match. |
| 739 | 10 This line contains the number 113312. # Match. |
| 740 | 11 This line contains the number 1112. # No match. |
| 741 | 12 This line contains the number 113312312. # Match. |
| 742 | 13 This line contains no numbers at all. # No match.</PRE |
| 743 | ></TD |
| 744 | ></TR |
| 745 | ></TABLE |
| 746 | ></P |
| 747 | ><TABLE |
| 748 | BORDER="0" |
| 749 | BGCOLOR="#E0E0E0" |
| 750 | WIDTH="100%" |
| 751 | ><TR |
| 752 | ><TD |
| 753 | ><PRE |
| 754 | CLASS="SCREEN" |
| 755 | > <TT |
| 756 | CLASS="PROMPT" |
| 757 | >bash$ </TT |
| 758 | ><TT |
| 759 | CLASS="USERINPUT" |
| 760 | ><B |
| 761 | >grep "1133*" tstfile</B |
| 762 | ></TT |
| 763 | > |
| 764 | <TT |
| 765 | CLASS="COMPUTEROUTPUT" |
| 766 | >Run grep "1133*" on this file. # Match. |
| 767 | This line contains the number 113. # Match. |
| 768 | This line contains the number 1133. # Match. |
| 769 | This line contains the number 113312. # Match. |
| 770 | This line contains the number 113312312. # Match.</TT |
| 771 | > |
| 772 | </PRE |
| 773 | ></TD |
| 774 | ></TR |
| 775 | ></TABLE |
| 776 | ></DIV |
| 777 | ></TD |
| 778 | ></TR |
| 779 | ></TABLE |
| 780 | ><UL |
| 781 | ><LI |
| 782 | STYLE="list-style-type: square" |
| 783 | ><DIV |
| 784 | CLASS="FORMALPARA" |
| 785 | ><P |
| 786 | ><B |
| 787 | ><A |
| 788 | NAME="EXTREGEX" |
| 789 | ></A |
| 790 | >Extended REs. </B |
| 791 | >Additional metacharacters added to the basic set. Used |
| 792 | in <A |
| 793 | HREF="textproc.html#EGREPREF" |
| 794 | >egrep</A |
| 795 | >, |
| 796 | <A |
| 797 | HREF="awk.html#AWKREF" |
| 798 | >awk</A |
| 799 | >, and <A |
| 800 | HREF="wrapper.html#PERLREF" |
| 801 | >Perl</A |
| 802 | >.</P |
| 803 | ></DIV |
| 804 | ></LI |
| 805 | ><LI |
| 806 | ><P |
| 807 | ><A |
| 808 | NAME="QUEXREGEX" |
| 809 | ></A |
| 810 | ></P |
| 811 | ><P |
| 812 | >The question mark -- <SPAN |
| 813 | CLASS="TOKEN" |
| 814 | >?</SPAN |
| 815 | > -- matches zero or |
| 816 | one of the previous RE. It is generally used for matching |
| 817 | single characters.</P |
| 818 | ></LI |
| 819 | ><LI |
| 820 | ><P |
| 821 | ><A |
| 822 | NAME="PLUSREF" |
| 823 | ></A |
| 824 | ></P |
| 825 | ><P |
| 826 | >The plus -- <SPAN |
| 827 | CLASS="TOKEN" |
| 828 | >+</SPAN |
| 829 | > -- matches one or more of the |
| 830 | previous RE. It serves a role similar to the <SPAN |
| 831 | CLASS="TOKEN" |
| 832 | >*</SPAN |
| 833 | >, but |
| 834 | does <SPAN |
| 835 | CLASS="emphasis" |
| 836 | ><I |
| 837 | CLASS="EMPHASIS" |
| 838 | >not</I |
| 839 | ></SPAN |
| 840 | > match zero occurrences.</P |
| 841 | ><P |
| 842 | ><TABLE |
| 843 | BORDER="0" |
| 844 | BGCOLOR="#E0E0E0" |
| 845 | WIDTH="90%" |
| 846 | ><TR |
| 847 | ><TD |
| 848 | ><PRE |
| 849 | CLASS="PROGRAMLISTING" |
| 850 | > 1 # GNU versions of sed and awk can use "+", |
| 851 | 2 # but it needs to be escaped. |
| 852 | 3 |
| 853 | 4 echo a111b | sed -ne '/a1\+b/p' |
| 854 | 5 echo a111b | grep 'a1\+b' |
| 855 | 6 echo a111b | gawk '/a1+b/' |
| 856 | 7 # All of above are equivalent. |
| 857 | 8 |
| 858 | 9 # Thanks, S.C.</PRE |
| 859 | ></TD |
| 860 | ></TR |
| 861 | ></TABLE |
| 862 | ></P |
| 863 | ><P |
| 864 | ><A |
| 865 | NAME="ESCPCB" |
| 866 | ></A |
| 867 | ></P |
| 868 | ></LI |
| 869 | ><LI |
| 870 | ><P |
| 871 | ><A |
| 872 | HREF="escapingsection.html#ESCP" |
| 873 | >Escaped</A |
| 874 | > <SPAN |
| 875 | CLASS="QUOTE" |
| 876 | >"curly |
| 877 | brackets"</SPAN |
| 878 | > -- <SPAN |
| 879 | CLASS="TOKEN" |
| 880 | >\{ \}</SPAN |
| 881 | > -- indicate the number |
| 882 | of occurrences of a preceding RE to match.</P |
| 883 | ><P |
| 884 | >It is necessary to escape the curly brackets since |
| 885 | they have only their literal character meaning |
| 886 | otherwise. This usage is technically not part of the basic |
| 887 | RE set.</P |
| 888 | ><P |
| 889 | ><SPAN |
| 890 | CLASS="QUOTE" |
| 891 | >"[0-9]\{5\}"</SPAN |
| 892 | > matches exactly five digits |
| 893 | (characters in the range of 0 to 9).</P |
| 894 | ><DIV |
| 895 | CLASS="NOTE" |
| 896 | ><TABLE |
| 897 | CLASS="NOTE" |
| 898 | WIDTH="90%" |
| 899 | BORDER="0" |
| 900 | ><TR |
| 901 | ><TD |
| 902 | WIDTH="25" |
| 903 | ALIGN="CENTER" |
| 904 | VALIGN="TOP" |
| 905 | ><IMG |
| 906 | SRC="common/note.png" |
| 907 | HSPACE="5" |
| 908 | ALT="Note"></TD |
| 909 | ><TD |
| 910 | ALIGN="LEFT" |
| 911 | VALIGN="TOP" |
| 912 | ><P |
| 913 | >Curly brackets are not available as an RE in the |
| 914 | <SPAN |
| 915 | CLASS="QUOTE" |
| 916 | >"classic"</SPAN |
| 917 | > (non-POSIX compliant) version |
| 918 | of <A |
| 919 | HREF="awk.html#AWKREF" |
| 920 | >awk</A |
| 921 | >. |
| 922 | <A |
| 923 | NAME="GNUGAWK" |
| 924 | ></A |
| 925 | >However, the GNU extended version |
| 926 | of <I |
| 927 | CLASS="FIRSTTERM" |
| 928 | >awk</I |
| 929 | >, <B |
| 930 | CLASS="COMMAND" |
| 931 | >gawk</B |
| 932 | >, |
| 933 | has the <TT |
| 934 | CLASS="OPTION" |
| 935 | >--re-interval</TT |
| 936 | > option that permits |
| 937 | them (without being escaped).</P |
| 938 | ><P |
| 939 | > <TABLE |
| 940 | BORDER="0" |
| 941 | BGCOLOR="#E0E0E0" |
| 942 | WIDTH="90%" |
| 943 | ><TR |
| 944 | ><TD |
| 945 | ><PRE |
| 946 | CLASS="SCREEN" |
| 947 | > <TT |
| 948 | CLASS="PROMPT" |
| 949 | >bash$ </TT |
| 950 | ><TT |
| 951 | CLASS="USERINPUT" |
| 952 | ><B |
| 953 | >echo 2222 | gawk --re-interval '/2{3}/'</B |
| 954 | ></TT |
| 955 | > |
| 956 | <TT |
| 957 | CLASS="COMPUTEROUTPUT" |
| 958 | >2222</TT |
| 959 | > |
| 960 | </PRE |
| 961 | ></TD |
| 962 | ></TR |
| 963 | ></TABLE |
| 964 | > |
| 965 | </P |
| 966 | ><P |
| 967 | ><B |
| 968 | CLASS="COMMAND" |
| 969 | >Perl</B |
| 970 | > and some |
| 971 | <B |
| 972 | CLASS="COMMAND" |
| 973 | >egrep</B |
| 974 | > versions do not require escaping |
| 975 | the curly brackets.</P |
| 976 | ></TD |
| 977 | ></TR |
| 978 | ></TABLE |
| 979 | ></DIV |
| 980 | ></LI |
| 981 | ><LI |
| 982 | ><P |
| 983 | ><A |
| 984 | NAME="PARENGRPS" |
| 985 | ></A |
| 986 | ></P |
| 987 | ><P |
| 988 | >Parentheses -- <B |
| 989 | CLASS="COMMAND" |
| 990 | >( )</B |
| 991 | > -- enclose a group of |
| 992 | REs. They are useful with the following |
| 993 | <SPAN |
| 994 | CLASS="QUOTE" |
| 995 | >"<SPAN |
| 996 | CLASS="TOKEN" |
| 997 | >|</SPAN |
| 998 | >"</SPAN |
| 999 | > operator and in <A |
| 1000 | HREF="manipulatingvars.html#EXPRPAREN" |
| 1001 | >substring extraction</A |
| 1002 | > using <A |
| 1003 | HREF="moreadv.html#EXPRREF" |
| 1004 | >expr</A |
| 1005 | >.</P |
| 1006 | ></LI |
| 1007 | ><LI |
| 1008 | ><P |
| 1009 | >The -- <B |
| 1010 | CLASS="COMMAND" |
| 1011 | >|</B |
| 1012 | > -- <SPAN |
| 1013 | CLASS="QUOTE" |
| 1014 | >"or"</SPAN |
| 1015 | > RE operator |
| 1016 | matches any of a set of alternate characters.</P |
| 1017 | ><P |
| 1018 | > <TABLE |
| 1019 | BORDER="0" |
| 1020 | BGCOLOR="#E0E0E0" |
| 1021 | WIDTH="90%" |
| 1022 | ><TR |
| 1023 | ><TD |
| 1024 | ><PRE |
| 1025 | CLASS="SCREEN" |
| 1026 | > <TT |
| 1027 | CLASS="PROMPT" |
| 1028 | >bash$ </TT |
| 1029 | ><TT |
| 1030 | CLASS="USERINPUT" |
| 1031 | ><B |
| 1032 | >egrep 're(a|e)d' misc.txt</B |
| 1033 | ></TT |
| 1034 | > |
| 1035 | <TT |
| 1036 | CLASS="COMPUTEROUTPUT" |
| 1037 | >People who read seem to be better informed than those who do not. |
| 1038 | The clarinet produces sound by the vibration of its reed.</TT |
| 1039 | > |
| 1040 | </PRE |
| 1041 | ></TD |
| 1042 | ></TR |
| 1043 | ></TABLE |
| 1044 | > |
| 1045 | </P |
| 1046 | ></LI |
| 1047 | ></UL |
| 1048 | ><DIV |
| 1049 | CLASS="NOTE" |
| 1050 | ><TABLE |
| 1051 | CLASS="NOTE" |
| 1052 | WIDTH="100%" |
| 1053 | BORDER="0" |
| 1054 | ><TR |
| 1055 | ><TD |
| 1056 | WIDTH="25" |
| 1057 | ALIGN="CENTER" |
| 1058 | VALIGN="TOP" |
| 1059 | ><IMG |
| 1060 | SRC="common/note.png" |
| 1061 | HSPACE="5" |
| 1062 | ALT="Note"></TD |
| 1063 | ><TD |
| 1064 | ALIGN="LEFT" |
| 1065 | VALIGN="TOP" |
| 1066 | ><P |
| 1067 | >Some versions of <B |
| 1068 | CLASS="COMMAND" |
| 1069 | >sed</B |
| 1070 | >, |
| 1071 | <B |
| 1072 | CLASS="COMMAND" |
| 1073 | >ed</B |
| 1074 | >, and <B |
| 1075 | CLASS="COMMAND" |
| 1076 | >ex</B |
| 1077 | > support |
| 1078 | escaped versions of the extended Regular Expressions |
| 1079 | described above, as do the GNU utilities.</P |
| 1080 | ></TD |
| 1081 | ></TR |
| 1082 | ></TABLE |
| 1083 | ></DIV |
| 1084 | ><UL |
| 1085 | ><LI |
| 1086 | STYLE="list-style-type: square" |
| 1087 | ><DIV |
| 1088 | CLASS="FORMALPARA" |
| 1089 | ><P |
| 1090 | ><B |
| 1091 | ><A |
| 1092 | NAME="POSIXREF" |
| 1093 | ></A |
| 1094 | >POSIX Character Classes. </B |
| 1095 | ><TT |
| 1096 | CLASS="USERINPUT" |
| 1097 | ><B |
| 1098 | >[:class:]</B |
| 1099 | ></TT |
| 1100 | ></P |
| 1101 | ></DIV |
| 1102 | ><P |
| 1103 | >This is an alternate method of specifying a range of |
| 1104 | characters to match.</P |
| 1105 | ></LI |
| 1106 | ><LI |
| 1107 | ><P |
| 1108 | ><TT |
| 1109 | CLASS="USERINPUT" |
| 1110 | ><B |
| 1111 | >[:alnum:]</B |
| 1112 | ></TT |
| 1113 | > matches alphabetic or |
| 1114 | numeric characters. This is equivalent to |
| 1115 | <TT |
| 1116 | CLASS="USERINPUT" |
| 1117 | ><B |
| 1118 | >A-Za-z0-9</B |
| 1119 | ></TT |
| 1120 | >.</P |
| 1121 | ></LI |
| 1122 | ><LI |
| 1123 | ><P |
| 1124 | ><TT |
| 1125 | CLASS="USERINPUT" |
| 1126 | ><B |
| 1127 | >[:alpha:]</B |
| 1128 | ></TT |
| 1129 | > matches alphabetic |
| 1130 | characters. This is equivalent to |
| 1131 | <TT |
| 1132 | CLASS="USERINPUT" |
| 1133 | ><B |
| 1134 | >A-Za-z</B |
| 1135 | ></TT |
| 1136 | >.</P |
| 1137 | ></LI |
| 1138 | ><LI |
| 1139 | ><P |
| 1140 | ><TT |
| 1141 | CLASS="USERINPUT" |
| 1142 | ><B |
| 1143 | >[:blank:]</B |
| 1144 | ></TT |
| 1145 | > matches a space or a |
| 1146 | tab.</P |
| 1147 | ></LI |
| 1148 | ><LI |
| 1149 | ><P |
| 1150 | ><TT |
| 1151 | CLASS="USERINPUT" |
| 1152 | ><B |
| 1153 | >[:cntrl:]</B |
| 1154 | ></TT |
| 1155 | > matches control |
| 1156 | characters.</P |
| 1157 | ></LI |
| 1158 | ><LI |
| 1159 | ><P |
| 1160 | ><TT |
| 1161 | CLASS="USERINPUT" |
| 1162 | ><B |
| 1163 | >[:digit:]</B |
| 1164 | ></TT |
| 1165 | > matches (decimal) |
| 1166 | digits. This is equivalent to |
| 1167 | <TT |
| 1168 | CLASS="USERINPUT" |
| 1169 | ><B |
| 1170 | >0-9</B |
| 1171 | ></TT |
| 1172 | >.</P |
| 1173 | ></LI |
| 1174 | ><LI |
| 1175 | ><P |
| 1176 | ><TT |
| 1177 | CLASS="USERINPUT" |
| 1178 | ><B |
| 1179 | >[:graph:]</B |
| 1180 | ></TT |
| 1181 | > (graphic printable |
| 1182 | characters). Matches characters in the range of <A |
| 1183 | HREF="special-chars.html#ASCIIDEF" |
| 1184 | >ASCII</A |
| 1185 | > 33 - 126. This is |
| 1186 | the same as <TT |
| 1187 | CLASS="USERINPUT" |
| 1188 | ><B |
| 1189 | >[:print:]</B |
| 1190 | ></TT |
| 1191 | >, below, |
| 1192 | but excluding the space character.</P |
| 1193 | ></LI |
| 1194 | ><LI |
| 1195 | ><P |
| 1196 | ><TT |
| 1197 | CLASS="USERINPUT" |
| 1198 | ><B |
| 1199 | >[:lower:]</B |
| 1200 | ></TT |
| 1201 | > matches lowercase |
| 1202 | alphabetic characters. This is equivalent to |
| 1203 | <TT |
| 1204 | CLASS="USERINPUT" |
| 1205 | ><B |
| 1206 | >a-z</B |
| 1207 | ></TT |
| 1208 | >.</P |
| 1209 | ></LI |
| 1210 | ><LI |
| 1211 | ><P |
| 1212 | ><TT |
| 1213 | CLASS="USERINPUT" |
| 1214 | ><B |
| 1215 | >[:print:]</B |
| 1216 | ></TT |
| 1217 | > (printable |
| 1218 | characters). Matches characters in the range of ASCII 32 - |
| 1219 | 126. This is the same as <TT |
| 1220 | CLASS="USERINPUT" |
| 1221 | ><B |
| 1222 | >[:graph:]</B |
| 1223 | ></TT |
| 1224 | >, |
| 1225 | above, but adding the space character.</P |
| 1226 | ></LI |
| 1227 | ><LI |
| 1228 | ><P |
| 1229 | ><A |
| 1230 | NAME="WSPOSIX" |
| 1231 | ></A |
| 1232 | ><TT |
| 1233 | CLASS="USERINPUT" |
| 1234 | ><B |
| 1235 | >[:space:]</B |
| 1236 | ></TT |
| 1237 | > |
| 1238 | matches whitespace characters (space and horizontal |
| 1239 | tab).</P |
| 1240 | ></LI |
| 1241 | ><LI |
| 1242 | ><P |
| 1243 | ><TT |
| 1244 | CLASS="USERINPUT" |
| 1245 | ><B |
| 1246 | >[:upper:]</B |
| 1247 | ></TT |
| 1248 | > matches uppercase |
| 1249 | alphabetic characters. This is equivalent to |
| 1250 | <TT |
| 1251 | CLASS="USERINPUT" |
| 1252 | ><B |
| 1253 | >A-Z</B |
| 1254 | ></TT |
| 1255 | >.</P |
| 1256 | ></LI |
| 1257 | ><LI |
| 1258 | ><P |
| 1259 | ><TT |
| 1260 | CLASS="USERINPUT" |
| 1261 | ><B |
| 1262 | >[:xdigit:]</B |
| 1263 | ></TT |
| 1264 | > matches hexadecimal |
| 1265 | digits. This is equivalent to |
| 1266 | <TT |
| 1267 | CLASS="USERINPUT" |
| 1268 | ><B |
| 1269 | >0-9A-Fa-f</B |
| 1270 | ></TT |
| 1271 | >.</P |
| 1272 | ><DIV |
| 1273 | CLASS="IMPORTANT" |
| 1274 | ><TABLE |
| 1275 | CLASS="IMPORTANT" |
| 1276 | WIDTH="90%" |
| 1277 | BORDER="0" |
| 1278 | ><TR |
| 1279 | ><TD |
| 1280 | WIDTH="25" |
| 1281 | ALIGN="CENTER" |
| 1282 | VALIGN="TOP" |
| 1283 | ><IMG |
| 1284 | SRC="common/important.png" |
| 1285 | HSPACE="5" |
| 1286 | ALT="Important"></TD |
| 1287 | ><TD |
| 1288 | ALIGN="LEFT" |
| 1289 | VALIGN="TOP" |
| 1290 | ><P |
| 1291 | >POSIX character classes generally require quoting |
| 1292 | or <A |
| 1293 | HREF="tests.html#DBLBRACKETS" |
| 1294 | >double brackets</A |
| 1295 | > |
| 1296 | ([[ ]]).</P |
| 1297 | ></TD |
| 1298 | ></TR |
| 1299 | ></TABLE |
| 1300 | ></DIV |
| 1301 | ><P |
| 1302 | > <TABLE |
| 1303 | BORDER="0" |
| 1304 | BGCOLOR="#E0E0E0" |
| 1305 | WIDTH="90%" |
| 1306 | ><TR |
| 1307 | ><TD |
| 1308 | ><PRE |
| 1309 | CLASS="SCREEN" |
| 1310 | > <TT |
| 1311 | CLASS="PROMPT" |
| 1312 | >bash$ </TT |
| 1313 | ><TT |
| 1314 | CLASS="USERINPUT" |
| 1315 | ><B |
| 1316 | >grep [[:digit:]] test.file</B |
| 1317 | ></TT |
| 1318 | > |
| 1319 | <TT |
| 1320 | CLASS="COMPUTEROUTPUT" |
| 1321 | >abc=723</TT |
| 1322 | > |
| 1323 | </PRE |
| 1324 | ></TD |
| 1325 | ></TR |
| 1326 | ></TABLE |
| 1327 | > |
| 1328 | </P |
| 1329 | ><P |
| 1330 | ><TABLE |
| 1331 | BORDER="0" |
| 1332 | BGCOLOR="#E0E0E0" |
| 1333 | WIDTH="90%" |
| 1334 | ><TR |
| 1335 | ><TD |
| 1336 | ><PRE |
| 1337 | CLASS="PROGRAMLISTING" |
| 1338 | > 1 # ... |
| 1339 | 2 if [[ $arow =~ [[:digit:]] ]] # Numerical input? |
| 1340 | 3 then # POSIX char class |
| 1341 | 4 if [[ $acol =~ [[:alpha:]] ]] # Number followed by a letter? Illegal! |
| 1342 | 5 # ... |
| 1343 | 6 # From ktour.sh example script.</PRE |
| 1344 | ></TD |
| 1345 | ></TR |
| 1346 | ></TABLE |
| 1347 | > |
| 1348 | </P |
| 1349 | ><P |
| 1350 | >These character classes may even be used with <A |
| 1351 | HREF="globbingref.html" |
| 1352 | >globbing</A |
| 1353 | >, to a limited |
| 1354 | extent.</P |
| 1355 | ><P |
| 1356 | > <TABLE |
| 1357 | BORDER="0" |
| 1358 | BGCOLOR="#E0E0E0" |
| 1359 | WIDTH="90%" |
| 1360 | ><TR |
| 1361 | ><TD |
| 1362 | ><PRE |
| 1363 | CLASS="SCREEN" |
| 1364 | > <TT |
| 1365 | CLASS="PROMPT" |
| 1366 | >bash$ </TT |
| 1367 | ><TT |
| 1368 | CLASS="USERINPUT" |
| 1369 | ><B |
| 1370 | >ls -l ?[[:digit:]][[:digit:]]?</B |
| 1371 | ></TT |
| 1372 | > |
| 1373 | <TT |
| 1374 | CLASS="COMPUTEROUTPUT" |
| 1375 | >-rw-rw-r-- 1 bozo bozo 0 Aug 21 14:47 a33b</TT |
| 1376 | > |
| 1377 | </PRE |
| 1378 | ></TD |
| 1379 | ></TR |
| 1380 | ></TABLE |
| 1381 | > |
| 1382 | </P |
| 1383 | ><P |
| 1384 | >POSIX character classes are used in |
| 1385 | <A |
| 1386 | HREF="textproc.html#EX49" |
| 1387 | >Example 16-21</A |
| 1388 | > and <A |
| 1389 | HREF="textproc.html#LOWERCASE" |
| 1390 | >Example 16-22</A |
| 1391 | >.</P |
| 1392 | ></LI |
| 1393 | ></UL |
| 1394 | ><P |
| 1395 | ><A |
| 1396 | HREF="sedawk.html#SEDREF" |
| 1397 | >Sed</A |
| 1398 | >, <A |
| 1399 | HREF="awk.html#AWKREF" |
| 1400 | >awk</A |
| 1401 | >, and <A |
| 1402 | HREF="wrapper.html#PERLREF" |
| 1403 | >Perl</A |
| 1404 | >, used as filters in scripts, take |
| 1405 | REs as arguments when "sifting" or transforming files or I/O |
| 1406 | streams. See <A |
| 1407 | HREF="contributed-scripts.html#BEHEAD" |
| 1408 | >Example A-12</A |
| 1409 | > and <A |
| 1410 | HREF="contributed-scripts.html#TREE" |
| 1411 | >Example A-16</A |
| 1412 | > |
| 1413 | for illustrations of this.</P |
| 1414 | ><P |
| 1415 | >The standard reference on this complex topic is Friedl's |
| 1416 | <I |
| 1417 | CLASS="CITETITLE" |
| 1418 | >Mastering Regular |
| 1419 | Expressions</I |
| 1420 | >. <I |
| 1421 | CLASS="CITETITLE" |
| 1422 | >Sed & |
| 1423 | Awk</I |
| 1424 | >, by Dougherty and Robbins, also gives a very |
| 1425 | lucid treatment of REs. See the <A |
| 1426 | HREF="biblio.html" |
| 1427 | ><I |
| 1428 | >Bibliography</I |
| 1429 | ></A |
| 1430 | > for |
| 1431 | more information on these books.</P |
| 1432 | ></DIV |
| 1433 | ></DIV |
| 1434 | ><H3 |
| 1435 | CLASS="FOOTNOTES" |
| 1436 | >Notes</H3 |
| 1437 | ><TABLE |
| 1438 | BORDER="0" |
| 1439 | CLASS="FOOTNOTES" |
| 1440 | WIDTH="100%" |
| 1441 | ><TR |
| 1442 | ><TD |
| 1443 | ALIGN="LEFT" |
| 1444 | VALIGN="TOP" |
| 1445 | WIDTH="5%" |
| 1446 | ><A |
| 1447 | NAME="FTN.AEN17134" |
| 1448 | HREF="regexp.html#AEN17134" |
| 1449 | >[1]</A |
| 1450 | ></TD |
| 1451 | ><TD |
| 1452 | ALIGN="LEFT" |
| 1453 | VALIGN="TOP" |
| 1454 | WIDTH="95%" |
| 1455 | ><P |
| 1456 | ><A |
| 1457 | NAME="METAMEANINGREF" |
| 1458 | ></A |
| 1459 | >A |
| 1460 | <I |
| 1461 | CLASS="FIRSTTERM" |
| 1462 | >meta-meaning</I |
| 1463 | > is the meaning of a |
| 1464 | term or expression on a higher level of abstraction. For |
| 1465 | example, the <I |
| 1466 | CLASS="FIRSTTERM" |
| 1467 | >literal</I |
| 1468 | > meaning |
| 1469 | of <I |
| 1470 | CLASS="FIRSTTERM" |
| 1471 | >regular expression</I |
| 1472 | > is an |
| 1473 | ordinary expression that conforms to accepted usage. The |
| 1474 | <I |
| 1475 | CLASS="FIRSTTERM" |
| 1476 | >meta-meaning</I |
| 1477 | > is drastically different, |
| 1478 | as discussed at length in this chapter.</P |
| 1479 | ></TD |
| 1480 | ></TR |
| 1481 | ><TR |
| 1482 | ><TD |
| 1483 | ALIGN="LEFT" |
| 1484 | VALIGN="TOP" |
| 1485 | WIDTH="5%" |
| 1486 | ><A |
| 1487 | NAME="FTN.AEN17189" |
| 1488 | HREF="regexp.html#AEN17189" |
| 1489 | >[2]</A |
| 1490 | ></TD |
| 1491 | ><TD |
| 1492 | ALIGN="LEFT" |
| 1493 | VALIGN="TOP" |
| 1494 | WIDTH="95%" |
| 1495 | ><P |
| 1496 | >Since <A |
| 1497 | HREF="sedawk.html#SEDREF" |
| 1498 | >sed</A |
| 1499 | >, <A |
| 1500 | HREF="awk.html#AWKREF" |
| 1501 | >awk</A |
| 1502 | >, and <A |
| 1503 | HREF="textproc.html#GREPREF" |
| 1504 | >grep</A |
| 1505 | > process single lines, there |
| 1506 | will usually not be a newline to match. In those cases where |
| 1507 | there is a newline in a multiple line expression, the dot |
| 1508 | will match the newline. |
| 1509 | <TABLE |
| 1510 | BORDER="0" |
| 1511 | BGCOLOR="#E0E0E0" |
| 1512 | WIDTH="90%" |
| 1513 | ><TR |
| 1514 | ><TD |
| 1515 | ><PRE |
| 1516 | CLASS="PROGRAMLISTING" |
| 1517 | > 1 #!/bin/bash |
| 1518 | 2 |
| 1519 | 3 sed -e 'N;s/.*/[&]/' << EOF # Here Document |
| 1520 | 4 line1 |
| 1521 | 5 line2 |
| 1522 | 6 EOF |
| 1523 | 7 # OUTPUT: |
| 1524 | 8 # [line1 |
| 1525 | 9 # line2] |
| 1526 | 10 |
| 1527 | 11 |
| 1528 | 12 |
| 1529 | 13 echo |
| 1530 | 14 |
| 1531 | 15 awk '{ $0=$1 "\n" $2; if (/line.1/) {print}}' << EOF |
| 1532 | 16 line 1 |
| 1533 | 17 line 2 |
| 1534 | 18 EOF |
| 1535 | 19 # OUTPUT: |
| 1536 | 20 # line |
| 1537 | 21 # 1 |
| 1538 | 22 |
| 1539 | 23 |
| 1540 | 24 # Thanks, S.C. |
| 1541 | 25 |
| 1542 | 26 exit 0</PRE |
| 1543 | ></TD |
| 1544 | ></TR |
| 1545 | ></TABLE |
| 1546 | ></P |
| 1547 | ></TD |
| 1548 | ></TR |
| 1549 | ></TABLE |
| 1550 | ><DIV |
| 1551 | CLASS="NAVFOOTER" |
| 1552 | ><HR |
| 1553 | ALIGN="LEFT" |
| 1554 | WIDTH="100%"><TABLE |
| 1555 | SUMMARY="Footer navigation table" |
| 1556 | WIDTH="100%" |
| 1557 | BORDER="0" |
| 1558 | CELLPADDING="0" |
| 1559 | CELLSPACING="0" |
| 1560 | ><TR |
| 1561 | ><TD |
| 1562 | WIDTH="33%" |
| 1563 | ALIGN="left" |
| 1564 | VALIGN="top" |
| 1565 | ><A |
| 1566 | HREF="part5.html" |
| 1567 | ACCESSKEY="P" |
| 1568 | >Prev</A |
| 1569 | ></TD |
| 1570 | ><TD |
| 1571 | WIDTH="34%" |
| 1572 | ALIGN="center" |
| 1573 | VALIGN="top" |
| 1574 | ><A |
| 1575 | HREF="index.html" |
| 1576 | ACCESSKEY="H" |
| 1577 | >Home</A |
| 1578 | ></TD |
| 1579 | ><TD |
| 1580 | WIDTH="33%" |
| 1581 | ALIGN="right" |
| 1582 | VALIGN="top" |
| 1583 | ><A |
| 1584 | HREF="globbingref.html" |
| 1585 | ACCESSKEY="N" |
| 1586 | >Next</A |
| 1587 | ></TD |
| 1588 | ></TR |
| 1589 | ><TR |
| 1590 | ><TD |
| 1591 | WIDTH="33%" |
| 1592 | ALIGN="left" |
| 1593 | VALIGN="top" |
| 1594 | >Advanced Topics</TD |
| 1595 | ><TD |
| 1596 | WIDTH="34%" |
| 1597 | ALIGN="center" |
| 1598 | VALIGN="top" |
| 1599 | ><A |
| 1600 | HREF="part5.html" |
| 1601 | ACCESSKEY="U" |
| 1602 | >Up</A |
| 1603 | ></TD |
| 1604 | ><TD |
| 1605 | WIDTH="33%" |
| 1606 | ALIGN="right" |
| 1607 | VALIGN="top" |
| 1608 | >Globbing</TD |
| 1609 | ></TR |
| 1610 | ></TABLE |
| 1611 | ></DIV |
| 1612 | ></BODY |
| 1613 | ></HTML |
| 1614 | > |