| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >Text Processing Commands</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="External Filters, Programs and Commands" |
| 15 | HREF="external.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Time / Date Commands" |
| 18 | HREF="timedate.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="File and Archiving Commands" |
| 21 | HREF="filearchiv.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="SECT1" |
| 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="timedate.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | >Chapter 16. External Filters, Programs and Commands</TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="filearchiv.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="SECT1" |
| 92 | ><H1 |
| 93 | CLASS="SECT1" |
| 94 | ><A |
| 95 | NAME="TEXTPROC" |
| 96 | ></A |
| 97 | >16.4. Text Processing Commands</H1 |
| 98 | ><DIV |
| 99 | CLASS="VARIABLELIST" |
| 100 | ><P |
| 101 | ><B |
| 102 | ><A |
| 103 | NAME="TPCOMMANDLISTING1" |
| 104 | ></A |
| 105 | >Commands affecting text and |
| 106 | text files</B |
| 107 | ></P |
| 108 | ><DL |
| 109 | ><DT |
| 110 | ><A |
| 111 | NAME="SORTREF" |
| 112 | ></A |
| 113 | ><B |
| 114 | CLASS="COMMAND" |
| 115 | >sort</B |
| 116 | ></DT |
| 117 | ><DD |
| 118 | ><P |
| 119 | >File sort utility, often used as a filter in a pipe. This |
| 120 | command sorts a <I |
| 121 | CLASS="FIRSTTERM" |
| 122 | >text stream</I |
| 123 | > |
| 124 | or file forwards or backwards, or according to various |
| 125 | keys or character positions. Using the <TT |
| 126 | CLASS="OPTION" |
| 127 | >-m</TT |
| 128 | > |
| 129 | option, it merges presorted input files. The <I |
| 130 | CLASS="FIRSTTERM" |
| 131 | >info |
| 132 | page</I |
| 133 | > lists its many capabilities and options. See |
| 134 | <A |
| 135 | HREF="loops.html#FINDSTRING" |
| 136 | >Example 11-10</A |
| 137 | >, <A |
| 138 | HREF="loops.html#SYMLINKS" |
| 139 | >Example 11-11</A |
| 140 | >, |
| 141 | and <A |
| 142 | HREF="contributed-scripts.html#MAKEDICT" |
| 143 | >Example A-8</A |
| 144 | >.</P |
| 145 | ></DD |
| 146 | ><DT |
| 147 | ><A |
| 148 | NAME="TSORTREF" |
| 149 | ></A |
| 150 | ><B |
| 151 | CLASS="COMMAND" |
| 152 | >tsort</B |
| 153 | ></DT |
| 154 | ><DD |
| 155 | ><P |
| 156 | ><I |
| 157 | CLASS="FIRSTTERM" |
| 158 | >Topological sort</I |
| 159 | >, reading in |
| 160 | pairs of whitespace-separated strings and sorting |
| 161 | according to input patterns. The original purpose of |
| 162 | <B |
| 163 | CLASS="COMMAND" |
| 164 | >tsort</B |
| 165 | > was to sort a list of dependencies |
| 166 | for an obsolete version of the <I |
| 167 | CLASS="FIRSTTERM" |
| 168 | >ld</I |
| 169 | > |
| 170 | linker in an <SPAN |
| 171 | CLASS="QUOTE" |
| 172 | >"ancient"</SPAN |
| 173 | > version of UNIX.</P |
| 174 | ><P |
| 175 | >The results of a <I |
| 176 | CLASS="FIRSTTERM" |
| 177 | >tsort</I |
| 178 | > will usually |
| 179 | differ markedly from those of the standard |
| 180 | <B |
| 181 | CLASS="COMMAND" |
| 182 | >sort</B |
| 183 | > command, above.</P |
| 184 | ></DD |
| 185 | ><DT |
| 186 | ><A |
| 187 | NAME="UNIQREF" |
| 188 | ></A |
| 189 | ><B |
| 190 | CLASS="COMMAND" |
| 191 | >uniq</B |
| 192 | ></DT |
| 193 | ><DD |
| 194 | ><P |
| 195 | >This filter removes duplicate lines from a sorted |
| 196 | file. It is often seen in a pipe coupled with |
| 197 | <A |
| 198 | HREF="textproc.html#SORTREF" |
| 199 | >sort</A |
| 200 | >.</P |
| 201 | ><P |
| 202 | ><TABLE |
| 203 | BORDER="0" |
| 204 | BGCOLOR="#E0E0E0" |
| 205 | WIDTH="90%" |
| 206 | ><TR |
| 207 | ><TD |
| 208 | ><PRE |
| 209 | CLASS="PROGRAMLISTING" |
| 210 | > 1 cat list-1 list-2 list-3 | sort | uniq > final.list |
| 211 | 2 # Concatenates the list files, |
| 212 | 3 # sorts them, |
| 213 | 4 # removes duplicate lines, |
| 214 | 5 # and finally writes the result to an output file.</PRE |
| 215 | ></TD |
| 216 | ></TR |
| 217 | ></TABLE |
| 218 | ></P |
| 219 | ><P |
| 220 | >The useful <TT |
| 221 | CLASS="OPTION" |
| 222 | >-c</TT |
| 223 | > option prefixes each line of |
| 224 | the input file with its number of occurrences.</P |
| 225 | ><P |
| 226 | > <TABLE |
| 227 | BORDER="0" |
| 228 | BGCOLOR="#E0E0E0" |
| 229 | WIDTH="90%" |
| 230 | ><TR |
| 231 | ><TD |
| 232 | ><PRE |
| 233 | CLASS="SCREEN" |
| 234 | > <TT |
| 235 | CLASS="PROMPT" |
| 236 | >bash$ </TT |
| 237 | ><TT |
| 238 | CLASS="USERINPUT" |
| 239 | ><B |
| 240 | >cat testfile</B |
| 241 | ></TT |
| 242 | > |
| 243 | <TT |
| 244 | CLASS="COMPUTEROUTPUT" |
| 245 | >This line occurs only once. |
| 246 | This line occurs twice. |
| 247 | This line occurs twice. |
| 248 | This line occurs three times. |
| 249 | This line occurs three times. |
| 250 | This line occurs three times.</TT |
| 251 | > |
| 252 | |
| 253 | |
| 254 | <TT |
| 255 | CLASS="PROMPT" |
| 256 | >bash$ </TT |
| 257 | ><TT |
| 258 | CLASS="USERINPUT" |
| 259 | ><B |
| 260 | >uniq -c testfile</B |
| 261 | ></TT |
| 262 | > |
| 263 | <TT |
| 264 | CLASS="COMPUTEROUTPUT" |
| 265 | > 1 This line occurs only once. |
| 266 | 2 This line occurs twice. |
| 267 | 3 This line occurs three times.</TT |
| 268 | > |
| 269 | |
| 270 | |
| 271 | <TT |
| 272 | CLASS="PROMPT" |
| 273 | >bash$ </TT |
| 274 | ><TT |
| 275 | CLASS="USERINPUT" |
| 276 | ><B |
| 277 | >sort testfile | uniq -c | sort -nr</B |
| 278 | ></TT |
| 279 | > |
| 280 | <TT |
| 281 | CLASS="COMPUTEROUTPUT" |
| 282 | > 3 This line occurs three times. |
| 283 | 2 This line occurs twice. |
| 284 | 1 This line occurs only once.</TT |
| 285 | > |
| 286 | </PRE |
| 287 | ></TD |
| 288 | ></TR |
| 289 | ></TABLE |
| 290 | > |
| 291 | </P |
| 292 | ><P |
| 293 | >The <TT |
| 294 | CLASS="USERINPUT" |
| 295 | ><B |
| 296 | >sort INPUTFILE | uniq -c | sort -nr</B |
| 297 | ></TT |
| 298 | > |
| 299 | command string produces a <I |
| 300 | CLASS="FIRSTTERM" |
| 301 | >frequency |
| 302 | of occurrence</I |
| 303 | > listing on the |
| 304 | <TT |
| 305 | CLASS="FILENAME" |
| 306 | >INPUTFILE</TT |
| 307 | > file (the |
| 308 | <TT |
| 309 | CLASS="OPTION" |
| 310 | >-nr</TT |
| 311 | > options to <B |
| 312 | CLASS="COMMAND" |
| 313 | >sort</B |
| 314 | > |
| 315 | cause a reverse numerical sort). This template finds |
| 316 | use in analysis of log files and dictionary lists, and |
| 317 | wherever the lexical structure of a document needs to |
| 318 | be examined.</P |
| 319 | ><DIV |
| 320 | CLASS="EXAMPLE" |
| 321 | ><HR><A |
| 322 | NAME="WF" |
| 323 | ></A |
| 324 | ><P |
| 325 | ><B |
| 326 | >Example 16-12. Word Frequency Analysis</B |
| 327 | ></P |
| 328 | ><TABLE |
| 329 | BORDER="0" |
| 330 | BGCOLOR="#E0E0E0" |
| 331 | WIDTH="90%" |
| 332 | ><TR |
| 333 | ><TD |
| 334 | ><PRE |
| 335 | CLASS="PROGRAMLISTING" |
| 336 | > 1 #!/bin/bash |
| 337 | 2 # wf.sh: Crude word frequency analysis on a text file. |
| 338 | 3 # This is a more efficient version of the "wf2.sh" script. |
| 339 | 4 |
| 340 | 5 |
| 341 | 6 # Check for input file on command-line. |
| 342 | 7 ARGS=1 |
| 343 | 8 E_BADARGS=85 |
| 344 | 9 E_NOFILE=86 |
| 345 | 10 |
| 346 | 11 if [ $# -ne "$ARGS" ] # Correct number of arguments passed to script? |
| 347 | 12 then |
| 348 | 13 echo "Usage: `basename $0` filename" |
| 349 | 14 exit $E_BADARGS |
| 350 | 15 fi |
| 351 | 16 |
| 352 | 17 if [ ! -f "$1" ] # Check if file exists. |
| 353 | 18 then |
| 354 | 19 echo "File \"$1\" does not exist." |
| 355 | 20 exit $E_NOFILE |
| 356 | 21 fi |
| 357 | 22 |
| 358 | 23 |
| 359 | 24 |
| 360 | 25 ######################################################## |
| 361 | 26 # main () |
| 362 | 27 sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\ |
| 363 | 28 /g' "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr |
| 364 | 29 # ========================= |
| 365 | 30 # Frequency of occurrence |
| 366 | 31 |
| 367 | 32 # Filter out periods and commas, and |
| 368 | 33 #+ change space between words to linefeed, |
| 369 | 34 #+ then shift characters to lowercase, and |
| 370 | 35 #+ finally prefix occurrence count and sort numerically. |
| 371 | 36 |
| 372 | 37 # Arun Giridhar suggests modifying the above to: |
| 373 | 38 # . . . | sort | uniq -c | sort +1 [-f] | sort +0 -nr |
| 374 | 39 # This adds a secondary sort key, so instances of |
| 375 | 40 #+ equal occurrence are sorted alphabetically. |
| 376 | 41 # As he explains it: |
| 377 | 42 # "This is effectively a radix sort, first on the |
| 378 | 43 #+ least significant column |
| 379 | 44 #+ (word or string, optionally case-insensitive) |
| 380 | 45 #+ and last on the most significant column (frequency)." |
| 381 | 46 # |
| 382 | 47 # As Frank Wang explains, the above is equivalent to |
| 383 | 48 #+ . . . | sort | uniq -c | sort +0 -nr |
| 384 | 49 #+ and the following also works: |
| 385 | 50 #+ . . . | sort | uniq -c | sort -k1nr -k |
| 386 | 51 ######################################################## |
| 387 | 52 |
| 388 | 53 exit 0 |
| 389 | 54 |
| 390 | 55 # Exercises: |
| 391 | 56 # --------- |
| 392 | 57 # 1) Add 'sed' commands to filter out other punctuation, |
| 393 | 58 #+ such as semicolons. |
| 394 | 59 # 2) Modify the script to also filter out multiple spaces and |
| 395 | 60 #+ other whitespace.</PRE |
| 396 | ></TD |
| 397 | ></TR |
| 398 | ></TABLE |
| 399 | ><HR></DIV |
| 400 | ><P |
| 401 | > <TABLE |
| 402 | BORDER="0" |
| 403 | BGCOLOR="#E0E0E0" |
| 404 | WIDTH="90%" |
| 405 | ><TR |
| 406 | ><TD |
| 407 | ><PRE |
| 408 | CLASS="SCREEN" |
| 409 | > <TT |
| 410 | CLASS="PROMPT" |
| 411 | >bash$ </TT |
| 412 | ><TT |
| 413 | CLASS="USERINPUT" |
| 414 | ><B |
| 415 | >cat testfile</B |
| 416 | ></TT |
| 417 | > |
| 418 | <TT |
| 419 | CLASS="COMPUTEROUTPUT" |
| 420 | >This line occurs only once. |
| 421 | This line occurs twice. |
| 422 | This line occurs twice. |
| 423 | This line occurs three times. |
| 424 | This line occurs three times. |
| 425 | This line occurs three times.</TT |
| 426 | > |
| 427 | |
| 428 | |
| 429 | <TT |
| 430 | CLASS="PROMPT" |
| 431 | >bash$ </TT |
| 432 | ><TT |
| 433 | CLASS="USERINPUT" |
| 434 | ><B |
| 435 | >./wf.sh testfile</B |
| 436 | ></TT |
| 437 | > |
| 438 | <TT |
| 439 | CLASS="COMPUTEROUTPUT" |
| 440 | > 6 this |
| 441 | 6 occurs |
| 442 | 6 line |
| 443 | 3 times |
| 444 | 3 three |
| 445 | 2 twice |
| 446 | 1 only |
| 447 | 1 once</TT |
| 448 | > |
| 449 | </PRE |
| 450 | ></TD |
| 451 | ></TR |
| 452 | ></TABLE |
| 453 | > |
| 454 | </P |
| 455 | ></DD |
| 456 | ><DT |
| 457 | ><A |
| 458 | NAME="EXPANDREF" |
| 459 | ></A |
| 460 | ><B |
| 461 | CLASS="COMMAND" |
| 462 | >expand</B |
| 463 | >, <B |
| 464 | CLASS="COMMAND" |
| 465 | >unexpand</B |
| 466 | ></DT |
| 467 | ><DD |
| 468 | ><P |
| 469 | >The <B |
| 470 | CLASS="COMMAND" |
| 471 | >expand</B |
| 472 | > filter converts tabs to |
| 473 | spaces. It is often used in a <A |
| 474 | HREF="special-chars.html#PIPEREF" |
| 475 | >pipe</A |
| 476 | >.</P |
| 477 | ><P |
| 478 | >The <B |
| 479 | CLASS="COMMAND" |
| 480 | >unexpand</B |
| 481 | > filter |
| 482 | converts spaces to tabs. This reverses the effect of |
| 483 | <B |
| 484 | CLASS="COMMAND" |
| 485 | >expand</B |
| 486 | >.</P |
| 487 | ></DD |
| 488 | ><DT |
| 489 | ><A |
| 490 | NAME="CUTREF" |
| 491 | ></A |
| 492 | ><B |
| 493 | CLASS="COMMAND" |
| 494 | >cut</B |
| 495 | ></DT |
| 496 | ><DD |
| 497 | ><P |
| 498 | >A tool for extracting <A |
| 499 | HREF="special-chars.html#FIELDREF" |
| 500 | >fields</A |
| 501 | > from files. It is similar |
| 502 | to the <TT |
| 503 | CLASS="USERINPUT" |
| 504 | ><B |
| 505 | >print $N</B |
| 506 | ></TT |
| 507 | > command set in <A |
| 508 | HREF="awk.html#AWKREF" |
| 509 | >awk</A |
| 510 | >, but more limited. It may be |
| 511 | simpler to use <I |
| 512 | CLASS="FIRSTTERM" |
| 513 | >cut</I |
| 514 | > in a script than |
| 515 | <I |
| 516 | CLASS="FIRSTTERM" |
| 517 | >awk</I |
| 518 | >. Particularly important are the |
| 519 | <TT |
| 520 | CLASS="OPTION" |
| 521 | >-d</TT |
| 522 | > (delimiter) and <TT |
| 523 | CLASS="OPTION" |
| 524 | >-f</TT |
| 525 | > |
| 526 | (field specifier) options.</P |
| 527 | ><P |
| 528 | >Using <B |
| 529 | CLASS="COMMAND" |
| 530 | >cut</B |
| 531 | > to obtain a listing of the |
| 532 | mounted filesystems: |
| 533 | <TABLE |
| 534 | BORDER="0" |
| 535 | BGCOLOR="#E0E0E0" |
| 536 | WIDTH="90%" |
| 537 | ><TR |
| 538 | ><TD |
| 539 | ><PRE |
| 540 | CLASS="PROGRAMLISTING" |
| 541 | > 1 cut -d ' ' -f1,2 /etc/mtab</PRE |
| 542 | ></TD |
| 543 | ></TR |
| 544 | ></TABLE |
| 545 | ></P |
| 546 | ><P |
| 547 | >Using <B |
| 548 | CLASS="COMMAND" |
| 549 | >cut</B |
| 550 | > to list the OS and kernel version: |
| 551 | <TABLE |
| 552 | BORDER="0" |
| 553 | BGCOLOR="#E0E0E0" |
| 554 | WIDTH="90%" |
| 555 | ><TR |
| 556 | ><TD |
| 557 | ><PRE |
| 558 | CLASS="PROGRAMLISTING" |
| 559 | > 1 uname -a | cut -d" " -f1,3,11,12</PRE |
| 560 | ></TD |
| 561 | ></TR |
| 562 | ></TABLE |
| 563 | ></P |
| 564 | ><P |
| 565 | >Using <B |
| 566 | CLASS="COMMAND" |
| 567 | >cut</B |
| 568 | > to extract message headers from |
| 569 | an e-mail folder: |
| 570 | |
| 571 | <TABLE |
| 572 | BORDER="0" |
| 573 | BGCOLOR="#E0E0E0" |
| 574 | WIDTH="90%" |
| 575 | ><TR |
| 576 | ><TD |
| 577 | ><PRE |
| 578 | CLASS="SCREEN" |
| 579 | > <TT |
| 580 | CLASS="PROMPT" |
| 581 | >bash$ </TT |
| 582 | ><TT |
| 583 | CLASS="USERINPUT" |
| 584 | ><B |
| 585 | >grep '^Subject:' read-messages | cut -c10-80</B |
| 586 | ></TT |
| 587 | > |
| 588 | <TT |
| 589 | CLASS="COMPUTEROUTPUT" |
| 590 | >Re: Linux suitable for mission-critical apps? |
| 591 | MAKE MILLIONS WORKING AT HOME!!! |
| 592 | Spam complaint |
| 593 | Re: Spam complaint</TT |
| 594 | ></PRE |
| 595 | ></TD |
| 596 | ></TR |
| 597 | ></TABLE |
| 598 | > |
| 599 | </P |
| 600 | ><P |
| 601 | >Using <B |
| 602 | CLASS="COMMAND" |
| 603 | >cut</B |
| 604 | > to parse a file: |
| 605 | <TABLE |
| 606 | BORDER="0" |
| 607 | BGCOLOR="#E0E0E0" |
| 608 | WIDTH="90%" |
| 609 | ><TR |
| 610 | ><TD |
| 611 | ><PRE |
| 612 | CLASS="PROGRAMLISTING" |
| 613 | > 1 # List all the users in /etc/passwd. |
| 614 | 2 |
| 615 | 3 FILENAME=/etc/passwd |
| 616 | 4 |
| 617 | 5 for user in $(cut -d: -f1 $FILENAME) |
| 618 | 6 do |
| 619 | 7 echo $user |
| 620 | 8 done |
| 621 | 9 |
| 622 | 10 # Thanks, Oleg Philon for suggesting this.</PRE |
| 623 | ></TD |
| 624 | ></TR |
| 625 | ></TABLE |
| 626 | ></P |
| 627 | ><P |
| 628 | ><TT |
| 629 | CLASS="USERINPUT" |
| 630 | ><B |
| 631 | >cut -d ' ' -f2,3 filename</B |
| 632 | ></TT |
| 633 | > is equivalent to |
| 634 | <TT |
| 635 | CLASS="USERINPUT" |
| 636 | ><B |
| 637 | >awk -F'[ ]' '{ print $2, $3 }' filename</B |
| 638 | ></TT |
| 639 | ></P |
| 640 | ><DIV |
| 641 | CLASS="NOTE" |
| 642 | ><TABLE |
| 643 | CLASS="NOTE" |
| 644 | WIDTH="90%" |
| 645 | BORDER="0" |
| 646 | ><TR |
| 647 | ><TD |
| 648 | WIDTH="25" |
| 649 | ALIGN="CENTER" |
| 650 | VALIGN="TOP" |
| 651 | ><IMG |
| 652 | SRC="common/note.png" |
| 653 | HSPACE="5" |
| 654 | ALT="Note"></TD |
| 655 | ><TD |
| 656 | ALIGN="LEFT" |
| 657 | VALIGN="TOP" |
| 658 | ><P |
| 659 | >It is even possible to specify a linefeed as a |
| 660 | delimiter. The trick is to actually embed a linefeed |
| 661 | (<B |
| 662 | CLASS="KEYCAP" |
| 663 | >RETURN</B |
| 664 | >) in the command sequence.</P |
| 665 | ><P |
| 666 | > <TABLE |
| 667 | BORDER="0" |
| 668 | BGCOLOR="#E0E0E0" |
| 669 | WIDTH="90%" |
| 670 | ><TR |
| 671 | ><TD |
| 672 | ><PRE |
| 673 | CLASS="SCREEN" |
| 674 | > <TT |
| 675 | CLASS="PROMPT" |
| 676 | >bash$ </TT |
| 677 | ><TT |
| 678 | CLASS="USERINPUT" |
| 679 | ><B |
| 680 | >cut -d' |
| 681 | ' -f3,7,19 testfile</B |
| 682 | ></TT |
| 683 | > |
| 684 | <TT |
| 685 | CLASS="COMPUTEROUTPUT" |
| 686 | >This is line 3 of testfile. |
| 687 | This is line 7 of testfile. |
| 688 | This is line 19 of testfile.</TT |
| 689 | > |
| 690 | </PRE |
| 691 | ></TD |
| 692 | ></TR |
| 693 | ></TABLE |
| 694 | > |
| 695 | </P |
| 696 | ><P |
| 697 | >Thank you, Jaka Kranjc, for pointing this out.</P |
| 698 | ></TD |
| 699 | ></TR |
| 700 | ></TABLE |
| 701 | ></DIV |
| 702 | ><P |
| 703 | >See also <A |
| 704 | HREF="mathc.html#BASE" |
| 705 | >Example 16-48</A |
| 706 | >.</P |
| 707 | ></DD |
| 708 | ><DT |
| 709 | ><A |
| 710 | NAME="PASTEREF" |
| 711 | ></A |
| 712 | ><B |
| 713 | CLASS="COMMAND" |
| 714 | >paste</B |
| 715 | ></DT |
| 716 | ><DD |
| 717 | ><P |
| 718 | >Tool for merging together different files into a single, |
| 719 | multi-column file. In combination with |
| 720 | <A |
| 721 | HREF="textproc.html#CUTREF" |
| 722 | >cut</A |
| 723 | >, useful for creating system log |
| 724 | files. |
| 725 | </P |
| 726 | ><P |
| 727 | > <TABLE |
| 728 | BORDER="0" |
| 729 | BGCOLOR="#E0E0E0" |
| 730 | WIDTH="90%" |
| 731 | ><TR |
| 732 | ><TD |
| 733 | ><PRE |
| 734 | CLASS="SCREEN" |
| 735 | > <TT |
| 736 | CLASS="PROMPT" |
| 737 | >bash$ </TT |
| 738 | ><TT |
| 739 | CLASS="USERINPUT" |
| 740 | ><B |
| 741 | >cat items</B |
| 742 | ></TT |
| 743 | > |
| 744 | <TT |
| 745 | CLASS="COMPUTEROUTPUT" |
| 746 | >alphabet blocks |
| 747 | building blocks |
| 748 | cables</TT |
| 749 | > |
| 750 | |
| 751 | <TT |
| 752 | CLASS="PROMPT" |
| 753 | >bash$ </TT |
| 754 | ><TT |
| 755 | CLASS="USERINPUT" |
| 756 | ><B |
| 757 | >cat prices</B |
| 758 | ></TT |
| 759 | > |
| 760 | <TT |
| 761 | CLASS="COMPUTEROUTPUT" |
| 762 | >$1.00/dozen |
| 763 | $2.50 ea. |
| 764 | $3.75</TT |
| 765 | > |
| 766 | |
| 767 | <TT |
| 768 | CLASS="PROMPT" |
| 769 | >bash$ </TT |
| 770 | ><TT |
| 771 | CLASS="USERINPUT" |
| 772 | ><B |
| 773 | >paste items prices</B |
| 774 | ></TT |
| 775 | > |
| 776 | <TT |
| 777 | CLASS="COMPUTEROUTPUT" |
| 778 | >alphabet blocks $1.00/dozen |
| 779 | building blocks $2.50 ea. |
| 780 | cables $3.75</TT |
| 781 | ></PRE |
| 782 | ></TD |
| 783 | ></TR |
| 784 | ></TABLE |
| 785 | > |
| 786 | </P |
| 787 | ></DD |
| 788 | ><DT |
| 789 | ><A |
| 790 | NAME="JOINREF" |
| 791 | ></A |
| 792 | ><B |
| 793 | CLASS="COMMAND" |
| 794 | >join</B |
| 795 | ></DT |
| 796 | ><DD |
| 797 | ><P |
| 798 | >Consider this a special-purpose cousin of |
| 799 | <B |
| 800 | CLASS="COMMAND" |
| 801 | >paste</B |
| 802 | >. This powerful utility allows |
| 803 | merging two files in a meaningful fashion, which essentially |
| 804 | creates a simple version of a relational database.</P |
| 805 | ><P |
| 806 | >The <B |
| 807 | CLASS="COMMAND" |
| 808 | >join</B |
| 809 | > command operates on |
| 810 | exactly two files, but pastes together only those lines |
| 811 | with a common tagged <A |
| 812 | HREF="special-chars.html#FIELDREF" |
| 813 | >field</A |
| 814 | > |
| 815 | (usually a numerical label), and writes the result to |
| 816 | <TT |
| 817 | CLASS="FILENAME" |
| 818 | >stdout</TT |
| 819 | >. The files to be joined should |
| 820 | be sorted according to the tagged field for the matchups |
| 821 | to work properly.</P |
| 822 | ><P |
| 823 | ><TABLE |
| 824 | BORDER="0" |
| 825 | BGCOLOR="#E0E0E0" |
| 826 | WIDTH="90%" |
| 827 | ><TR |
| 828 | ><TD |
| 829 | ><PRE |
| 830 | CLASS="PROGRAMLISTING" |
| 831 | > 1 File: 1.data |
| 832 | 2 |
| 833 | 3 100 Shoes |
| 834 | 4 200 Laces |
| 835 | 5 300 Socks</PRE |
| 836 | ></TD |
| 837 | ></TR |
| 838 | ></TABLE |
| 839 | ></P |
| 840 | ><P |
| 841 | ><TABLE |
| 842 | BORDER="0" |
| 843 | BGCOLOR="#E0E0E0" |
| 844 | WIDTH="90%" |
| 845 | ><TR |
| 846 | ><TD |
| 847 | ><PRE |
| 848 | CLASS="PROGRAMLISTING" |
| 849 | > 1 File: 2.data |
| 850 | 2 |
| 851 | 3 100 $40.00 |
| 852 | 4 200 $1.00 |
| 853 | 5 300 $2.00</PRE |
| 854 | ></TD |
| 855 | ></TR |
| 856 | ></TABLE |
| 857 | ></P |
| 858 | ><P |
| 859 | > <TABLE |
| 860 | BORDER="0" |
| 861 | BGCOLOR="#E0E0E0" |
| 862 | WIDTH="90%" |
| 863 | ><TR |
| 864 | ><TD |
| 865 | ><PRE |
| 866 | CLASS="SCREEN" |
| 867 | > <TT |
| 868 | CLASS="PROMPT" |
| 869 | >bash$ </TT |
| 870 | ><TT |
| 871 | CLASS="USERINPUT" |
| 872 | ><B |
| 873 | >join 1.data 2.data</B |
| 874 | ></TT |
| 875 | > |
| 876 | <TT |
| 877 | CLASS="COMPUTEROUTPUT" |
| 878 | >File: 1.data 2.data |
| 879 | |
| 880 | 100 Shoes $40.00 |
| 881 | 200 Laces $1.00 |
| 882 | 300 Socks $2.00</TT |
| 883 | > |
| 884 | </PRE |
| 885 | ></TD |
| 886 | ></TR |
| 887 | ></TABLE |
| 888 | > |
| 889 | </P |
| 890 | ><DIV |
| 891 | CLASS="NOTE" |
| 892 | ><TABLE |
| 893 | CLASS="NOTE" |
| 894 | WIDTH="90%" |
| 895 | BORDER="0" |
| 896 | ><TR |
| 897 | ><TD |
| 898 | WIDTH="25" |
| 899 | ALIGN="CENTER" |
| 900 | VALIGN="TOP" |
| 901 | ><IMG |
| 902 | SRC="common/note.png" |
| 903 | HSPACE="5" |
| 904 | ALT="Note"></TD |
| 905 | ><TD |
| 906 | ALIGN="LEFT" |
| 907 | VALIGN="TOP" |
| 908 | ><P |
| 909 | >The tagged field appears only once in the |
| 910 | output.</P |
| 911 | ></TD |
| 912 | ></TR |
| 913 | ></TABLE |
| 914 | ></DIV |
| 915 | ></DD |
| 916 | ><DT |
| 917 | ><A |
| 918 | NAME="HEADREF" |
| 919 | ></A |
| 920 | ><B |
| 921 | CLASS="COMMAND" |
| 922 | >head</B |
| 923 | ></DT |
| 924 | ><DD |
| 925 | ><P |
| 926 | >lists the beginning of a file to <TT |
| 927 | CLASS="FILENAME" |
| 928 | >stdout</TT |
| 929 | >. |
| 930 | The default is <TT |
| 931 | CLASS="LITERAL" |
| 932 | >10</TT |
| 933 | > lines, but a different |
| 934 | number can be specified. The command has a number of |
| 935 | interesting options. |
| 936 | |
| 937 | <DIV |
| 938 | CLASS="EXAMPLE" |
| 939 | ><HR><A |
| 940 | NAME="SCRIPTDETECTOR" |
| 941 | ></A |
| 942 | ><P |
| 943 | ><B |
| 944 | >Example 16-13. Which files are scripts?</B |
| 945 | ></P |
| 946 | ><TABLE |
| 947 | BORDER="0" |
| 948 | BGCOLOR="#E0E0E0" |
| 949 | WIDTH="90%" |
| 950 | ><TR |
| 951 | ><TD |
| 952 | ><PRE |
| 953 | CLASS="PROGRAMLISTING" |
| 954 | > 1 #!/bin/bash |
| 955 | 2 # script-detector.sh: Detects scripts within a directory. |
| 956 | 3 |
| 957 | 4 TESTCHARS=2 # Test first 2 characters. |
| 958 | 5 SHABANG='#!' # Scripts begin with a "sha-bang." |
| 959 | 6 |
| 960 | 7 for file in * # Traverse all the files in current directory. |
| 961 | 8 do |
| 962 | 9 if [[ `head -c$TESTCHARS "$file"` = "$SHABANG" ]] |
| 963 | 10 # head -c2 #! |
| 964 | 11 # The '-c' option to "head" outputs a specified |
| 965 | 12 #+ number of characters, rather than lines (the default). |
| 966 | 13 then |
| 967 | 14 echo "File \"$file\" is a script." |
| 968 | 15 else |
| 969 | 16 echo "File \"$file\" is *not* a script." |
| 970 | 17 fi |
| 971 | 18 done |
| 972 | 19 |
| 973 | 20 exit 0 |
| 974 | 21 |
| 975 | 22 # Exercises: |
| 976 | 23 # --------- |
| 977 | 24 # 1) Modify this script to take as an optional argument |
| 978 | 25 #+ the directory to scan for scripts |
| 979 | 26 #+ (rather than just the current working directory). |
| 980 | 27 # |
| 981 | 28 # 2) As it stands, this script gives "false positives" for |
| 982 | 29 #+ Perl, awk, and other scripting language scripts. |
| 983 | 30 # Correct this.</PRE |
| 984 | ></TD |
| 985 | ></TR |
| 986 | ></TABLE |
| 987 | ><HR></DIV |
| 988 | > |
| 989 | |
| 990 | <DIV |
| 991 | CLASS="EXAMPLE" |
| 992 | ><HR><A |
| 993 | NAME="RND" |
| 994 | ></A |
| 995 | ><P |
| 996 | ><B |
| 997 | >Example 16-14. Generating 10-digit random numbers</B |
| 998 | ></P |
| 999 | ><TABLE |
| 1000 | BORDER="0" |
| 1001 | BGCOLOR="#E0E0E0" |
| 1002 | WIDTH="90%" |
| 1003 | ><TR |
| 1004 | ><TD |
| 1005 | ><PRE |
| 1006 | CLASS="PROGRAMLISTING" |
| 1007 | > 1 #!/bin/bash |
| 1008 | 2 # rnd.sh: Outputs a 10-digit random number |
| 1009 | 3 |
| 1010 | 4 # Script by Stephane Chazelas. |
| 1011 | 5 |
| 1012 | 6 head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' |
| 1013 | 7 |
| 1014 | 8 |
| 1015 | 9 # =================================================================== # |
| 1016 | 10 |
| 1017 | 11 # Analysis |
| 1018 | 12 # -------- |
| 1019 | 13 |
| 1020 | 14 # head: |
| 1021 | 15 # -c4 option takes first 4 bytes. |
| 1022 | 16 |
| 1023 | 17 # od: |
| 1024 | 18 # -N4 option limits output to 4 bytes. |
| 1025 | 19 # -tu4 option selects unsigned decimal format for output. |
| 1026 | 20 |
| 1027 | 21 # sed: |
| 1028 | 22 # -n option, in combination with "p" flag to the "s" command, |
| 1029 | 23 # outputs only matched lines. |
| 1030 | 24 |
| 1031 | 25 |
| 1032 | 26 |
| 1033 | 27 # The author of this script explains the action of 'sed', as follows. |
| 1034 | 28 |
| 1035 | 29 # head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p' |
| 1036 | 30 # ----------------------------------> | |
| 1037 | 31 |
| 1038 | 32 # Assume output up to "sed" --------> | |
| 1039 | 33 # is 0000000 1198195154\n |
| 1040 | 34 |
| 1041 | 35 # sed begins reading characters: 0000000 1198195154\n. |
| 1042 | 36 # Here it finds a newline character, |
| 1043 | 37 #+ so it is ready to process the first line (0000000 1198195154). |
| 1044 | 38 # It looks at its <range><action>s. The first and only one is |
| 1045 | 39 |
| 1046 | 40 # range action |
| 1047 | 41 # 1 s/.* //p |
| 1048 | 42 |
| 1049 | 43 # The line number is in the range, so it executes the action: |
| 1050 | 44 #+ tries to substitute the longest string ending with a space in the line |
| 1051 | 45 # ("0000000 ") with nothing (//), and if it succeeds, prints the result |
| 1052 | 46 # ("p" is a flag to the "s" command here, this is different |
| 1053 | 47 #+ from the "p" command). |
| 1054 | 48 |
| 1055 | 49 # sed is now ready to continue reading its input. (Note that before |
| 1056 | 50 #+ continuing, if -n option had not been passed, sed would have printed |
| 1057 | 51 #+ the line once again). |
| 1058 | 52 |
| 1059 | 53 # Now, sed reads the remainder of the characters, and finds the |
| 1060 | 54 #+ end of the file. |
| 1061 | 55 # It is now ready to process its 2nd line (which is also numbered '$' as |
| 1062 | 56 #+ it's the last one). |
| 1063 | 57 # It sees it is not matched by any <range>, so its job is done. |
| 1064 | 58 |
| 1065 | 59 # In few word this sed commmand means: |
| 1066 | 60 # "On the first line only, remove any character up to the right-most space, |
| 1067 | 61 #+ then print it." |
| 1068 | 62 |
| 1069 | 63 # A better way to do this would have been: |
| 1070 | 64 # sed -e 's/.* //;q' |
| 1071 | 65 |
| 1072 | 66 # Here, two <range><action>s (could have been written |
| 1073 | 67 # sed -e 's/.* //' -e q): |
| 1074 | 68 |
| 1075 | 69 # range action |
| 1076 | 70 # nothing (matches line) s/.* // |
| 1077 | 71 # nothing (matches line) q (quit) |
| 1078 | 72 |
| 1079 | 73 # Here, sed only reads its first line of input. |
| 1080 | 74 # It performs both actions, and prints the line (substituted) before |
| 1081 | 75 #+ quitting (because of the "q" action) since the "-n" option is not passed. |
| 1082 | 76 |
| 1083 | 77 # =================================================================== # |
| 1084 | 78 |
| 1085 | 79 # An even simpler altenative to the above one-line script would be: |
| 1086 | 80 # head -c4 /dev/urandom| od -An -tu4 |
| 1087 | 81 |
| 1088 | 82 exit</PRE |
| 1089 | ></TD |
| 1090 | ></TR |
| 1091 | ></TABLE |
| 1092 | ><HR></DIV |
| 1093 | > |
| 1094 | |
| 1095 | See also <A |
| 1096 | HREF="filearchiv.html#EX52" |
| 1097 | >Example 16-39</A |
| 1098 | >.</P |
| 1099 | ></DD |
| 1100 | ><DT |
| 1101 | ><A |
| 1102 | NAME="TAILREF" |
| 1103 | ></A |
| 1104 | ><B |
| 1105 | CLASS="COMMAND" |
| 1106 | >tail</B |
| 1107 | ></DT |
| 1108 | ><DD |
| 1109 | ><P |
| 1110 | >lists the (tail) end of a file to <TT |
| 1111 | CLASS="FILENAME" |
| 1112 | >stdout</TT |
| 1113 | >. |
| 1114 | The default is <TT |
| 1115 | CLASS="LITERAL" |
| 1116 | >10</TT |
| 1117 | > lines, but this can |
| 1118 | be changed with the <TT |
| 1119 | CLASS="OPTION" |
| 1120 | >-n</TT |
| 1121 | > option. |
| 1122 | Commonly used to keep track of |
| 1123 | changes to a system logfile, using the <TT |
| 1124 | CLASS="OPTION" |
| 1125 | >-f</TT |
| 1126 | > |
| 1127 | option, which outputs lines appended to the file.</P |
| 1128 | ><DIV |
| 1129 | CLASS="EXAMPLE" |
| 1130 | ><HR><A |
| 1131 | NAME="EX12" |
| 1132 | ></A |
| 1133 | ><P |
| 1134 | ><B |
| 1135 | >Example 16-15. Using <I |
| 1136 | CLASS="FIRSTTERM" |
| 1137 | >tail</I |
| 1138 | > to monitor the system log</B |
| 1139 | ></P |
| 1140 | ><TABLE |
| 1141 | BORDER="0" |
| 1142 | BGCOLOR="#E0E0E0" |
| 1143 | WIDTH="90%" |
| 1144 | ><TR |
| 1145 | ><TD |
| 1146 | ><PRE |
| 1147 | CLASS="PROGRAMLISTING" |
| 1148 | > 1 #!/bin/bash |
| 1149 | 2 |
| 1150 | 3 filename=sys.log |
| 1151 | 4 |
| 1152 | 5 cat /dev/null > $filename; echo "Creating / cleaning out file." |
| 1153 | 6 # Creates the file if it does not already exist, |
| 1154 | 7 #+ and truncates it to zero length if it does. |
| 1155 | 8 # : > filename and > filename also work. |
| 1156 | 9 |
| 1157 | 10 tail /var/log/messages > $filename |
| 1158 | 11 # /var/log/messages must have world read permission for this to work. |
| 1159 | 12 |
| 1160 | 13 echo "$filename contains tail end of system log." |
| 1161 | 14 |
| 1162 | 15 exit 0</PRE |
| 1163 | ></TD |
| 1164 | ></TR |
| 1165 | ></TABLE |
| 1166 | ><HR></DIV |
| 1167 | ><DIV |
| 1168 | CLASS="TIP" |
| 1169 | ><TABLE |
| 1170 | CLASS="TIP" |
| 1171 | WIDTH="90%" |
| 1172 | BORDER="0" |
| 1173 | ><TR |
| 1174 | ><TD |
| 1175 | WIDTH="25" |
| 1176 | ALIGN="CENTER" |
| 1177 | VALIGN="TOP" |
| 1178 | ><IMG |
| 1179 | SRC="common/tip.png" |
| 1180 | HSPACE="5" |
| 1181 | ALT="Tip"></TD |
| 1182 | ><TD |
| 1183 | ALIGN="LEFT" |
| 1184 | VALIGN="TOP" |
| 1185 | ><P |
| 1186 | >To list a specific line of a text file, |
| 1187 | <A |
| 1188 | HREF="special-chars.html#PIPEREF" |
| 1189 | >pipe</A |
| 1190 | > the output of |
| 1191 | <B |
| 1192 | CLASS="COMMAND" |
| 1193 | >head</B |
| 1194 | > to <B |
| 1195 | CLASS="COMMAND" |
| 1196 | >tail -n 1</B |
| 1197 | >. |
| 1198 | For example <TT |
| 1199 | CLASS="USERINPUT" |
| 1200 | ><B |
| 1201 | >head -n 8 database.txt | tail |
| 1202 | -n 1</B |
| 1203 | ></TT |
| 1204 | > lists the 8th line of the file |
| 1205 | <TT |
| 1206 | CLASS="FILENAME" |
| 1207 | >database.txt</TT |
| 1208 | >.</P |
| 1209 | ><P |
| 1210 | >To set a variable to a given block of a text file: |
| 1211 | <TABLE |
| 1212 | BORDER="0" |
| 1213 | BGCOLOR="#E0E0E0" |
| 1214 | WIDTH="90%" |
| 1215 | ><TR |
| 1216 | ><TD |
| 1217 | ><PRE |
| 1218 | CLASS="PROGRAMLISTING" |
| 1219 | > 1 var=$(head -n $m $filename | tail -n $n) |
| 1220 | 2 |
| 1221 | 3 # filename = name of file |
| 1222 | 4 # m = from beginning of file, number of lines to end of block |
| 1223 | 5 # n = number of lines to set variable to (trim from end of block)</PRE |
| 1224 | ></TD |
| 1225 | ></TR |
| 1226 | ></TABLE |
| 1227 | ></P |
| 1228 | ></TD |
| 1229 | ></TR |
| 1230 | ></TABLE |
| 1231 | ></DIV |
| 1232 | ><DIV |
| 1233 | CLASS="NOTE" |
| 1234 | ><TABLE |
| 1235 | CLASS="NOTE" |
| 1236 | WIDTH="90%" |
| 1237 | BORDER="0" |
| 1238 | ><TR |
| 1239 | ><TD |
| 1240 | WIDTH="25" |
| 1241 | ALIGN="CENTER" |
| 1242 | VALIGN="TOP" |
| 1243 | ><IMG |
| 1244 | SRC="common/note.png" |
| 1245 | HSPACE="5" |
| 1246 | ALT="Note"></TD |
| 1247 | ><TD |
| 1248 | ALIGN="LEFT" |
| 1249 | VALIGN="TOP" |
| 1250 | ><P |
| 1251 | >Newer implementations of <B |
| 1252 | CLASS="COMMAND" |
| 1253 | >tail</B |
| 1254 | > |
| 1255 | deprecate the older <B |
| 1256 | CLASS="COMMAND" |
| 1257 | >tail -$LINES |
| 1258 | filename</B |
| 1259 | > usage. The standard <B |
| 1260 | CLASS="COMMAND" |
| 1261 | >tail -n $LINES |
| 1262 | filename</B |
| 1263 | > is correct.</P |
| 1264 | ></TD |
| 1265 | ></TR |
| 1266 | ></TABLE |
| 1267 | ></DIV |
| 1268 | ><P |
| 1269 | >See also <A |
| 1270 | HREF="moreadv.html#EX41" |
| 1271 | >Example 16-5</A |
| 1272 | >, <A |
| 1273 | HREF="filearchiv.html#EX52" |
| 1274 | >Example 16-39</A |
| 1275 | > and |
| 1276 | <A |
| 1277 | HREF="debugging.html#ONLINE" |
| 1278 | >Example 32-6</A |
| 1279 | >.</P |
| 1280 | ></DD |
| 1281 | ><DT |
| 1282 | ><A |
| 1283 | NAME="GREPREF" |
| 1284 | ></A |
| 1285 | ><B |
| 1286 | CLASS="COMMAND" |
| 1287 | >grep</B |
| 1288 | ></DT |
| 1289 | ><DD |
| 1290 | ><P |
| 1291 | >A multi-purpose file search tool that uses |
| 1292 | <A |
| 1293 | HREF="regexp.html#REGEXREF" |
| 1294 | >Regular Expressions</A |
| 1295 | >. |
| 1296 | It was originally a command/filter in the |
| 1297 | venerable <B |
| 1298 | CLASS="COMMAND" |
| 1299 | >ed</B |
| 1300 | > line editor: |
| 1301 | <TT |
| 1302 | CLASS="USERINPUT" |
| 1303 | ><B |
| 1304 | >g/re/p</B |
| 1305 | ></TT |
| 1306 | > -- <I |
| 1307 | CLASS="FIRSTTERM" |
| 1308 | >global - |
| 1309 | regular expression - print</I |
| 1310 | >.</P |
| 1311 | ><P |
| 1312 | ><P |
| 1313 | ><B |
| 1314 | CLASS="COMMAND" |
| 1315 | >grep</B |
| 1316 | > <TT |
| 1317 | CLASS="REPLACEABLE" |
| 1318 | ><I |
| 1319 | >pattern</I |
| 1320 | ></TT |
| 1321 | > [<TT |
| 1322 | CLASS="REPLACEABLE" |
| 1323 | ><I |
| 1324 | >file</I |
| 1325 | ></TT |
| 1326 | >...]</P |
| 1327 | >Search the target file(s) for |
| 1328 | occurrences of <TT |
| 1329 | CLASS="REPLACEABLE" |
| 1330 | ><I |
| 1331 | >pattern</I |
| 1332 | ></TT |
| 1333 | >, where |
| 1334 | <TT |
| 1335 | CLASS="REPLACEABLE" |
| 1336 | ><I |
| 1337 | >pattern</I |
| 1338 | ></TT |
| 1339 | > may be literal text |
| 1340 | or a Regular Expression.</P |
| 1341 | ><P |
| 1342 | > <TABLE |
| 1343 | BORDER="0" |
| 1344 | BGCOLOR="#E0E0E0" |
| 1345 | WIDTH="90%" |
| 1346 | ><TR |
| 1347 | ><TD |
| 1348 | ><PRE |
| 1349 | CLASS="SCREEN" |
| 1350 | > <TT |
| 1351 | CLASS="PROMPT" |
| 1352 | >bash$ </TT |
| 1353 | ><TT |
| 1354 | CLASS="USERINPUT" |
| 1355 | ><B |
| 1356 | >grep '[rst]ystem.$' osinfo.txt</B |
| 1357 | ></TT |
| 1358 | > |
| 1359 | <TT |
| 1360 | CLASS="COMPUTEROUTPUT" |
| 1361 | >The GPL governs the distribution of the Linux operating system.</TT |
| 1362 | > |
| 1363 | </PRE |
| 1364 | ></TD |
| 1365 | ></TR |
| 1366 | ></TABLE |
| 1367 | > |
| 1368 | </P |
| 1369 | ><P |
| 1370 | >If no target file(s) specified, <B |
| 1371 | CLASS="COMMAND" |
| 1372 | >grep</B |
| 1373 | > |
| 1374 | works as a filter on <TT |
| 1375 | CLASS="FILENAME" |
| 1376 | >stdout</TT |
| 1377 | >, as in |
| 1378 | a <A |
| 1379 | HREF="special-chars.html#PIPEREF" |
| 1380 | >pipe</A |
| 1381 | >.</P |
| 1382 | ><P |
| 1383 | > <TABLE |
| 1384 | BORDER="0" |
| 1385 | BGCOLOR="#E0E0E0" |
| 1386 | WIDTH="90%" |
| 1387 | ><TR |
| 1388 | ><TD |
| 1389 | ><PRE |
| 1390 | CLASS="SCREEN" |
| 1391 | > <TT |
| 1392 | CLASS="PROMPT" |
| 1393 | >bash$ </TT |
| 1394 | ><TT |
| 1395 | CLASS="USERINPUT" |
| 1396 | ><B |
| 1397 | >ps ax | grep clock</B |
| 1398 | ></TT |
| 1399 | > |
| 1400 | <TT |
| 1401 | CLASS="COMPUTEROUTPUT" |
| 1402 | >765 tty1 S 0:00 xclock |
| 1403 | 901 pts/1 S 0:00 grep clock</TT |
| 1404 | > |
| 1405 | </PRE |
| 1406 | ></TD |
| 1407 | ></TR |
| 1408 | ></TABLE |
| 1409 | > |
| 1410 | </P |
| 1411 | ><P |
| 1412 | >The <TT |
| 1413 | CLASS="OPTION" |
| 1414 | >-i</TT |
| 1415 | > option causes a case-insensitive |
| 1416 | search.</P |
| 1417 | ><P |
| 1418 | >The <TT |
| 1419 | CLASS="OPTION" |
| 1420 | >-w</TT |
| 1421 | > option matches only whole |
| 1422 | words.</P |
| 1423 | ><P |
| 1424 | >The <TT |
| 1425 | CLASS="OPTION" |
| 1426 | >-l</TT |
| 1427 | > option lists only the files in which |
| 1428 | matches were found, but not the matching lines.</P |
| 1429 | ><P |
| 1430 | >The <TT |
| 1431 | CLASS="OPTION" |
| 1432 | >-r</TT |
| 1433 | > (recursive) option searches files in |
| 1434 | the current working directory and all subdirectories below |
| 1435 | it.</P |
| 1436 | ><P |
| 1437 | >The <TT |
| 1438 | CLASS="OPTION" |
| 1439 | >-n</TT |
| 1440 | > option lists the matching lines, |
| 1441 | together with line numbers.</P |
| 1442 | ><P |
| 1443 | > <TABLE |
| 1444 | BORDER="0" |
| 1445 | BGCOLOR="#E0E0E0" |
| 1446 | WIDTH="90%" |
| 1447 | ><TR |
| 1448 | ><TD |
| 1449 | ><PRE |
| 1450 | CLASS="SCREEN" |
| 1451 | > <TT |
| 1452 | CLASS="PROMPT" |
| 1453 | >bash$ </TT |
| 1454 | ><TT |
| 1455 | CLASS="USERINPUT" |
| 1456 | ><B |
| 1457 | >grep -n Linux osinfo.txt</B |
| 1458 | ></TT |
| 1459 | > |
| 1460 | <TT |
| 1461 | CLASS="COMPUTEROUTPUT" |
| 1462 | >2:This is a file containing information about Linux. |
| 1463 | 6:The GPL governs the distribution of the Linux operating system.</TT |
| 1464 | > |
| 1465 | </PRE |
| 1466 | ></TD |
| 1467 | ></TR |
| 1468 | ></TABLE |
| 1469 | > |
| 1470 | </P |
| 1471 | ><P |
| 1472 | >The <TT |
| 1473 | CLASS="OPTION" |
| 1474 | >-v</TT |
| 1475 | > (or <TT |
| 1476 | CLASS="OPTION" |
| 1477 | >--invert-match</TT |
| 1478 | >) |
| 1479 | option <I |
| 1480 | CLASS="FIRSTTERM" |
| 1481 | >filters out</I |
| 1482 | > matches. |
| 1483 | <TABLE |
| 1484 | BORDER="0" |
| 1485 | BGCOLOR="#E0E0E0" |
| 1486 | WIDTH="90%" |
| 1487 | ><TR |
| 1488 | ><TD |
| 1489 | ><PRE |
| 1490 | CLASS="PROGRAMLISTING" |
| 1491 | > 1 grep pattern1 *.txt | grep -v pattern2 |
| 1492 | 2 |
| 1493 | 3 # Matches all lines in "*.txt" files containing "pattern1", |
| 1494 | 4 # but ***not*** "pattern2". </PRE |
| 1495 | ></TD |
| 1496 | ></TR |
| 1497 | ></TABLE |
| 1498 | ></P |
| 1499 | ><P |
| 1500 | >The <TT |
| 1501 | CLASS="OPTION" |
| 1502 | >-c</TT |
| 1503 | > (<TT |
| 1504 | CLASS="OPTION" |
| 1505 | >--count</TT |
| 1506 | >) |
| 1507 | option gives a numerical count of matches, rather than |
| 1508 | actually listing the matches. |
| 1509 | |
| 1510 | <TABLE |
| 1511 | BORDER="0" |
| 1512 | BGCOLOR="#E0E0E0" |
| 1513 | WIDTH="90%" |
| 1514 | ><TR |
| 1515 | ><TD |
| 1516 | ><PRE |
| 1517 | CLASS="PROGRAMLISTING" |
| 1518 | > 1 grep -c txt *.sgml # (number of occurrences of "txt" in "*.sgml" files) |
| 1519 | 2 |
| 1520 | 3 |
| 1521 | 4 # grep -cz . |
| 1522 | 5 # ^ dot |
| 1523 | 6 # means count (-c) zero-separated (-z) items matching "." |
| 1524 | 7 # that is, non-empty ones (containing at least 1 character). |
| 1525 | 8 # |
| 1526 | 9 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz . # 3 |
| 1527 | 10 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '$' # 5 |
| 1528 | 11 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '^' # 5 |
| 1529 | 12 # |
| 1530 | 13 printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -c '$' # 9 |
| 1531 | 14 # By default, newline chars (\n) separate items to match. |
| 1532 | 15 |
| 1533 | 16 # Note that the -z option is GNU "grep" specific. |
| 1534 | 17 |
| 1535 | 18 |
| 1536 | 19 # Thanks, S.C.</PRE |
| 1537 | ></TD |
| 1538 | ></TR |
| 1539 | ></TABLE |
| 1540 | > |
| 1541 | </P |
| 1542 | ><P |
| 1543 | >The <TT |
| 1544 | CLASS="OPTION" |
| 1545 | >--color</TT |
| 1546 | > (or <TT |
| 1547 | CLASS="OPTION" |
| 1548 | >--colour</TT |
| 1549 | >) |
| 1550 | option marks the matching string in color (on the console |
| 1551 | or in an <I |
| 1552 | CLASS="FIRSTTERM" |
| 1553 | >xterm</I |
| 1554 | > window). Since |
| 1555 | <I |
| 1556 | CLASS="FIRSTTERM" |
| 1557 | >grep</I |
| 1558 | > prints out each entire line |
| 1559 | containing the matching pattern, this lets you see exactly |
| 1560 | <SPAN |
| 1561 | CLASS="emphasis" |
| 1562 | ><I |
| 1563 | CLASS="EMPHASIS" |
| 1564 | >what</I |
| 1565 | ></SPAN |
| 1566 | > is being matched. See also |
| 1567 | the <TT |
| 1568 | CLASS="OPTION" |
| 1569 | >-o</TT |
| 1570 | > option, which shows only the |
| 1571 | matching portion of the line(s).</P |
| 1572 | ><DIV |
| 1573 | CLASS="EXAMPLE" |
| 1574 | ><HR><A |
| 1575 | NAME="FROMSH" |
| 1576 | ></A |
| 1577 | ><P |
| 1578 | ><B |
| 1579 | >Example 16-16. Printing out the <I |
| 1580 | CLASS="FIRSTTERM" |
| 1581 | >From</I |
| 1582 | > lines in |
| 1583 | stored e-mail messages</B |
| 1584 | ></P |
| 1585 | ><TABLE |
| 1586 | BORDER="0" |
| 1587 | BGCOLOR="#E0E0E0" |
| 1588 | WIDTH="90%" |
| 1589 | ><TR |
| 1590 | ><TD |
| 1591 | ><PRE |
| 1592 | CLASS="PROGRAMLISTING" |
| 1593 | > 1 #!/bin/bash |
| 1594 | 2 # from.sh |
| 1595 | 3 |
| 1596 | 4 # Emulates the useful 'from' utility in Solaris, BSD, etc. |
| 1597 | 5 # Echoes the "From" header line in all messages |
| 1598 | 6 #+ in your e-mail directory. |
| 1599 | 7 |
| 1600 | 8 |
| 1601 | 9 MAILDIR=~/mail/* # No quoting of variable. Why? |
| 1602 | 10 # Maybe check if-exists $MAILDIR: if [ -d $MAILDIR ] . . . |
| 1603 | 11 GREP_OPTS="-H -A 5 --color" # Show file, plus extra context lines |
| 1604 | 12 #+ and display "From" in color. |
| 1605 | 13 TARGETSTR="^From" # "From" at beginning of line. |
| 1606 | 14 |
| 1607 | 15 for file in $MAILDIR # No quoting of variable. |
| 1608 | 16 do |
| 1609 | 17 grep $GREP_OPTS "$TARGETSTR" "$file" |
| 1610 | 18 # ^^^^^^^^^^ # Again, do not quote this variable. |
| 1611 | 19 echo |
| 1612 | 20 done |
| 1613 | 21 |
| 1614 | 22 exit $? |
| 1615 | 23 |
| 1616 | 24 # You might wish to pipe the output of this script to 'more' |
| 1617 | 25 #+ or redirect it to a file . . .</PRE |
| 1618 | ></TD |
| 1619 | ></TR |
| 1620 | ></TABLE |
| 1621 | ><HR></DIV |
| 1622 | ><P |
| 1623 | >When invoked with more than one target file given, |
| 1624 | <B |
| 1625 | CLASS="COMMAND" |
| 1626 | >grep</B |
| 1627 | > specifies which file contains |
| 1628 | matches.</P |
| 1629 | ><P |
| 1630 | > <TABLE |
| 1631 | BORDER="0" |
| 1632 | BGCOLOR="#E0E0E0" |
| 1633 | WIDTH="90%" |
| 1634 | ><TR |
| 1635 | ><TD |
| 1636 | ><PRE |
| 1637 | CLASS="SCREEN" |
| 1638 | > <TT |
| 1639 | CLASS="PROMPT" |
| 1640 | >bash$ </TT |
| 1641 | ><TT |
| 1642 | CLASS="USERINPUT" |
| 1643 | ><B |
| 1644 | >grep Linux osinfo.txt misc.txt</B |
| 1645 | ></TT |
| 1646 | > |
| 1647 | <TT |
| 1648 | CLASS="COMPUTEROUTPUT" |
| 1649 | >osinfo.txt:This is a file containing information about Linux. |
| 1650 | osinfo.txt:The GPL governs the distribution of the Linux operating system. |
| 1651 | misc.txt:The Linux operating system is steadily gaining in popularity.</TT |
| 1652 | > |
| 1653 | </PRE |
| 1654 | ></TD |
| 1655 | ></TR |
| 1656 | ></TABLE |
| 1657 | > |
| 1658 | </P |
| 1659 | ><DIV |
| 1660 | CLASS="TIP" |
| 1661 | ><TABLE |
| 1662 | CLASS="TIP" |
| 1663 | WIDTH="90%" |
| 1664 | BORDER="0" |
| 1665 | ><TR |
| 1666 | ><TD |
| 1667 | WIDTH="25" |
| 1668 | ALIGN="CENTER" |
| 1669 | VALIGN="TOP" |
| 1670 | ><IMG |
| 1671 | SRC="common/tip.png" |
| 1672 | HSPACE="5" |
| 1673 | ALT="Tip"></TD |
| 1674 | ><TD |
| 1675 | ALIGN="LEFT" |
| 1676 | VALIGN="TOP" |
| 1677 | ><P |
| 1678 | >To force <B |
| 1679 | CLASS="COMMAND" |
| 1680 | >grep</B |
| 1681 | > to show the filename |
| 1682 | when searching only one target file, simply give |
| 1683 | <TT |
| 1684 | CLASS="FILENAME" |
| 1685 | >/dev/null</TT |
| 1686 | > as the second file.</P |
| 1687 | ><P |
| 1688 | > <TABLE |
| 1689 | BORDER="0" |
| 1690 | BGCOLOR="#E0E0E0" |
| 1691 | WIDTH="90%" |
| 1692 | ><TR |
| 1693 | ><TD |
| 1694 | ><PRE |
| 1695 | CLASS="SCREEN" |
| 1696 | > <TT |
| 1697 | CLASS="PROMPT" |
| 1698 | >bash$ </TT |
| 1699 | ><TT |
| 1700 | CLASS="USERINPUT" |
| 1701 | ><B |
| 1702 | >grep Linux osinfo.txt /dev/null</B |
| 1703 | ></TT |
| 1704 | > |
| 1705 | <TT |
| 1706 | CLASS="COMPUTEROUTPUT" |
| 1707 | >osinfo.txt:This is a file containing information about Linux. |
| 1708 | osinfo.txt:The GPL governs the distribution of the Linux operating system.</TT |
| 1709 | > |
| 1710 | </PRE |
| 1711 | ></TD |
| 1712 | ></TR |
| 1713 | ></TABLE |
| 1714 | > |
| 1715 | </P |
| 1716 | ></TD |
| 1717 | ></TR |
| 1718 | ></TABLE |
| 1719 | ></DIV |
| 1720 | ><P |
| 1721 | >If there is a successful match, <B |
| 1722 | CLASS="COMMAND" |
| 1723 | >grep</B |
| 1724 | > |
| 1725 | returns an <A |
| 1726 | HREF="exit-status.html#EXITSTATUSREF" |
| 1727 | >exit status</A |
| 1728 | > |
| 1729 | of 0, which makes it useful in a condition test in a |
| 1730 | script, especially in combination with the <TT |
| 1731 | CLASS="OPTION" |
| 1732 | >-q</TT |
| 1733 | > |
| 1734 | option to suppress output. |
| 1735 | <TABLE |
| 1736 | BORDER="0" |
| 1737 | BGCOLOR="#E0E0E0" |
| 1738 | WIDTH="90%" |
| 1739 | ><TR |
| 1740 | ><TD |
| 1741 | ><PRE |
| 1742 | CLASS="PROGRAMLISTING" |
| 1743 | > 1 SUCCESS=0 # if grep lookup succeeds |
| 1744 | 2 word=Linux |
| 1745 | 3 filename=data.file |
| 1746 | 4 |
| 1747 | 5 grep -q "$word" "$filename" # The "-q" option |
| 1748 | 6 #+ causes nothing to echo to stdout. |
| 1749 | 7 if [ $? -eq $SUCCESS ] |
| 1750 | 8 # if grep -q "$word" "$filename" can replace lines 5 - 7. |
| 1751 | 9 then |
| 1752 | 10 echo "$word found in $filename" |
| 1753 | 11 else |
| 1754 | 12 echo "$word not found in $filename" |
| 1755 | 13 fi</PRE |
| 1756 | ></TD |
| 1757 | ></TR |
| 1758 | ></TABLE |
| 1759 | > |
| 1760 | </P |
| 1761 | ><P |
| 1762 | ><A |
| 1763 | HREF="debugging.html#ONLINE" |
| 1764 | >Example 32-6</A |
| 1765 | > demonstrates how to use |
| 1766 | <B |
| 1767 | CLASS="COMMAND" |
| 1768 | >grep</B |
| 1769 | > to search for a word pattern in |
| 1770 | a system logfile.</P |
| 1771 | ><DIV |
| 1772 | CLASS="EXAMPLE" |
| 1773 | ><HR><A |
| 1774 | NAME="GRP" |
| 1775 | ></A |
| 1776 | ><P |
| 1777 | ><B |
| 1778 | >Example 16-17. Emulating <I |
| 1779 | CLASS="FIRSTTERM" |
| 1780 | >grep</I |
| 1781 | > in a script</B |
| 1782 | ></P |
| 1783 | ><TABLE |
| 1784 | BORDER="0" |
| 1785 | BGCOLOR="#E0E0E0" |
| 1786 | WIDTH="90%" |
| 1787 | ><TR |
| 1788 | ><TD |
| 1789 | ><PRE |
| 1790 | CLASS="PROGRAMLISTING" |
| 1791 | > 1 #!/bin/bash |
| 1792 | 2 # grp.sh: Rudimentary reimplementation of grep. |
| 1793 | 3 |
| 1794 | 4 E_BADARGS=85 |
| 1795 | 5 |
| 1796 | 6 if [ -z "$1" ] # Check for argument to script. |
| 1797 | 7 then |
| 1798 | 8 echo "Usage: `basename $0` pattern" |
| 1799 | 9 exit $E_BADARGS |
| 1800 | 10 fi |
| 1801 | 11 |
| 1802 | 12 echo |
| 1803 | 13 |
| 1804 | 14 for file in * # Traverse all files in $PWD. |
| 1805 | 15 do |
| 1806 | 16 output=$(sed -n /"$1"/p $file) # Command substitution. |
| 1807 | 17 |
| 1808 | 18 if [ ! -z "$output" ] # What happens if "$output" is not quoted? |
| 1809 | 19 then |
| 1810 | 20 echo -n "$file: " |
| 1811 | 21 echo "$output" |
| 1812 | 22 fi # sed -ne "/$1/s|^|${file}: |p" is equivalent to above. |
| 1813 | 23 |
| 1814 | 24 echo |
| 1815 | 25 done |
| 1816 | 26 |
| 1817 | 27 echo |
| 1818 | 28 |
| 1819 | 29 exit 0 |
| 1820 | 30 |
| 1821 | 31 # Exercises: |
| 1822 | 32 # --------- |
| 1823 | 33 # 1) Add newlines to output, if more than one match in any given file. |
| 1824 | 34 # 2) Add features.</PRE |
| 1825 | ></TD |
| 1826 | ></TR |
| 1827 | ></TABLE |
| 1828 | ><HR></DIV |
| 1829 | ><P |
| 1830 | >How can <B |
| 1831 | CLASS="COMMAND" |
| 1832 | >grep</B |
| 1833 | > search for two (or |
| 1834 | more) separate patterns? What if you want |
| 1835 | <B |
| 1836 | CLASS="COMMAND" |
| 1837 | >grep</B |
| 1838 | > to display all lines in a file |
| 1839 | or files that contain both <SPAN |
| 1840 | CLASS="QUOTE" |
| 1841 | >"pattern1"</SPAN |
| 1842 | > |
| 1843 | <SPAN |
| 1844 | CLASS="emphasis" |
| 1845 | ><I |
| 1846 | CLASS="EMPHASIS" |
| 1847 | >and</I |
| 1848 | ></SPAN |
| 1849 | > <SPAN |
| 1850 | CLASS="QUOTE" |
| 1851 | >"pattern2"</SPAN |
| 1852 | >?</P |
| 1853 | ><P |
| 1854 | >One method is to <A |
| 1855 | HREF="special-chars.html#PIPEREF" |
| 1856 | >pipe</A |
| 1857 | > the result of <B |
| 1858 | CLASS="COMMAND" |
| 1859 | >grep |
| 1860 | pattern1</B |
| 1861 | > to <B |
| 1862 | CLASS="COMMAND" |
| 1863 | >grep pattern2</B |
| 1864 | >.</P |
| 1865 | ><P |
| 1866 | >For example, given the following file:</P |
| 1867 | ><P |
| 1868 | > <TABLE |
| 1869 | BORDER="0" |
| 1870 | BGCOLOR="#E0E0E0" |
| 1871 | WIDTH="90%" |
| 1872 | ><TR |
| 1873 | ><TD |
| 1874 | ><PRE |
| 1875 | CLASS="PROGRAMLISTING" |
| 1876 | > 1 # Filename: tstfile |
| 1877 | 2 |
| 1878 | 3 This is a sample file. |
| 1879 | 4 This is an ordinary text file. |
| 1880 | 5 This file does not contain any unusual text. |
| 1881 | 6 This file is not unusual. |
| 1882 | 7 Here is some text.</PRE |
| 1883 | ></TD |
| 1884 | ></TR |
| 1885 | ></TABLE |
| 1886 | > |
| 1887 | </P |
| 1888 | ><P |
| 1889 | >Now, let's search this file for lines containing |
| 1890 | <SPAN |
| 1891 | CLASS="emphasis" |
| 1892 | ><I |
| 1893 | CLASS="EMPHASIS" |
| 1894 | >both</I |
| 1895 | ></SPAN |
| 1896 | > <SPAN |
| 1897 | CLASS="QUOTE" |
| 1898 | >"file"</SPAN |
| 1899 | > and |
| 1900 | <SPAN |
| 1901 | CLASS="QUOTE" |
| 1902 | >"text"</SPAN |
| 1903 | > . . . </P |
| 1904 | ><TABLE |
| 1905 | BORDER="0" |
| 1906 | BGCOLOR="#E0E0E0" |
| 1907 | WIDTH="90%" |
| 1908 | ><TR |
| 1909 | ><TD |
| 1910 | ><PRE |
| 1911 | CLASS="SCREEN" |
| 1912 | > <TT |
| 1913 | CLASS="PROMPT" |
| 1914 | >bash$ </TT |
| 1915 | ><TT |
| 1916 | CLASS="USERINPUT" |
| 1917 | ><B |
| 1918 | >grep file tstfile</B |
| 1919 | ></TT |
| 1920 | > |
| 1921 | <TT |
| 1922 | CLASS="COMPUTEROUTPUT" |
| 1923 | ># Filename: tstfile |
| 1924 | This is a sample file. |
| 1925 | This is an ordinary text file. |
| 1926 | This file does not contain any unusual text. |
| 1927 | This file is not unusual.</TT |
| 1928 | > |
| 1929 | |
| 1930 | <TT |
| 1931 | CLASS="PROMPT" |
| 1932 | >bash$ </TT |
| 1933 | ><TT |
| 1934 | CLASS="USERINPUT" |
| 1935 | ><B |
| 1936 | >grep file tstfile | grep text</B |
| 1937 | ></TT |
| 1938 | > |
| 1939 | <TT |
| 1940 | CLASS="COMPUTEROUTPUT" |
| 1941 | >This is an ordinary text file. |
| 1942 | This file does not contain any unusual text.</TT |
| 1943 | ></PRE |
| 1944 | ></TD |
| 1945 | ></TR |
| 1946 | ></TABLE |
| 1947 | ><P |
| 1948 | >Now, for an interesting recreational use |
| 1949 | of <I |
| 1950 | CLASS="FIRSTTERM" |
| 1951 | >grep</I |
| 1952 | > . . .</P |
| 1953 | ><DIV |
| 1954 | CLASS="EXAMPLE" |
| 1955 | ><HR><A |
| 1956 | NAME="CWSOLVER" |
| 1957 | ></A |
| 1958 | ><P |
| 1959 | ><B |
| 1960 | >Example 16-18. Crossword puzzle solver</B |
| 1961 | ></P |
| 1962 | ><TABLE |
| 1963 | BORDER="0" |
| 1964 | BGCOLOR="#E0E0E0" |
| 1965 | WIDTH="90%" |
| 1966 | ><TR |
| 1967 | ><TD |
| 1968 | ><PRE |
| 1969 | CLASS="PROGRAMLISTING" |
| 1970 | > 1 #!/bin/bash |
| 1971 | 2 # cw-solver.sh |
| 1972 | 3 # This is actually a wrapper around a one-liner (line 46). |
| 1973 | 4 |
| 1974 | 5 # Crossword puzzle and anagramming word game solver. |
| 1975 | 6 # You know *some* of the letters in the word you're looking for, |
| 1976 | 7 #+ so you need a list of all valid words |
| 1977 | 8 #+ with the known letters in given positions. |
| 1978 | 9 # For example: w...i....n |
| 1979 | 10 # 1???5????10 |
| 1980 | 11 # w in position 1, 3 unknowns, i in the 5th, 4 unknowns, n at the end. |
| 1981 | 12 # (See comments at end of script.) |
| 1982 | 13 |
| 1983 | 14 |
| 1984 | 15 E_NOPATT=71 |
| 1985 | 16 DICT=/usr/share/dict/word.lst |
| 1986 | 17 # ^^^^^^^^ Looks for word list here. |
| 1987 | 18 # ASCII word list, one word per line. |
| 1988 | 19 # If you happen to need an appropriate list, |
| 1989 | 20 #+ download the author's "yawl" word list package. |
| 1990 | 21 # http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz |
| 1991 | 22 # or |
| 1992 | 23 # http://bash.deta.in/yawl-0.3.2.tar.gz |
| 1993 | 24 |
| 1994 | 25 |
| 1995 | 26 if [ -z "$1" ] # If no word pattern specified |
| 1996 | 27 then #+ as a command-line argument . . . |
| 1997 | 28 echo #+ . . . then . . . |
| 1998 | 29 echo "Usage:" #+ Usage message. |
| 1999 | 30 echo |
| 2000 | 31 echo ""$0" \"pattern,\"" |
| 2001 | 32 echo "where \"pattern\" is in the form" |
| 2002 | 33 echo "xxx..x.x..." |
| 2003 | 34 echo |
| 2004 | 35 echo "The x's represent known letters," |
| 2005 | 36 echo "and the periods are unknown letters (blanks)." |
| 2006 | 37 echo "Letters and periods can be in any position." |
| 2007 | 38 echo "For example, try: sh cw-solver.sh w...i....n" |
| 2008 | 39 echo |
| 2009 | 40 exit $E_NOPATT |
| 2010 | 41 fi |
| 2011 | 42 |
| 2012 | 43 echo |
| 2013 | 44 # =============================================== |
| 2014 | 45 # This is where all the work gets done. |
| 2015 | 46 grep ^"$1"$ "$DICT" # Yes, only one line! |
| 2016 | 47 # | | |
| 2017 | 48 # ^ is start-of-word regex anchor. |
| 2018 | 49 # $ is end-of-word regex anchor. |
| 2019 | 50 |
| 2020 | 51 # From _Stupid Grep Tricks_, vol. 1, |
| 2021 | 52 #+ a book the ABS Guide author may yet get around |
| 2022 | 53 #+ to writing . . . one of these days . . . |
| 2023 | 54 # =============================================== |
| 2024 | 55 echo |
| 2025 | 56 |
| 2026 | 57 |
| 2027 | 58 exit $? # Script terminates here. |
| 2028 | 59 # If there are too many words generated, |
| 2029 | 60 #+ redirect the output to a file. |
| 2030 | 61 |
| 2031 | 62 $ sh cw-solver.sh w...i....n |
| 2032 | 63 |
| 2033 | 64 wellington |
| 2034 | 65 workingman |
| 2035 | 66 workingmen</PRE |
| 2036 | ></TD |
| 2037 | ></TR |
| 2038 | ></TABLE |
| 2039 | ><HR></DIV |
| 2040 | ><P |
| 2041 | ><A |
| 2042 | NAME="EGREPREF" |
| 2043 | ></A |
| 2044 | ><B |
| 2045 | CLASS="COMMAND" |
| 2046 | >egrep</B |
| 2047 | > |
| 2048 | -- <I |
| 2049 | CLASS="FIRSTTERM" |
| 2050 | >extended grep</I |
| 2051 | > -- is the same |
| 2052 | as <B |
| 2053 | CLASS="COMMAND" |
| 2054 | >grep -E</B |
| 2055 | >. This uses a somewhat |
| 2056 | different, extended set of <A |
| 2057 | HREF="regexp.html#REGEXREF" |
| 2058 | >Regular |
| 2059 | Expressions</A |
| 2060 | >, which can make the search a bit more |
| 2061 | flexible. It also allows the boolean | |
| 2062 | (<I |
| 2063 | CLASS="FIRSTTERM" |
| 2064 | >or</I |
| 2065 | >) operator. |
| 2066 | <TABLE |
| 2067 | BORDER="0" |
| 2068 | BGCOLOR="#E0E0E0" |
| 2069 | WIDTH="90%" |
| 2070 | ><TR |
| 2071 | ><TD |
| 2072 | ><PRE |
| 2073 | CLASS="SCREEN" |
| 2074 | > <TT |
| 2075 | CLASS="PROMPT" |
| 2076 | >bash $ </TT |
| 2077 | ><TT |
| 2078 | CLASS="USERINPUT" |
| 2079 | ><B |
| 2080 | >egrep 'matches|Matches' file.txt</B |
| 2081 | ></TT |
| 2082 | > |
| 2083 | <TT |
| 2084 | CLASS="COMPUTEROUTPUT" |
| 2085 | >Line 1 matches. |
| 2086 | Line 3 Matches. |
| 2087 | Line 4 contains matches, but also Matches</TT |
| 2088 | > |
| 2089 | </PRE |
| 2090 | ></TD |
| 2091 | ></TR |
| 2092 | ></TABLE |
| 2093 | > |
| 2094 | </P |
| 2095 | ><P |
| 2096 | ><A |
| 2097 | NAME="FGREPREF" |
| 2098 | ></A |
| 2099 | ><B |
| 2100 | CLASS="COMMAND" |
| 2101 | >fgrep</B |
| 2102 | > -- |
| 2103 | <I |
| 2104 | CLASS="FIRSTTERM" |
| 2105 | >fast grep</I |
| 2106 | > -- is the same as |
| 2107 | <B |
| 2108 | CLASS="COMMAND" |
| 2109 | >grep -F</B |
| 2110 | >. It does a literal string search |
| 2111 | (no <A |
| 2112 | HREF="regexp.html#REGEXREF" |
| 2113 | >Regular Expressions</A |
| 2114 | >), |
| 2115 | which generally speeds things up a bit.</P |
| 2116 | ><DIV |
| 2117 | CLASS="NOTE" |
| 2118 | ><TABLE |
| 2119 | CLASS="NOTE" |
| 2120 | WIDTH="90%" |
| 2121 | BORDER="0" |
| 2122 | ><TR |
| 2123 | ><TD |
| 2124 | WIDTH="25" |
| 2125 | ALIGN="CENTER" |
| 2126 | VALIGN="TOP" |
| 2127 | ><IMG |
| 2128 | SRC="common/note.png" |
| 2129 | HSPACE="5" |
| 2130 | ALT="Note"></TD |
| 2131 | ><TD |
| 2132 | ALIGN="LEFT" |
| 2133 | VALIGN="TOP" |
| 2134 | ><P |
| 2135 | >On some Linux distros, <B |
| 2136 | CLASS="COMMAND" |
| 2137 | >egrep</B |
| 2138 | > and |
| 2139 | <B |
| 2140 | CLASS="COMMAND" |
| 2141 | >fgrep</B |
| 2142 | > are symbolic links to, or aliases for |
| 2143 | <B |
| 2144 | CLASS="COMMAND" |
| 2145 | >grep</B |
| 2146 | >, but invoked with the |
| 2147 | <TT |
| 2148 | CLASS="OPTION" |
| 2149 | >-E</TT |
| 2150 | > and <TT |
| 2151 | CLASS="OPTION" |
| 2152 | >-F</TT |
| 2153 | > options, |
| 2154 | respectively.</P |
| 2155 | ></TD |
| 2156 | ></TR |
| 2157 | ></TABLE |
| 2158 | ></DIV |
| 2159 | ><DIV |
| 2160 | CLASS="EXAMPLE" |
| 2161 | ><HR><A |
| 2162 | NAME="DICTLOOKUP" |
| 2163 | ></A |
| 2164 | ><P |
| 2165 | ><B |
| 2166 | >Example 16-19. Looking up definitions in <I |
| 2167 | CLASS="CITETITLE" |
| 2168 | >Webster's 1913 Dictionary</I |
| 2169 | ></B |
| 2170 | ></P |
| 2171 | ><TABLE |
| 2172 | BORDER="0" |
| 2173 | BGCOLOR="#E0E0E0" |
| 2174 | WIDTH="90%" |
| 2175 | ><TR |
| 2176 | ><TD |
| 2177 | ><PRE |
| 2178 | CLASS="PROGRAMLISTING" |
| 2179 | > 1 #!/bin/bash |
| 2180 | 2 # dict-lookup.sh |
| 2181 | 3 |
| 2182 | 4 # This script looks up definitions in the 1913 Webster's Dictionary. |
| 2183 | 5 # This Public Domain dictionary is available for download |
| 2184 | 6 #+ from various sites, including |
| 2185 | 7 #+ Project Gutenberg (http://www.gutenberg.org/etext/247). |
| 2186 | 8 # |
| 2187 | 9 # Convert it from DOS to UNIX format (with only LF at end of line) |
| 2188 | 10 #+ before using it with this script. |
| 2189 | 11 # Store the file in plain, uncompressed ASCII text. |
| 2190 | 12 # Set DEFAULT_DICTFILE variable below to path/filename. |
| 2191 | 13 |
| 2192 | 14 |
| 2193 | 15 E_BADARGS=85 |
| 2194 | 16 MAXCONTEXTLINES=50 # Maximum number of lines to show. |
| 2195 | 17 DEFAULT_DICTFILE="/usr/share/dict/webster1913-dict.txt" |
| 2196 | 18 # Default dictionary file pathname. |
| 2197 | 19 # Change this as necessary. |
| 2198 | 20 # Note: |
| 2199 | 21 # ---- |
| 2200 | 22 # This particular edition of the 1913 Webster's |
| 2201 | 23 #+ begins each entry with an uppercase letter |
| 2202 | 24 #+ (lowercase for the remaining characters). |
| 2203 | 25 # Only the *very first line* of an entry begins this way, |
| 2204 | 26 #+ and that's why the search algorithm below works. |
| 2205 | 27 |
| 2206 | 28 |
| 2207 | 29 |
| 2208 | 30 if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]] |
| 2209 | 31 # Must at least specify word to look up, and |
| 2210 | 32 #+ it must start with an uppercase letter. |
| 2211 | 33 then |
| 2212 | 34 echo "Usage: `basename $0` Word-to-define [dictionary-file]" |
| 2213 | 35 echo |
| 2214 | 36 echo "Note: Word to look up must start with capital letter," |
| 2215 | 37 echo "with the rest of the word in lowercase." |
| 2216 | 38 echo "--------------------------------------------" |
| 2217 | 39 echo "Examples: Abandon, Dictionary, Marking, etc." |
| 2218 | 40 exit $E_BADARGS |
| 2219 | 41 fi |
| 2220 | 42 |
| 2221 | 43 |
| 2222 | 44 if [ -z "$2" ] # May specify different dictionary |
| 2223 | 45 #+ as an argument to this script. |
| 2224 | 46 then |
| 2225 | 47 dictfile=$DEFAULT_DICTFILE |
| 2226 | 48 else |
| 2227 | 49 dictfile="$2" |
| 2228 | 50 fi |
| 2229 | 51 |
| 2230 | 52 # --------------------------------------------------------- |
| 2231 | 53 Definition=$(fgrep -A $MAXCONTEXTLINES "$1 \\" "$dictfile") |
| 2232 | 54 # Definitions in form "Word \..." |
| 2233 | 55 # |
| 2234 | 56 # And, yes, "fgrep" is fast enough |
| 2235 | 57 #+ to search even a very large text file. |
| 2236 | 58 |
| 2237 | 59 |
| 2238 | 60 # Now, snip out just the definition block. |
| 2239 | 61 |
| 2240 | 62 echo "$Definition" | |
| 2241 | 63 sed -n '1,/^[A-Z]/p' | |
| 2242 | 64 # Print from first line of output |
| 2243 | 65 #+ to the first line of the next entry. |
| 2244 | 66 sed '$d' | sed '$d' |
| 2245 | 67 # Delete last two lines of output |
| 2246 | 68 #+ (blank line and first line of next entry). |
| 2247 | 69 # --------------------------------------------------------- |
| 2248 | 70 |
| 2249 | 71 exit $? |
| 2250 | 72 |
| 2251 | 73 # Exercises: |
| 2252 | 74 # --------- |
| 2253 | 75 # 1) Modify the script to accept any type of alphabetic input |
| 2254 | 76 # + (uppercase, lowercase, mixed case), and convert it |
| 2255 | 77 # + to an acceptable format for processing. |
| 2256 | 78 # |
| 2257 | 79 # 2) Convert the script to a GUI application, |
| 2258 | 80 # + using something like 'gdialog' or 'zenity' . . . |
| 2259 | 81 # The script will then no longer take its argument(s) |
| 2260 | 82 # + from the command-line. |
| 2261 | 83 # |
| 2262 | 84 # 3) Modify the script to parse one of the other available |
| 2263 | 85 # + Public Domain Dictionaries, such as the U.S. Census Bureau Gazetteer.</PRE |
| 2264 | ></TD |
| 2265 | ></TR |
| 2266 | ></TABLE |
| 2267 | ><HR></DIV |
| 2268 | ><DIV |
| 2269 | CLASS="NOTE" |
| 2270 | ><TABLE |
| 2271 | CLASS="NOTE" |
| 2272 | WIDTH="90%" |
| 2273 | BORDER="0" |
| 2274 | ><TR |
| 2275 | ><TD |
| 2276 | WIDTH="25" |
| 2277 | ALIGN="CENTER" |
| 2278 | VALIGN="TOP" |
| 2279 | ><IMG |
| 2280 | SRC="common/note.png" |
| 2281 | HSPACE="5" |
| 2282 | ALT="Note"></TD |
| 2283 | ><TD |
| 2284 | ALIGN="LEFT" |
| 2285 | VALIGN="TOP" |
| 2286 | ><P |
| 2287 | >See also <A |
| 2288 | HREF="contributed-scripts.html#QKY" |
| 2289 | >Example A-41</A |
| 2290 | > for an example |
| 2291 | of speedy <I |
| 2292 | CLASS="FIRSTTERM" |
| 2293 | >fgrep</I |
| 2294 | > lookup on a large |
| 2295 | text file.</P |
| 2296 | ></TD |
| 2297 | ></TR |
| 2298 | ></TABLE |
| 2299 | ></DIV |
| 2300 | ><P |
| 2301 | ><A |
| 2302 | NAME="AGREPREF" |
| 2303 | ></A |
| 2304 | ></P |
| 2305 | ><P |
| 2306 | ><B |
| 2307 | CLASS="COMMAND" |
| 2308 | >agrep</B |
| 2309 | > (<I |
| 2310 | CLASS="FIRSTTERM" |
| 2311 | >approximate |
| 2312 | grep</I |
| 2313 | >) extends the capabilities of |
| 2314 | <B |
| 2315 | CLASS="COMMAND" |
| 2316 | >grep</B |
| 2317 | > to approximate matching. The search |
| 2318 | string may differ by a specified number of characters |
| 2319 | from the resulting matches. This utility is not part of |
| 2320 | the core Linux distribution.</P |
| 2321 | ><P |
| 2322 | ><A |
| 2323 | NAME="ZEGREPREF" |
| 2324 | ></A |
| 2325 | ></P |
| 2326 | ><DIV |
| 2327 | CLASS="TIP" |
| 2328 | ><TABLE |
| 2329 | CLASS="TIP" |
| 2330 | WIDTH="90%" |
| 2331 | BORDER="0" |
| 2332 | ><TR |
| 2333 | ><TD |
| 2334 | WIDTH="25" |
| 2335 | ALIGN="CENTER" |
| 2336 | VALIGN="TOP" |
| 2337 | ><IMG |
| 2338 | SRC="common/tip.png" |
| 2339 | HSPACE="5" |
| 2340 | ALT="Tip"></TD |
| 2341 | ><TD |
| 2342 | ALIGN="LEFT" |
| 2343 | VALIGN="TOP" |
| 2344 | ><P |
| 2345 | >To search compressed files, use |
| 2346 | <B |
| 2347 | CLASS="COMMAND" |
| 2348 | >zgrep</B |
| 2349 | >, <B |
| 2350 | CLASS="COMMAND" |
| 2351 | >zegrep</B |
| 2352 | >, or |
| 2353 | <B |
| 2354 | CLASS="COMMAND" |
| 2355 | >zfgrep</B |
| 2356 | >. These also work on non-compressed |
| 2357 | files, though slower than plain <B |
| 2358 | CLASS="COMMAND" |
| 2359 | >grep</B |
| 2360 | >, |
| 2361 | <B |
| 2362 | CLASS="COMMAND" |
| 2363 | >egrep</B |
| 2364 | >, <B |
| 2365 | CLASS="COMMAND" |
| 2366 | >fgrep</B |
| 2367 | >. |
| 2368 | They are handy for searching through a mixed set of files, |
| 2369 | some compressed, some not.</P |
| 2370 | ><P |
| 2371 | ><A |
| 2372 | NAME="BZGREPREF" |
| 2373 | ></A |
| 2374 | ></P |
| 2375 | ><P |
| 2376 | >To search <A |
| 2377 | HREF="filearchiv.html#BZIPREF" |
| 2378 | >bzipped</A |
| 2379 | > |
| 2380 | files, use <B |
| 2381 | CLASS="COMMAND" |
| 2382 | >bzgrep</B |
| 2383 | >.</P |
| 2384 | ></TD |
| 2385 | ></TR |
| 2386 | ></TABLE |
| 2387 | ></DIV |
| 2388 | ></DD |
| 2389 | ><DT |
| 2390 | ><A |
| 2391 | NAME="LOOKREF" |
| 2392 | ></A |
| 2393 | ><B |
| 2394 | CLASS="COMMAND" |
| 2395 | >look</B |
| 2396 | ></DT |
| 2397 | ><DD |
| 2398 | ><P |
| 2399 | >The command <B |
| 2400 | CLASS="COMMAND" |
| 2401 | >look</B |
| 2402 | > works like |
| 2403 | <B |
| 2404 | CLASS="COMMAND" |
| 2405 | >grep</B |
| 2406 | >, but does a lookup on |
| 2407 | a <SPAN |
| 2408 | CLASS="QUOTE" |
| 2409 | >"dictionary,"</SPAN |
| 2410 | > a sorted word list. |
| 2411 | By default, <B |
| 2412 | CLASS="COMMAND" |
| 2413 | >look</B |
| 2414 | > searches for a match |
| 2415 | in <TT |
| 2416 | CLASS="FILENAME" |
| 2417 | >/usr/dict/words</TT |
| 2418 | >, but a different |
| 2419 | dictionary file may be specified.</P |
| 2420 | ><DIV |
| 2421 | CLASS="EXAMPLE" |
| 2422 | ><HR><A |
| 2423 | NAME="LOOKUP" |
| 2424 | ></A |
| 2425 | ><P |
| 2426 | ><B |
| 2427 | >Example 16-20. Checking words in a list for validity</B |
| 2428 | ></P |
| 2429 | ><TABLE |
| 2430 | BORDER="0" |
| 2431 | BGCOLOR="#E0E0E0" |
| 2432 | WIDTH="90%" |
| 2433 | ><TR |
| 2434 | ><TD |
| 2435 | ><PRE |
| 2436 | CLASS="PROGRAMLISTING" |
| 2437 | > 1 #!/bin/bash |
| 2438 | 2 # lookup: Does a dictionary lookup on each word in a data file. |
| 2439 | 3 |
| 2440 | 4 file=words.data # Data file from which to read words to test. |
| 2441 | 5 |
| 2442 | 6 echo |
| 2443 | 7 echo "Testing file $file" |
| 2444 | 8 echo |
| 2445 | 9 |
| 2446 | 10 while [ "$word" != end ] # Last word in data file. |
| 2447 | 11 do # ^^^ |
| 2448 | 12 read word # From data file, because of redirection at end of loop. |
| 2449 | 13 look $word > /dev/null # Don't want to display lines in dictionary file. |
| 2450 | 14 # Searches for words in the file /usr/share/dict/words |
| 2451 | 15 #+ (usually a link to linux.words). |
| 2452 | 16 lookup=$? # Exit status of 'look' command. |
| 2453 | 17 |
| 2454 | 18 if [ "$lookup" -eq 0 ] |
| 2455 | 19 then |
| 2456 | 20 echo "\"$word\" is valid." |
| 2457 | 21 else |
| 2458 | 22 echo "\"$word\" is invalid." |
| 2459 | 23 fi |
| 2460 | 24 |
| 2461 | 25 done <"$file" # Redirects stdin to $file, so "reads" come from there. |
| 2462 | 26 |
| 2463 | 27 echo |
| 2464 | 28 |
| 2465 | 29 exit 0 |
| 2466 | 30 |
| 2467 | 31 # ---------------------------------------------------------------- |
| 2468 | 32 # Code below line will not execute because of "exit" command above. |
| 2469 | 33 |
| 2470 | 34 |
| 2471 | 35 # Stephane Chazelas proposes the following, more concise alternative: |
| 2472 | 36 |
| 2473 | 37 while read word && [[ $word != end ]] |
| 2474 | 38 do if look "$word" > /dev/null |
| 2475 | 39 then echo "\"$word\" is valid." |
| 2476 | 40 else echo "\"$word\" is invalid." |
| 2477 | 41 fi |
| 2478 | 42 done <"$file" |
| 2479 | 43 |
| 2480 | 44 exit 0</PRE |
| 2481 | ></TD |
| 2482 | ></TR |
| 2483 | ></TABLE |
| 2484 | ><HR></DIV |
| 2485 | ></DD |
| 2486 | ><DT |
| 2487 | ><B |
| 2488 | CLASS="COMMAND" |
| 2489 | >sed</B |
| 2490 | >, <B |
| 2491 | CLASS="COMMAND" |
| 2492 | >awk</B |
| 2493 | ></DT |
| 2494 | ><DD |
| 2495 | ><P |
| 2496 | >Scripting languages especially suited for parsing text |
| 2497 | files and command output. May be embedded singly or in |
| 2498 | combination in pipes and shell scripts.</P |
| 2499 | ></DD |
| 2500 | ><DT |
| 2501 | ><B |
| 2502 | CLASS="COMMAND" |
| 2503 | ><A |
| 2504 | HREF="sedawk.html#SEDREF" |
| 2505 | >sed</A |
| 2506 | ></B |
| 2507 | ></DT |
| 2508 | ><DD |
| 2509 | ><P |
| 2510 | >Non-interactive <SPAN |
| 2511 | CLASS="QUOTE" |
| 2512 | >"stream editor"</SPAN |
| 2513 | >, permits using |
| 2514 | many <B |
| 2515 | CLASS="COMMAND" |
| 2516 | >ex</B |
| 2517 | > commands in <A |
| 2518 | HREF="timedate.html#BATCHPROCREF" |
| 2519 | >batch</A |
| 2520 | > mode. It finds many |
| 2521 | uses in shell scripts.</P |
| 2522 | ></DD |
| 2523 | ><DT |
| 2524 | ><B |
| 2525 | CLASS="COMMAND" |
| 2526 | ><A |
| 2527 | HREF="awk.html#AWKREF" |
| 2528 | >awk</A |
| 2529 | ></B |
| 2530 | ></DT |
| 2531 | ><DD |
| 2532 | ><P |
| 2533 | >Programmable file extractor and formatter, good for |
| 2534 | manipulating and/or extracting <A |
| 2535 | HREF="special-chars.html#FIELDREF" |
| 2536 | >fields</A |
| 2537 | > (columns) in structured |
| 2538 | text files. Its syntax is similar to C.</P |
| 2539 | ></DD |
| 2540 | ><DT |
| 2541 | ><A |
| 2542 | NAME="WCREF" |
| 2543 | ></A |
| 2544 | ><B |
| 2545 | CLASS="COMMAND" |
| 2546 | >wc</B |
| 2547 | ></DT |
| 2548 | ><DD |
| 2549 | ><P |
| 2550 | ><I |
| 2551 | CLASS="FIRSTTERM" |
| 2552 | >wc</I |
| 2553 | > gives a <SPAN |
| 2554 | CLASS="QUOTE" |
| 2555 | >"word |
| 2556 | count"</SPAN |
| 2557 | > on a file or I/O stream: |
| 2558 | |
| 2559 | <TABLE |
| 2560 | BORDER="0" |
| 2561 | BGCOLOR="#E0E0E0" |
| 2562 | WIDTH="90%" |
| 2563 | ><TR |
| 2564 | ><TD |
| 2565 | ><PRE |
| 2566 | CLASS="SCREEN" |
| 2567 | > <TT |
| 2568 | CLASS="PROMPT" |
| 2569 | >bash $ </TT |
| 2570 | ><TT |
| 2571 | CLASS="USERINPUT" |
| 2572 | ><B |
| 2573 | >wc /usr/share/doc/sed-4.1.2/README</B |
| 2574 | ></TT |
| 2575 | > |
| 2576 | <TT |
| 2577 | CLASS="COMPUTEROUTPUT" |
| 2578 | >13 70 447 README</TT |
| 2579 | > |
| 2580 | [13 lines 70 words 447 characters]</PRE |
| 2581 | ></TD |
| 2582 | ></TR |
| 2583 | ></TABLE |
| 2584 | ></P |
| 2585 | ><P |
| 2586 | ><TT |
| 2587 | CLASS="USERINPUT" |
| 2588 | ><B |
| 2589 | >wc -w</B |
| 2590 | ></TT |
| 2591 | > gives only the word count.</P |
| 2592 | ><P |
| 2593 | ><TT |
| 2594 | CLASS="USERINPUT" |
| 2595 | ><B |
| 2596 | >wc -l</B |
| 2597 | ></TT |
| 2598 | > gives only the line count.</P |
| 2599 | ><P |
| 2600 | ><TT |
| 2601 | CLASS="USERINPUT" |
| 2602 | ><B |
| 2603 | >wc -c</B |
| 2604 | ></TT |
| 2605 | > gives only the byte count.</P |
| 2606 | ><P |
| 2607 | ><TT |
| 2608 | CLASS="USERINPUT" |
| 2609 | ><B |
| 2610 | >wc -m</B |
| 2611 | ></TT |
| 2612 | > gives only the character count.</P |
| 2613 | ><P |
| 2614 | ><TT |
| 2615 | CLASS="USERINPUT" |
| 2616 | ><B |
| 2617 | >wc -L</B |
| 2618 | ></TT |
| 2619 | > gives only the length of the longest line.</P |
| 2620 | ><P |
| 2621 | >Using <B |
| 2622 | CLASS="COMMAND" |
| 2623 | >wc</B |
| 2624 | > to count how many |
| 2625 | <TT |
| 2626 | CLASS="FILENAME" |
| 2627 | >.txt</TT |
| 2628 | > files are in current working directory: |
| 2629 | <TABLE |
| 2630 | BORDER="0" |
| 2631 | BGCOLOR="#E0E0E0" |
| 2632 | WIDTH="90%" |
| 2633 | ><TR |
| 2634 | ><TD |
| 2635 | ><PRE |
| 2636 | CLASS="PROGRAMLISTING" |
| 2637 | > 1 $ ls *.txt | wc -l |
| 2638 | 2 # Will work as long as none of the "*.txt" files |
| 2639 | 3 #+ have a linefeed embedded in their name. |
| 2640 | 4 |
| 2641 | 5 # Alternative ways of doing this are: |
| 2642 | 6 # find . -maxdepth 1 -name \*.txt -print0 | grep -cz . |
| 2643 | 7 # (shopt -s nullglob; set -- *.txt; echo $#) |
| 2644 | 8 |
| 2645 | 9 # Thanks, S.C.</PRE |
| 2646 | ></TD |
| 2647 | ></TR |
| 2648 | ></TABLE |
| 2649 | > |
| 2650 | </P |
| 2651 | ><P |
| 2652 | >Using <B |
| 2653 | CLASS="COMMAND" |
| 2654 | >wc</B |
| 2655 | > to total up the size of all the |
| 2656 | files whose names begin with letters in the range d - h |
| 2657 | <TABLE |
| 2658 | BORDER="0" |
| 2659 | BGCOLOR="#E0E0E0" |
| 2660 | WIDTH="90%" |
| 2661 | ><TR |
| 2662 | ><TD |
| 2663 | ><PRE |
| 2664 | CLASS="SCREEN" |
| 2665 | > <TT |
| 2666 | CLASS="PROMPT" |
| 2667 | >bash$ </TT |
| 2668 | ><TT |
| 2669 | CLASS="USERINPUT" |
| 2670 | ><B |
| 2671 | >wc [d-h]* | grep total | awk '{print $3}'</B |
| 2672 | ></TT |
| 2673 | > |
| 2674 | <TT |
| 2675 | CLASS="COMPUTEROUTPUT" |
| 2676 | >71832</TT |
| 2677 | > |
| 2678 | </PRE |
| 2679 | ></TD |
| 2680 | ></TR |
| 2681 | ></TABLE |
| 2682 | > |
| 2683 | </P |
| 2684 | ><P |
| 2685 | >Using <B |
| 2686 | CLASS="COMMAND" |
| 2687 | >wc</B |
| 2688 | > to count the instances of the |
| 2689 | word <SPAN |
| 2690 | CLASS="QUOTE" |
| 2691 | >"Linux"</SPAN |
| 2692 | > in the main source file for |
| 2693 | this book. |
| 2694 | <TABLE |
| 2695 | BORDER="0" |
| 2696 | BGCOLOR="#E0E0E0" |
| 2697 | WIDTH="90%" |
| 2698 | ><TR |
| 2699 | ><TD |
| 2700 | ><PRE |
| 2701 | CLASS="SCREEN" |
| 2702 | > <TT |
| 2703 | CLASS="PROMPT" |
| 2704 | >bash$ </TT |
| 2705 | ><TT |
| 2706 | CLASS="USERINPUT" |
| 2707 | ><B |
| 2708 | >grep Linux abs-book.sgml | wc -l</B |
| 2709 | ></TT |
| 2710 | > |
| 2711 | <TT |
| 2712 | CLASS="COMPUTEROUTPUT" |
| 2713 | >138</TT |
| 2714 | > |
| 2715 | </PRE |
| 2716 | ></TD |
| 2717 | ></TR |
| 2718 | ></TABLE |
| 2719 | > |
| 2720 | </P |
| 2721 | ><P |
| 2722 | >See also <A |
| 2723 | HREF="filearchiv.html#EX52" |
| 2724 | >Example 16-39</A |
| 2725 | > and <A |
| 2726 | HREF="redircb.html#REDIR4" |
| 2727 | >Example 20-8</A |
| 2728 | >.</P |
| 2729 | ><P |
| 2730 | >Certain commands include some of the |
| 2731 | functionality of <B |
| 2732 | CLASS="COMMAND" |
| 2733 | >wc</B |
| 2734 | > as options. |
| 2735 | |
| 2736 | <TABLE |
| 2737 | BORDER="0" |
| 2738 | BGCOLOR="#E0E0E0" |
| 2739 | WIDTH="90%" |
| 2740 | ><TR |
| 2741 | ><TD |
| 2742 | ><PRE |
| 2743 | CLASS="PROGRAMLISTING" |
| 2744 | > 1 ... | grep foo | wc -l |
| 2745 | 2 # This frequently used construct can be more concisely rendered. |
| 2746 | 3 |
| 2747 | 4 ... | grep -c foo |
| 2748 | 5 # Just use the "-c" (or "--count") option of grep. |
| 2749 | 6 |
| 2750 | 7 # Thanks, S.C.</PRE |
| 2751 | ></TD |
| 2752 | ></TR |
| 2753 | ></TABLE |
| 2754 | ></P |
| 2755 | ></DD |
| 2756 | ><DT |
| 2757 | ><A |
| 2758 | NAME="TRREF" |
| 2759 | ></A |
| 2760 | ><B |
| 2761 | CLASS="COMMAND" |
| 2762 | >tr</B |
| 2763 | ></DT |
| 2764 | ><DD |
| 2765 | ><P |
| 2766 | >character translation filter.</P |
| 2767 | ><DIV |
| 2768 | CLASS="CAUTION" |
| 2769 | ><TABLE |
| 2770 | CLASS="CAUTION" |
| 2771 | WIDTH="90%" |
| 2772 | BORDER="0" |
| 2773 | ><TR |
| 2774 | ><TD |
| 2775 | WIDTH="25" |
| 2776 | ALIGN="CENTER" |
| 2777 | VALIGN="TOP" |
| 2778 | ><IMG |
| 2779 | SRC="common/caution.png" |
| 2780 | HSPACE="5" |
| 2781 | ALT="Caution"></TD |
| 2782 | ><TD |
| 2783 | ALIGN="LEFT" |
| 2784 | VALIGN="TOP" |
| 2785 | ><P |
| 2786 | ><A |
| 2787 | HREF="special-chars.html#UCREF" |
| 2788 | >Must use quoting and/or |
| 2789 | brackets</A |
| 2790 | >, as appropriate. Quotes prevent the |
| 2791 | shell from reinterpreting the special characters in |
| 2792 | <B |
| 2793 | CLASS="COMMAND" |
| 2794 | >tr</B |
| 2795 | > command sequences. Brackets should be |
| 2796 | quoted to prevent expansion by the shell. </P |
| 2797 | ></TD |
| 2798 | ></TR |
| 2799 | ></TABLE |
| 2800 | ></DIV |
| 2801 | ><P |
| 2802 | >Either <TT |
| 2803 | CLASS="USERINPUT" |
| 2804 | ><B |
| 2805 | >tr "A-Z" "*" <filename</B |
| 2806 | ></TT |
| 2807 | > |
| 2808 | or <TT |
| 2809 | CLASS="USERINPUT" |
| 2810 | ><B |
| 2811 | >tr A-Z \* <filename</B |
| 2812 | ></TT |
| 2813 | > changes |
| 2814 | all the uppercase letters in <TT |
| 2815 | CLASS="FILENAME" |
| 2816 | >filename</TT |
| 2817 | > |
| 2818 | to asterisks (writes to <TT |
| 2819 | CLASS="FILENAME" |
| 2820 | >stdout</TT |
| 2821 | >). |
| 2822 | On some systems this may not work, but <TT |
| 2823 | CLASS="USERINPUT" |
| 2824 | ><B |
| 2825 | >tr A-Z |
| 2826 | '[**]'</B |
| 2827 | ></TT |
| 2828 | > will.</P |
| 2829 | ><P |
| 2830 | ><A |
| 2831 | NAME="TROPTIONS" |
| 2832 | ></A |
| 2833 | ></P |
| 2834 | ><P |
| 2835 | >The <TT |
| 2836 | CLASS="OPTION" |
| 2837 | >-d</TT |
| 2838 | > option deletes a range of |
| 2839 | characters. |
| 2840 | <TABLE |
| 2841 | BORDER="0" |
| 2842 | BGCOLOR="#E0E0E0" |
| 2843 | WIDTH="90%" |
| 2844 | ><TR |
| 2845 | ><TD |
| 2846 | ><PRE |
| 2847 | CLASS="PROGRAMLISTING" |
| 2848 | > 1 echo "abcdef" # abcdef |
| 2849 | 2 echo "abcdef" | tr -d b-d # aef |
| 2850 | 3 |
| 2851 | 4 |
| 2852 | 5 tr -d 0-9 <filename |
| 2853 | 6 # Deletes all digits from the file "filename".</PRE |
| 2854 | ></TD |
| 2855 | ></TR |
| 2856 | ></TABLE |
| 2857 | ></P |
| 2858 | ><P |
| 2859 | >The <TT |
| 2860 | CLASS="OPTION" |
| 2861 | >--squeeze-repeats</TT |
| 2862 | > (or |
| 2863 | <TT |
| 2864 | CLASS="OPTION" |
| 2865 | >-s</TT |
| 2866 | >) option deletes all but the |
| 2867 | first instance of a string of consecutive characters. |
| 2868 | This option is useful for removing excess <A |
| 2869 | HREF="special-chars.html#WHITESPACEREF" |
| 2870 | >whitespace</A |
| 2871 | >. |
| 2872 | |
| 2873 | |
| 2874 | |
| 2875 | <TABLE |
| 2876 | BORDER="0" |
| 2877 | BGCOLOR="#E0E0E0" |
| 2878 | WIDTH="90%" |
| 2879 | ><TR |
| 2880 | ><TD |
| 2881 | ><PRE |
| 2882 | CLASS="SCREEN" |
| 2883 | > <TT |
| 2884 | CLASS="PROMPT" |
| 2885 | >bash$ </TT |
| 2886 | ><TT |
| 2887 | CLASS="USERINPUT" |
| 2888 | ><B |
| 2889 | >echo "XXXXX" | tr --squeeze-repeats 'X'</B |
| 2890 | ></TT |
| 2891 | > |
| 2892 | <TT |
| 2893 | CLASS="COMPUTEROUTPUT" |
| 2894 | >X</TT |
| 2895 | ></PRE |
| 2896 | ></TD |
| 2897 | ></TR |
| 2898 | ></TABLE |
| 2899 | ></P |
| 2900 | ><P |
| 2901 | >The <TT |
| 2902 | CLASS="OPTION" |
| 2903 | >-c</TT |
| 2904 | > <SPAN |
| 2905 | CLASS="QUOTE" |
| 2906 | >"complement"</SPAN |
| 2907 | > |
| 2908 | option <I |
| 2909 | CLASS="FIRSTTERM" |
| 2910 | >inverts</I |
| 2911 | > the character set to |
| 2912 | match. With this option, <B |
| 2913 | CLASS="COMMAND" |
| 2914 | >tr</B |
| 2915 | > acts only |
| 2916 | upon those characters <SPAN |
| 2917 | CLASS="emphasis" |
| 2918 | ><I |
| 2919 | CLASS="EMPHASIS" |
| 2920 | >not</I |
| 2921 | ></SPAN |
| 2922 | > matching |
| 2923 | the specified set.</P |
| 2924 | ><P |
| 2925 | > <TABLE |
| 2926 | BORDER="0" |
| 2927 | BGCOLOR="#E0E0E0" |
| 2928 | WIDTH="90%" |
| 2929 | ><TR |
| 2930 | ><TD |
| 2931 | ><PRE |
| 2932 | CLASS="SCREEN" |
| 2933 | > <TT |
| 2934 | CLASS="PROMPT" |
| 2935 | >bash$ </TT |
| 2936 | ><TT |
| 2937 | CLASS="USERINPUT" |
| 2938 | ><B |
| 2939 | >echo "acfdeb123" | tr -c b-d +</B |
| 2940 | ></TT |
| 2941 | > |
| 2942 | <TT |
| 2943 | CLASS="COMPUTEROUTPUT" |
| 2944 | >+c+d+b++++</TT |
| 2945 | ></PRE |
| 2946 | ></TD |
| 2947 | ></TR |
| 2948 | ></TABLE |
| 2949 | > |
| 2950 | </P |
| 2951 | ><P |
| 2952 | >Note that <B |
| 2953 | CLASS="COMMAND" |
| 2954 | >tr</B |
| 2955 | > recognizes <A |
| 2956 | HREF="regexp.html#POSIXREF" |
| 2957 | >POSIX character classes</A |
| 2958 | >. |
| 2959 | <A |
| 2960 | NAME="AEN11502" |
| 2961 | HREF="#FTN.AEN11502" |
| 2962 | >[1]</A |
| 2963 | > |
| 2964 | </P |
| 2965 | ><P |
| 2966 | > <TABLE |
| 2967 | BORDER="0" |
| 2968 | BGCOLOR="#E0E0E0" |
| 2969 | WIDTH="90%" |
| 2970 | ><TR |
| 2971 | ><TD |
| 2972 | ><PRE |
| 2973 | CLASS="SCREEN" |
| 2974 | > <TT |
| 2975 | CLASS="PROMPT" |
| 2976 | >bash$ </TT |
| 2977 | ><TT |
| 2978 | CLASS="USERINPUT" |
| 2979 | ><B |
| 2980 | >echo "abcd2ef1" | tr '[:alpha:]' -</B |
| 2981 | ></TT |
| 2982 | > |
| 2983 | <TT |
| 2984 | CLASS="COMPUTEROUTPUT" |
| 2985 | >----2--1</TT |
| 2986 | > |
| 2987 | </PRE |
| 2988 | ></TD |
| 2989 | ></TR |
| 2990 | ></TABLE |
| 2991 | > |
| 2992 | </P |
| 2993 | ><DIV |
| 2994 | CLASS="EXAMPLE" |
| 2995 | ><HR><A |
| 2996 | NAME="EX49" |
| 2997 | ></A |
| 2998 | ><P |
| 2999 | ><B |
| 3000 | >Example 16-21. <I |
| 3001 | CLASS="FIRSTTERM" |
| 3002 | >toupper</I |
| 3003 | >: Transforms a file |
| 3004 | to all uppercase.</B |
| 3005 | ></P |
| 3006 | ><TABLE |
| 3007 | BORDER="0" |
| 3008 | BGCOLOR="#E0E0E0" |
| 3009 | WIDTH="90%" |
| 3010 | ><TR |
| 3011 | ><TD |
| 3012 | ><PRE |
| 3013 | CLASS="PROGRAMLISTING" |
| 3014 | > 1 #!/bin/bash |
| 3015 | 2 # Changes a file to all uppercase. |
| 3016 | 3 |
| 3017 | 4 E_BADARGS=85 |
| 3018 | 5 |
| 3019 | 6 if [ -z "$1" ] # Standard check for command-line arg. |
| 3020 | 7 then |
| 3021 | 8 echo "Usage: `basename $0` filename" |
| 3022 | 9 exit $E_BADARGS |
| 3023 | 10 fi |
| 3024 | 11 |
| 3025 | 12 tr a-z A-Z <"$1" |
| 3026 | 13 |
| 3027 | 14 # Same effect as above, but using POSIX character set notation: |
| 3028 | 15 # tr '[:lower:]' '[:upper:]' <"$1" |
| 3029 | 16 # Thanks, S.C. |
| 3030 | 17 |
| 3031 | 18 # Or even . . . |
| 3032 | 19 # cat "$1" | tr a-z A-Z |
| 3033 | 20 # Or dozens of other ways . . . |
| 3034 | 21 |
| 3035 | 22 exit 0 |
| 3036 | 23 |
| 3037 | 24 # Exercise: |
| 3038 | 25 # Rewrite this script to give the option of changing a file |
| 3039 | 26 #+ to *either* upper or lowercase. |
| 3040 | 27 # Hint: Use either the "case" or "select" command.</PRE |
| 3041 | ></TD |
| 3042 | ></TR |
| 3043 | ></TABLE |
| 3044 | ><HR></DIV |
| 3045 | ><DIV |
| 3046 | CLASS="EXAMPLE" |
| 3047 | ><HR><A |
| 3048 | NAME="LOWERCASE" |
| 3049 | ></A |
| 3050 | ><P |
| 3051 | ><B |
| 3052 | >Example 16-22. <I |
| 3053 | CLASS="FIRSTTERM" |
| 3054 | >lowercase</I |
| 3055 | >: Changes all |
| 3056 | filenames in working directory to lowercase.</B |
| 3057 | ></P |
| 3058 | ><TABLE |
| 3059 | BORDER="0" |
| 3060 | BGCOLOR="#E0E0E0" |
| 3061 | WIDTH="90%" |
| 3062 | ><TR |
| 3063 | ><TD |
| 3064 | ><PRE |
| 3065 | CLASS="PROGRAMLISTING" |
| 3066 | > 1 #!/bin/bash |
| 3067 | 2 # |
| 3068 | 3 # Changes every filename in working directory to all lowercase. |
| 3069 | 4 # |
| 3070 | 5 # Inspired by a script of John Dubois, |
| 3071 | 6 #+ which was translated into Bash by Chet Ramey, |
| 3072 | 7 #+ and considerably simplified by the author of the ABS Guide. |
| 3073 | 8 |
| 3074 | 9 |
| 3075 | 10 for filename in * # Traverse all files in directory. |
| 3076 | 11 do |
| 3077 | 12 fname=`basename $filename` |
| 3078 | 13 n=`echo $fname | tr A-Z a-z` # Change name to lowercase. |
| 3079 | 14 if [ "$fname" != "$n" ] # Rename only files not already lowercase. |
| 3080 | 15 then |
| 3081 | 16 mv $fname $n |
| 3082 | 17 fi |
| 3083 | 18 done |
| 3084 | 19 |
| 3085 | 20 exit $? |
| 3086 | 21 |
| 3087 | 22 |
| 3088 | 23 # Code below this line will not execute because of "exit". |
| 3089 | 24 #--------------------------------------------------------# |
| 3090 | 25 # To run it, delete script above line. |
| 3091 | 26 |
| 3092 | 27 # The above script will not work on filenames containing blanks or newlines. |
| 3093 | 28 # Stephane Chazelas therefore suggests the following alternative: |
| 3094 | 29 |
| 3095 | 30 |
| 3096 | 31 for filename in * # Not necessary to use basename, |
| 3097 | 32 # since "*" won't return any file containing "/". |
| 3098 | 33 do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'` |
| 3099 | 34 # POSIX char set notation. |
| 3100 | 35 # Slash added so that trailing newlines are not |
| 3101 | 36 # removed by command substitution. |
| 3102 | 37 # Variable substitution: |
| 3103 | 38 n=${n%/} # Removes trailing slash, added above, from filename. |
| 3104 | 39 [[ $filename == $n ]] || mv "$filename" "$n" |
| 3105 | 40 # Checks if filename already lowercase. |
| 3106 | 41 done |
| 3107 | 42 |
| 3108 | 43 exit $?</PRE |
| 3109 | ></TD |
| 3110 | ></TR |
| 3111 | ></TABLE |
| 3112 | ><HR></DIV |
| 3113 | ><P |
| 3114 | ><A |
| 3115 | NAME="TRD2U" |
| 3116 | ></A |
| 3117 | ></P |
| 3118 | ><DIV |
| 3119 | CLASS="EXAMPLE" |
| 3120 | ><HR><A |
| 3121 | NAME="DU" |
| 3122 | ></A |
| 3123 | ><P |
| 3124 | ><B |
| 3125 | >Example 16-23. <I |
| 3126 | CLASS="FIRSTTERM" |
| 3127 | >du</I |
| 3128 | >: DOS to UNIX text file conversion.</B |
| 3129 | ></P |
| 3130 | ><TABLE |
| 3131 | BORDER="0" |
| 3132 | BGCOLOR="#E0E0E0" |
| 3133 | WIDTH="90%" |
| 3134 | ><TR |
| 3135 | ><TD |
| 3136 | ><PRE |
| 3137 | CLASS="PROGRAMLISTING" |
| 3138 | > 1 #!/bin/bash |
| 3139 | 2 # Du.sh: DOS to UNIX text file converter. |
| 3140 | 3 |
| 3141 | 4 E_WRONGARGS=85 |
| 3142 | 5 |
| 3143 | 6 if [ -z "$1" ] |
| 3144 | 7 then |
| 3145 | 8 echo "Usage: `basename $0` filename-to-convert" |
| 3146 | 9 exit $E_WRONGARGS |
| 3147 | 10 fi |
| 3148 | 11 |
| 3149 | 12 NEWFILENAME=$1.unx |
| 3150 | 13 |
| 3151 | 14 CR='\015' # Carriage return. |
| 3152 | 15 # 015 is octal ASCII code for CR. |
| 3153 | 16 # Lines in a DOS text file end in CR-LF. |
| 3154 | 17 # Lines in a UNIX text file end in LF only. |
| 3155 | 18 |
| 3156 | 19 tr -d $CR < $1 > $NEWFILENAME |
| 3157 | 20 # Delete CR's and write to new file. |
| 3158 | 21 |
| 3159 | 22 echo "Original DOS text file is \"$1\"." |
| 3160 | 23 echo "Converted UNIX text file is \"$NEWFILENAME\"." |
| 3161 | 24 |
| 3162 | 25 exit 0 |
| 3163 | 26 |
| 3164 | 27 # Exercise: |
| 3165 | 28 # -------- |
| 3166 | 29 # Change the above script to convert from UNIX to DOS.</PRE |
| 3167 | ></TD |
| 3168 | ></TR |
| 3169 | ></TABLE |
| 3170 | ><HR></DIV |
| 3171 | ><DIV |
| 3172 | CLASS="EXAMPLE" |
| 3173 | ><HR><A |
| 3174 | NAME="ROT13" |
| 3175 | ></A |
| 3176 | ><P |
| 3177 | ><B |
| 3178 | >Example 16-24. <I |
| 3179 | CLASS="FIRSTTERM" |
| 3180 | >rot13</I |
| 3181 | >: ultra-weak encryption.</B |
| 3182 | ></P |
| 3183 | ><TABLE |
| 3184 | BORDER="0" |
| 3185 | BGCOLOR="#E0E0E0" |
| 3186 | WIDTH="90%" |
| 3187 | ><TR |
| 3188 | ><TD |
| 3189 | ><PRE |
| 3190 | CLASS="PROGRAMLISTING" |
| 3191 | > 1 #!/bin/bash |
| 3192 | 2 # rot13.sh: Classic rot13 algorithm, |
| 3193 | 3 # encryption that might fool a 3-year old |
| 3194 | 4 # for about 10 minutes. |
| 3195 | 5 |
| 3196 | 6 # Usage: ./rot13.sh filename |
| 3197 | 7 # or ./rot13.sh <filename |
| 3198 | 8 # or ./rot13.sh and supply keyboard input (stdin) |
| 3199 | 9 |
| 3200 | 10 cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' # "a" goes to "n", "b" to "o" ... |
| 3201 | 11 # The cat "$@" construct |
| 3202 | 12 #+ permits input either from stdin or from files. |
| 3203 | 13 |
| 3204 | 14 exit 0</PRE |
| 3205 | ></TD |
| 3206 | ></TR |
| 3207 | ></TABLE |
| 3208 | ><HR></DIV |
| 3209 | ><DIV |
| 3210 | CLASS="EXAMPLE" |
| 3211 | ><HR><A |
| 3212 | NAME="CRYPTOQUOTE" |
| 3213 | ></A |
| 3214 | ><P |
| 3215 | ><B |
| 3216 | >Example 16-25. Generating <SPAN |
| 3217 | CLASS="QUOTE" |
| 3218 | >"Crypto-Quote"</SPAN |
| 3219 | > Puzzles</B |
| 3220 | ></P |
| 3221 | ><TABLE |
| 3222 | BORDER="0" |
| 3223 | BGCOLOR="#E0E0E0" |
| 3224 | WIDTH="90%" |
| 3225 | ><TR |
| 3226 | ><TD |
| 3227 | ><PRE |
| 3228 | CLASS="PROGRAMLISTING" |
| 3229 | > 1 #!/bin/bash |
| 3230 | 2 # crypto-quote.sh: Encrypt quotes |
| 3231 | 3 |
| 3232 | 4 # Will encrypt famous quotes in a simple monoalphabetic substitution. |
| 3233 | 5 # The result is similar to the "Crypto Quote" puzzles |
| 3234 | 6 #+ seen in the Op Ed pages of the Sunday paper. |
| 3235 | 7 |
| 3236 | 8 |
| 3237 | 9 key=ETAOINSHRDLUBCFGJMQPVWZYXK |
| 3238 | 10 # The "key" is nothing more than a scrambled alphabet. |
| 3239 | 11 # Changing the "key" changes the encryption. |
| 3240 | 12 |
| 3241 | 13 # The 'cat "$@"' construction gets input either from stdin or from files. |
| 3242 | 14 # If using stdin, terminate input with a Control-D. |
| 3243 | 15 # Otherwise, specify filename as command-line parameter. |
| 3244 | 16 |
| 3245 | 17 cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key" |
| 3246 | 18 # | to uppercase | encrypt |
| 3247 | 19 # Will work on lowercase, uppercase, or mixed-case quotes. |
| 3248 | 20 # Passes non-alphabetic characters through unchanged. |
| 3249 | 21 |
| 3250 | 22 |
| 3251 | 23 # Try this script with something like: |
| 3252 | 24 # "Nothing so needs reforming as other people's habits." |
| 3253 | 25 # --Mark Twain |
| 3254 | 26 # |
| 3255 | 27 # Output is: |
| 3256 | 28 # "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ." |
| 3257 | 29 # --BEML PZERC |
| 3258 | 30 |
| 3259 | 31 # To reverse the encryption: |
| 3260 | 32 # cat "$@" | tr "$key" "A-Z" |
| 3261 | 33 |
| 3262 | 34 |
| 3263 | 35 # This simple-minded cipher can be broken by an average 12-year old |
| 3264 | 36 #+ using only pencil and paper. |
| 3265 | 37 |
| 3266 | 38 exit 0 |
| 3267 | 39 |
| 3268 | 40 # Exercise: |
| 3269 | 41 # -------- |
| 3270 | 42 # Modify the script so that it will either encrypt or decrypt, |
| 3271 | 43 #+ depending on command-line argument(s).</PRE |
| 3272 | ></TD |
| 3273 | ></TR |
| 3274 | ></TABLE |
| 3275 | ><HR></DIV |
| 3276 | ><P |
| 3277 | ><A |
| 3278 | NAME="JABH" |
| 3279 | ></A |
| 3280 | >Of course, <I |
| 3281 | CLASS="FIRSTTERM" |
| 3282 | >tr</I |
| 3283 | > |
| 3284 | lends itself to <I |
| 3285 | CLASS="FIRSTTERM" |
| 3286 | >code |
| 3287 | obfuscation</I |
| 3288 | >.</P |
| 3289 | ><P |
| 3290 | ><TABLE |
| 3291 | BORDER="0" |
| 3292 | BGCOLOR="#E0E0E0" |
| 3293 | WIDTH="90%" |
| 3294 | ><TR |
| 3295 | ><TD |
| 3296 | ><PRE |
| 3297 | CLASS="PROGRAMLISTING" |
| 3298 | > 1 #!/bin/bash |
| 3299 | 2 # jabh.sh |
| 3300 | 3 |
| 3301 | 4 x="wftedskaebjgdBstbdbsmnjgz" |
| 3302 | 5 echo $x | tr "a-z" 'oh, turtleneck Phrase Jar!' |
| 3303 | 6 |
| 3304 | 7 # Based on the Wikipedia "Just another Perl hacker" article.</PRE |
| 3305 | ></TD |
| 3306 | ></TR |
| 3307 | ></TABLE |
| 3308 | ></P |
| 3309 | ><P |
| 3310 | ><A |
| 3311 | NAME="TRVARIANTS" |
| 3312 | ></A |
| 3313 | ></P |
| 3314 | ><TABLE |
| 3315 | CLASS="SIDEBAR" |
| 3316 | BORDER="1" |
| 3317 | CELLPADDING="5" |
| 3318 | ><TR |
| 3319 | ><TD |
| 3320 | ><DIV |
| 3321 | CLASS="SIDEBAR" |
| 3322 | ><A |
| 3323 | NAME="AEN11540" |
| 3324 | ></A |
| 3325 | ><P |
| 3326 | ><B |
| 3327 | ><I |
| 3328 | CLASS="FIRSTTERM" |
| 3329 | >tr</I |
| 3330 | > variants</B |
| 3331 | ></P |
| 3332 | ><P |
| 3333 | > The <B |
| 3334 | CLASS="COMMAND" |
| 3335 | >tr</B |
| 3336 | > utility has two historic |
| 3337 | variants. The BSD version does not use brackets |
| 3338 | (<TT |
| 3339 | CLASS="USERINPUT" |
| 3340 | ><B |
| 3341 | >tr a-z A-Z</B |
| 3342 | ></TT |
| 3343 | >), but the SysV one does |
| 3344 | (<TT |
| 3345 | CLASS="USERINPUT" |
| 3346 | ><B |
| 3347 | >tr '[a-z]' '[A-Z]'</B |
| 3348 | ></TT |
| 3349 | >). The GNU version |
| 3350 | of <B |
| 3351 | CLASS="COMMAND" |
| 3352 | >tr</B |
| 3353 | > resembles the BSD one. |
| 3354 | </P |
| 3355 | ></DIV |
| 3356 | ></TD |
| 3357 | ></TR |
| 3358 | ></TABLE |
| 3359 | ></DD |
| 3360 | ><DT |
| 3361 | ><A |
| 3362 | NAME="FOLDREF" |
| 3363 | ></A |
| 3364 | ><B |
| 3365 | CLASS="COMMAND" |
| 3366 | >fold</B |
| 3367 | ></DT |
| 3368 | ><DD |
| 3369 | ><P |
| 3370 | >A filter that wraps lines of input to a specified width. |
| 3371 | This is especially useful with the <TT |
| 3372 | CLASS="OPTION" |
| 3373 | >-s</TT |
| 3374 | > |
| 3375 | option, which breaks lines at word spaces (see <A |
| 3376 | HREF="textproc.html#EX50" |
| 3377 | >Example 16-26</A |
| 3378 | > and <A |
| 3379 | HREF="contributed-scripts.html#MAILFORMAT" |
| 3380 | >Example A-1</A |
| 3381 | >).</P |
| 3382 | ></DD |
| 3383 | ><DT |
| 3384 | ><A |
| 3385 | NAME="FMTREF" |
| 3386 | ></A |
| 3387 | ><B |
| 3388 | CLASS="COMMAND" |
| 3389 | >fmt</B |
| 3390 | ></DT |
| 3391 | ><DD |
| 3392 | ><P |
| 3393 | >Simple-minded file formatter, used as a filter in a |
| 3394 | pipe to <SPAN |
| 3395 | CLASS="QUOTE" |
| 3396 | >"wrap"</SPAN |
| 3397 | > long lines of text |
| 3398 | output.</P |
| 3399 | ><DIV |
| 3400 | CLASS="EXAMPLE" |
| 3401 | ><HR><A |
| 3402 | NAME="EX50" |
| 3403 | ></A |
| 3404 | ><P |
| 3405 | ><B |
| 3406 | >Example 16-26. Formatted file listing.</B |
| 3407 | ></P |
| 3408 | ><TABLE |
| 3409 | BORDER="0" |
| 3410 | BGCOLOR="#E0E0E0" |
| 3411 | WIDTH="90%" |
| 3412 | ><TR |
| 3413 | ><TD |
| 3414 | ><PRE |
| 3415 | CLASS="PROGRAMLISTING" |
| 3416 | > 1 #!/bin/bash |
| 3417 | 2 |
| 3418 | 3 WIDTH=40 # 40 columns wide. |
| 3419 | 4 |
| 3420 | 5 b=`ls /usr/local/bin` # Get a file listing... |
| 3421 | 6 |
| 3422 | 7 echo $b | fmt -w $WIDTH |
| 3423 | 8 |
| 3424 | 9 # Could also have been done by |
| 3425 | 10 # echo $b | fold - -s -w $WIDTH |
| 3426 | 11 |
| 3427 | 12 exit 0</PRE |
| 3428 | ></TD |
| 3429 | ></TR |
| 3430 | ></TABLE |
| 3431 | ><HR></DIV |
| 3432 | ><P |
| 3433 | >See also <A |
| 3434 | HREF="moreadv.html#EX41" |
| 3435 | >Example 16-5</A |
| 3436 | >.</P |
| 3437 | ><DIV |
| 3438 | CLASS="TIP" |
| 3439 | ><TABLE |
| 3440 | CLASS="TIP" |
| 3441 | WIDTH="90%" |
| 3442 | BORDER="0" |
| 3443 | ><TR |
| 3444 | ><TD |
| 3445 | WIDTH="25" |
| 3446 | ALIGN="CENTER" |
| 3447 | VALIGN="TOP" |
| 3448 | ><IMG |
| 3449 | SRC="common/tip.png" |
| 3450 | HSPACE="5" |
| 3451 | ALT="Tip"></TD |
| 3452 | ><TD |
| 3453 | ALIGN="LEFT" |
| 3454 | VALIGN="TOP" |
| 3455 | ><P |
| 3456 | >A powerful alternative to <B |
| 3457 | CLASS="COMMAND" |
| 3458 | >fmt</B |
| 3459 | > is |
| 3460 | Kamil Toman's <B |
| 3461 | CLASS="COMMAND" |
| 3462 | >par</B |
| 3463 | > |
| 3464 | utility, available from <A |
| 3465 | HREF="http://www.cs.berkeley.edu/~amc/Par/" |
| 3466 | TARGET="_top" |
| 3467 | >http://www.cs.berkeley.edu/~amc/Par/</A |
| 3468 | >. |
| 3469 | </P |
| 3470 | ></TD |
| 3471 | ></TR |
| 3472 | ></TABLE |
| 3473 | ></DIV |
| 3474 | ></DD |
| 3475 | ><DT |
| 3476 | ><A |
| 3477 | NAME="COLREF" |
| 3478 | ></A |
| 3479 | ><B |
| 3480 | CLASS="COMMAND" |
| 3481 | >col</B |
| 3482 | ></DT |
| 3483 | ><DD |
| 3484 | ><P |
| 3485 | >This deceptively named filter removes reverse line feeds |
| 3486 | from an input stream. It also attempts to replace |
| 3487 | whitespace with equivalent tabs. The chief use of |
| 3488 | <B |
| 3489 | CLASS="COMMAND" |
| 3490 | >col</B |
| 3491 | > is in filtering the output |
| 3492 | from certain text processing utilities, such as |
| 3493 | <B |
| 3494 | CLASS="COMMAND" |
| 3495 | >groff</B |
| 3496 | > and <B |
| 3497 | CLASS="COMMAND" |
| 3498 | >tbl</B |
| 3499 | >.</P |
| 3500 | ></DD |
| 3501 | ><DT |
| 3502 | ><A |
| 3503 | NAME="COLUMNREF" |
| 3504 | ></A |
| 3505 | ><B |
| 3506 | CLASS="COMMAND" |
| 3507 | >column</B |
| 3508 | ></DT |
| 3509 | ><DD |
| 3510 | ><P |
| 3511 | >Column formatter. This filter transforms list-type |
| 3512 | text output into a <SPAN |
| 3513 | CLASS="QUOTE" |
| 3514 | >"pretty-printed"</SPAN |
| 3515 | > table |
| 3516 | by inserting tabs at appropriate places.</P |
| 3517 | ><DIV |
| 3518 | CLASS="EXAMPLE" |
| 3519 | ><HR><A |
| 3520 | NAME="COL" |
| 3521 | ></A |
| 3522 | ><P |
| 3523 | ><B |
| 3524 | >Example 16-27. Using <I |
| 3525 | CLASS="FIRSTTERM" |
| 3526 | >column</I |
| 3527 | > to format a directory |
| 3528 | listing</B |
| 3529 | ></P |
| 3530 | ><TABLE |
| 3531 | BORDER="0" |
| 3532 | BGCOLOR="#E0E0E0" |
| 3533 | WIDTH="90%" |
| 3534 | ><TR |
| 3535 | ><TD |
| 3536 | ><PRE |
| 3537 | CLASS="PROGRAMLISTING" |
| 3538 | > 1 #!/bin/bash |
| 3539 | 2 # colms.sh |
| 3540 | 3 # A minor modification of the example file in the "column" man page. |
| 3541 | 4 |
| 3542 | 5 |
| 3543 | 6 (printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \ |
| 3544 | 7 ; ls -l | sed 1d) | column -t |
| 3545 | 8 # ^^^^^^ ^^ |
| 3546 | 9 |
| 3547 | 10 # The "sed 1d" in the pipe deletes the first line of output, |
| 3548 | 11 #+ which would be "total N", |
| 3549 | 12 #+ where "N" is the total number of files found by "ls -l". |
| 3550 | 13 |
| 3551 | 14 # The -t option to "column" pretty-prints a table. |
| 3552 | 15 |
| 3553 | 16 exit 0</PRE |
| 3554 | ></TD |
| 3555 | ></TR |
| 3556 | ></TABLE |
| 3557 | ><HR></DIV |
| 3558 | ></DD |
| 3559 | ><DT |
| 3560 | ><A |
| 3561 | NAME="COLRMREF" |
| 3562 | ></A |
| 3563 | ><B |
| 3564 | CLASS="COMMAND" |
| 3565 | >colrm</B |
| 3566 | ></DT |
| 3567 | ><DD |
| 3568 | ><P |
| 3569 | >Column removal filter. This removes columns (characters) |
| 3570 | from a file and writes the file, lacking the range of |
| 3571 | specified columns, back to <TT |
| 3572 | CLASS="FILENAME" |
| 3573 | >stdout</TT |
| 3574 | >. |
| 3575 | <TT |
| 3576 | CLASS="USERINPUT" |
| 3577 | ><B |
| 3578 | >colrm 2 4 <filename</B |
| 3579 | ></TT |
| 3580 | > removes the |
| 3581 | second through fourth characters from each line of the |
| 3582 | text file <TT |
| 3583 | CLASS="FILENAME" |
| 3584 | >filename</TT |
| 3585 | >.</P |
| 3586 | ><DIV |
| 3587 | CLASS="CAUTION" |
| 3588 | ><TABLE |
| 3589 | CLASS="CAUTION" |
| 3590 | WIDTH="90%" |
| 3591 | BORDER="0" |
| 3592 | ><TR |
| 3593 | ><TD |
| 3594 | WIDTH="25" |
| 3595 | ALIGN="CENTER" |
| 3596 | VALIGN="TOP" |
| 3597 | ><IMG |
| 3598 | SRC="common/caution.png" |
| 3599 | HSPACE="5" |
| 3600 | ALT="Caution"></TD |
| 3601 | ><TD |
| 3602 | ALIGN="LEFT" |
| 3603 | VALIGN="TOP" |
| 3604 | ><P |
| 3605 | >If the file contains tabs or nonprintable |
| 3606 | characters, this may cause unpredictable |
| 3607 | behavior. In such cases, consider using |
| 3608 | <A |
| 3609 | HREF="textproc.html#EXPANDREF" |
| 3610 | >expand</A |
| 3611 | > and |
| 3612 | <B |
| 3613 | CLASS="COMMAND" |
| 3614 | >unexpand</B |
| 3615 | > in a pipe preceding |
| 3616 | <B |
| 3617 | CLASS="COMMAND" |
| 3618 | >colrm</B |
| 3619 | >.</P |
| 3620 | ></TD |
| 3621 | ></TR |
| 3622 | ></TABLE |
| 3623 | ></DIV |
| 3624 | ></DD |
| 3625 | ><DT |
| 3626 | ><A |
| 3627 | NAME="NLREF" |
| 3628 | ></A |
| 3629 | ><B |
| 3630 | CLASS="COMMAND" |
| 3631 | >nl</B |
| 3632 | ></DT |
| 3633 | ><DD |
| 3634 | ><P |
| 3635 | >Line numbering filter: <TT |
| 3636 | CLASS="USERINPUT" |
| 3637 | ><B |
| 3638 | >nl filename</B |
| 3639 | ></TT |
| 3640 | > |
| 3641 | lists <TT |
| 3642 | CLASS="FILENAME" |
| 3643 | >filename</TT |
| 3644 | > to |
| 3645 | <TT |
| 3646 | CLASS="FILENAME" |
| 3647 | >stdout</TT |
| 3648 | >, but inserts consecutive |
| 3649 | numbers at the beginning of each non-blank line. If |
| 3650 | <TT |
| 3651 | CLASS="FILENAME" |
| 3652 | >filename</TT |
| 3653 | > omitted, operates on |
| 3654 | <TT |
| 3655 | CLASS="FILENAME" |
| 3656 | >stdin.</TT |
| 3657 | ></P |
| 3658 | ><P |
| 3659 | >The output of <B |
| 3660 | CLASS="COMMAND" |
| 3661 | >nl</B |
| 3662 | > is very similar to |
| 3663 | <TT |
| 3664 | CLASS="USERINPUT" |
| 3665 | ><B |
| 3666 | >cat -b</B |
| 3667 | ></TT |
| 3668 | >, since, by default |
| 3669 | <B |
| 3670 | CLASS="COMMAND" |
| 3671 | >nl</B |
| 3672 | > does not list blank lines.</P |
| 3673 | ><DIV |
| 3674 | CLASS="EXAMPLE" |
| 3675 | ><HR><A |
| 3676 | NAME="LNUM" |
| 3677 | ></A |
| 3678 | ><P |
| 3679 | ><B |
| 3680 | >Example 16-28. <I |
| 3681 | CLASS="FIRSTTERM" |
| 3682 | >nl</I |
| 3683 | >: A self-numbering script.</B |
| 3684 | ></P |
| 3685 | ><TABLE |
| 3686 | BORDER="0" |
| 3687 | BGCOLOR="#E0E0E0" |
| 3688 | WIDTH="90%" |
| 3689 | ><TR |
| 3690 | ><TD |
| 3691 | ><PRE |
| 3692 | CLASS="PROGRAMLISTING" |
| 3693 | > 1 #!/bin/bash |
| 3694 | 2 # line-number.sh |
| 3695 | 3 |
| 3696 | 4 # This script echoes itself twice to stdout with its lines numbered. |
| 3697 | 5 |
| 3698 | 6 echo " line number = $LINENO" # 'nl' sees this as line 4 |
| 3699 | 7 # (nl does not number blank lines). |
| 3700 | 8 # 'cat -n' sees it correctly as line #6. |
| 3701 | 9 |
| 3702 | 10 nl `basename $0` |
| 3703 | 11 |
| 3704 | 12 echo; echo # Now, let's try it with 'cat -n' |
| 3705 | 13 |
| 3706 | 14 cat -n `basename $0` |
| 3707 | 15 # The difference is that 'cat -n' numbers the blank lines. |
| 3708 | 16 # Note that 'nl -ba' will also do so. |
| 3709 | 17 |
| 3710 | 18 exit 0 |
| 3711 | 19 # -----------------------------------------------------------------</PRE |
| 3712 | ></TD |
| 3713 | ></TR |
| 3714 | ></TABLE |
| 3715 | ><HR></DIV |
| 3716 | ></DD |
| 3717 | ><DT |
| 3718 | ><A |
| 3719 | NAME="PRREF" |
| 3720 | ></A |
| 3721 | ><B |
| 3722 | CLASS="COMMAND" |
| 3723 | >pr</B |
| 3724 | ></DT |
| 3725 | ><DD |
| 3726 | ><P |
| 3727 | >Print formatting filter. This will paginate files |
| 3728 | (or <TT |
| 3729 | CLASS="FILENAME" |
| 3730 | >stdout</TT |
| 3731 | >) into sections suitable for |
| 3732 | hard copy printing or viewing on screen. Various options |
| 3733 | permit row and column manipulation, joining lines, setting |
| 3734 | margins, numbering lines, adding page headers, and merging |
| 3735 | files, among other things. The <B |
| 3736 | CLASS="COMMAND" |
| 3737 | >pr</B |
| 3738 | > |
| 3739 | command combines much of the functionality of |
| 3740 | <B |
| 3741 | CLASS="COMMAND" |
| 3742 | >nl</B |
| 3743 | >, <B |
| 3744 | CLASS="COMMAND" |
| 3745 | >paste</B |
| 3746 | >, |
| 3747 | <B |
| 3748 | CLASS="COMMAND" |
| 3749 | >fold</B |
| 3750 | >, <B |
| 3751 | CLASS="COMMAND" |
| 3752 | >column</B |
| 3753 | >, and |
| 3754 | <B |
| 3755 | CLASS="COMMAND" |
| 3756 | >expand</B |
| 3757 | >.</P |
| 3758 | ><P |
| 3759 | ><TT |
| 3760 | CLASS="USERINPUT" |
| 3761 | ><B |
| 3762 | >pr -o 5 --width=65 fileZZZ | more</B |
| 3763 | ></TT |
| 3764 | > |
| 3765 | gives a nice paginated listing to screen of |
| 3766 | <TT |
| 3767 | CLASS="FILENAME" |
| 3768 | >fileZZZ</TT |
| 3769 | > with margins set at 5 and |
| 3770 | 65.</P |
| 3771 | ><P |
| 3772 | >A particularly useful option is <TT |
| 3773 | CLASS="OPTION" |
| 3774 | >-d</TT |
| 3775 | >, |
| 3776 | forcing double-spacing (same effect as <B |
| 3777 | CLASS="COMMAND" |
| 3778 | >sed |
| 3779 | -G</B |
| 3780 | >).</P |
| 3781 | ></DD |
| 3782 | ><DT |
| 3783 | ><A |
| 3784 | NAME="GETTEXTREF" |
| 3785 | ></A |
| 3786 | ><B |
| 3787 | CLASS="COMMAND" |
| 3788 | >gettext</B |
| 3789 | ></DT |
| 3790 | ><DD |
| 3791 | ><P |
| 3792 | >The GNU <B |
| 3793 | CLASS="COMMAND" |
| 3794 | >gettext</B |
| 3795 | > package is a set of |
| 3796 | utilities for <A |
| 3797 | HREF="localization.html" |
| 3798 | >localizing</A |
| 3799 | > |
| 3800 | and translating the text output of programs into foreign |
| 3801 | languages. While originally intended for C programs, it |
| 3802 | now supports quite a number of programming and scripting |
| 3803 | languages.</P |
| 3804 | ><P |
| 3805 | >The <B |
| 3806 | CLASS="COMMAND" |
| 3807 | >gettext</B |
| 3808 | > |
| 3809 | <SPAN |
| 3810 | CLASS="emphasis" |
| 3811 | ><I |
| 3812 | CLASS="EMPHASIS" |
| 3813 | >program</I |
| 3814 | ></SPAN |
| 3815 | > works on shell scripts. See |
| 3816 | the <TT |
| 3817 | CLASS="REPLACEABLE" |
| 3818 | ><I |
| 3819 | >info page</I |
| 3820 | ></TT |
| 3821 | >.</P |
| 3822 | ></DD |
| 3823 | ><DT |
| 3824 | ><A |
| 3825 | NAME="MSGFMTREF" |
| 3826 | ></A |
| 3827 | ><B |
| 3828 | CLASS="COMMAND" |
| 3829 | >msgfmt</B |
| 3830 | ></DT |
| 3831 | ><DD |
| 3832 | ><P |
| 3833 | >A program for generating binary |
| 3834 | message catalogs. It is used for <A |
| 3835 | HREF="localization.html" |
| 3836 | >localization</A |
| 3837 | >.</P |
| 3838 | ></DD |
| 3839 | ><DT |
| 3840 | ><A |
| 3841 | NAME="ICONVREF" |
| 3842 | ></A |
| 3843 | ><B |
| 3844 | CLASS="COMMAND" |
| 3845 | >iconv</B |
| 3846 | ></DT |
| 3847 | ><DD |
| 3848 | ><P |
| 3849 | >A utility for converting file(s) to a different encoding |
| 3850 | (character set). Its chief use is for <A |
| 3851 | HREF="localization.html" |
| 3852 | >localization</A |
| 3853 | >.</P |
| 3854 | ><P |
| 3855 | > <TABLE |
| 3856 | BORDER="0" |
| 3857 | BGCOLOR="#E0E0E0" |
| 3858 | WIDTH="90%" |
| 3859 | ><TR |
| 3860 | ><TD |
| 3861 | ><PRE |
| 3862 | CLASS="PROGRAMLISTING" |
| 3863 | > 1 # Convert a string from UTF-8 to UTF-16 and print to the BookList |
| 3864 | 2 function write_utf8_string { |
| 3865 | 3 STRING=$1 |
| 3866 | 4 BOOKLIST=$2 |
| 3867 | 5 echo -n "$STRING" | iconv -f UTF8 -t UTF16 | \ |
| 3868 | 6 cut -b 3- | tr -d \\n >> "$BOOKLIST" |
| 3869 | 7 } |
| 3870 | 8 |
| 3871 | 9 # From Peter Knowles' "booklistgen.sh" script |
| 3872 | 10 #+ for converting files to Sony Librie/PRS-50X format. |
| 3873 | 11 # (http://booklistgensh.peterknowles.com)</PRE |
| 3874 | ></TD |
| 3875 | ></TR |
| 3876 | ></TABLE |
| 3877 | > |
| 3878 | </P |
| 3879 | ></DD |
| 3880 | ><DT |
| 3881 | ><A |
| 3882 | NAME="RECODEREF" |
| 3883 | ></A |
| 3884 | ><B |
| 3885 | CLASS="COMMAND" |
| 3886 | >recode</B |
| 3887 | ></DT |
| 3888 | ><DD |
| 3889 | ><P |
| 3890 | >Consider this a fancier version of |
| 3891 | <B |
| 3892 | CLASS="COMMAND" |
| 3893 | >iconv</B |
| 3894 | >, above. This very versatile utility |
| 3895 | for converting a file to a different encoding scheme. |
| 3896 | Note that <I |
| 3897 | CLASS="FIRSTTERM" |
| 3898 | >recode</I |
| 3899 | > is not part of the |
| 3900 | standard Linux installation.</P |
| 3901 | ></DD |
| 3902 | ><DT |
| 3903 | ><A |
| 3904 | NAME="TEXREF" |
| 3905 | ></A |
| 3906 | ><B |
| 3907 | CLASS="COMMAND" |
| 3908 | >TeX</B |
| 3909 | >, <A |
| 3910 | NAME="GSREF" |
| 3911 | ></A |
| 3912 | ><B |
| 3913 | CLASS="COMMAND" |
| 3914 | >gs</B |
| 3915 | ></DT |
| 3916 | ><DD |
| 3917 | ><P |
| 3918 | ><B |
| 3919 | CLASS="COMMAND" |
| 3920 | >TeX</B |
| 3921 | > and <B |
| 3922 | CLASS="COMMAND" |
| 3923 | >Postscript</B |
| 3924 | > |
| 3925 | are text markup languages used for preparing copy for |
| 3926 | printing or formatted video display.</P |
| 3927 | ><P |
| 3928 | ><B |
| 3929 | CLASS="COMMAND" |
| 3930 | >TeX</B |
| 3931 | > is Donald Knuth's elaborate |
| 3932 | typsetting system. It is often convenient to write a |
| 3933 | shell script encapsulating all the options and arguments |
| 3934 | passed to one of these markup languages.</P |
| 3935 | ><P |
| 3936 | ><I |
| 3937 | CLASS="FIRSTTERM" |
| 3938 | >Ghostscript</I |
| 3939 | > |
| 3940 | (<B |
| 3941 | CLASS="COMMAND" |
| 3942 | >gs</B |
| 3943 | >) is a GPL-ed Postscript |
| 3944 | interpreter.</P |
| 3945 | ></DD |
| 3946 | ><DT |
| 3947 | ><A |
| 3948 | NAME="TEXEXECREF" |
| 3949 | ></A |
| 3950 | ><B |
| 3951 | CLASS="COMMAND" |
| 3952 | >texexec</B |
| 3953 | ></DT |
| 3954 | ><DD |
| 3955 | ><P |
| 3956 | >Utility for processing <I |
| 3957 | CLASS="FIRSTTERM" |
| 3958 | >TeX</I |
| 3959 | > and |
| 3960 | <I |
| 3961 | CLASS="FIRSTTERM" |
| 3962 | >pdf</I |
| 3963 | > files. Found in |
| 3964 | <TT |
| 3965 | CLASS="FILENAME" |
| 3966 | >/usr/bin</TT |
| 3967 | > |
| 3968 | on many Linux distros, it is actually a <A |
| 3969 | HREF="wrapper.html#SHWRAPPER" |
| 3970 | >shell wrapper</A |
| 3971 | > that |
| 3972 | calls <A |
| 3973 | HREF="wrapper.html#PERLREF" |
| 3974 | >Perl</A |
| 3975 | > to invoke |
| 3976 | <I |
| 3977 | CLASS="FIRSTTERM" |
| 3978 | >Tex</I |
| 3979 | >.</P |
| 3980 | ><P |
| 3981 | > <TABLE |
| 3982 | BORDER="0" |
| 3983 | BGCOLOR="#E0E0E0" |
| 3984 | WIDTH="90%" |
| 3985 | ><TR |
| 3986 | ><TD |
| 3987 | ><PRE |
| 3988 | CLASS="PROGRAMLISTING" |
| 3989 | > 1 texexec --pdfarrange --result=Concatenated.pdf *pdf |
| 3990 | 2 |
| 3991 | 3 # Concatenates all the pdf files in the current working directory |
| 3992 | 4 #+ into the merged file, Concatenated.pdf . . . |
| 3993 | 5 # (The --pdfarrange option repaginates a pdf file. See also --pdfcombine.) |
| 3994 | 6 # The above command-line could be parameterized and put into a shell script.</PRE |
| 3995 | ></TD |
| 3996 | ></TR |
| 3997 | ></TABLE |
| 3998 | > |
| 3999 | </P |
| 4000 | ></DD |
| 4001 | ><DT |
| 4002 | ><A |
| 4003 | NAME="ENSCRIPTREF" |
| 4004 | ></A |
| 4005 | ><B |
| 4006 | CLASS="COMMAND" |
| 4007 | >enscript</B |
| 4008 | ></DT |
| 4009 | ><DD |
| 4010 | ><P |
| 4011 | >Utility for converting plain text file to PostScript</P |
| 4012 | ><P |
| 4013 | >For example, <B |
| 4014 | CLASS="COMMAND" |
| 4015 | >enscript filename.txt -p filename.ps</B |
| 4016 | > |
| 4017 | produces the PostScript output file |
| 4018 | <TT |
| 4019 | CLASS="FILENAME" |
| 4020 | >filename.ps</TT |
| 4021 | >.</P |
| 4022 | ></DD |
| 4023 | ><DT |
| 4024 | ><A |
| 4025 | NAME="GROFFREF" |
| 4026 | ></A |
| 4027 | ><B |
| 4028 | CLASS="COMMAND" |
| 4029 | >groff</B |
| 4030 | >, <A |
| 4031 | NAME="TBLREF" |
| 4032 | ></A |
| 4033 | ><B |
| 4034 | CLASS="COMMAND" |
| 4035 | >tbl</B |
| 4036 | >, <A |
| 4037 | NAME="EQNREF" |
| 4038 | ></A |
| 4039 | ><B |
| 4040 | CLASS="COMMAND" |
| 4041 | >eqn</B |
| 4042 | ></DT |
| 4043 | ><DD |
| 4044 | ><P |
| 4045 | >Yet another text markup and display formatting language |
| 4046 | is <B |
| 4047 | CLASS="COMMAND" |
| 4048 | >groff</B |
| 4049 | >. This is the enhanced GNU version |
| 4050 | of the venerable UNIX <B |
| 4051 | CLASS="COMMAND" |
| 4052 | >roff/troff</B |
| 4053 | > display |
| 4054 | and typesetting package. <A |
| 4055 | HREF="external.html#MANREF" |
| 4056 | >Manpages</A |
| 4057 | > |
| 4058 | use <B |
| 4059 | CLASS="COMMAND" |
| 4060 | >groff</B |
| 4061 | >.</P |
| 4062 | ><P |
| 4063 | >The <B |
| 4064 | CLASS="COMMAND" |
| 4065 | >tbl</B |
| 4066 | > table processing utility |
| 4067 | is considered part of <B |
| 4068 | CLASS="COMMAND" |
| 4069 | >groff</B |
| 4070 | >, as its |
| 4071 | function is to convert table markup into |
| 4072 | <B |
| 4073 | CLASS="COMMAND" |
| 4074 | >groff</B |
| 4075 | > commands.</P |
| 4076 | ><P |
| 4077 | >The <B |
| 4078 | CLASS="COMMAND" |
| 4079 | >eqn</B |
| 4080 | > equation processing utility |
| 4081 | is likewise part of <B |
| 4082 | CLASS="COMMAND" |
| 4083 | >groff</B |
| 4084 | >, and |
| 4085 | its function is to convert equation markup into |
| 4086 | <B |
| 4087 | CLASS="COMMAND" |
| 4088 | >groff</B |
| 4089 | > commands.</P |
| 4090 | ><DIV |
| 4091 | CLASS="EXAMPLE" |
| 4092 | ><HR><A |
| 4093 | NAME="MANVIEW" |
| 4094 | ></A |
| 4095 | ><P |
| 4096 | ><B |
| 4097 | >Example 16-29. <I |
| 4098 | CLASS="FIRSTTERM" |
| 4099 | >manview</I |
| 4100 | >: Viewing formatted manpages</B |
| 4101 | ></P |
| 4102 | ><TABLE |
| 4103 | BORDER="0" |
| 4104 | BGCOLOR="#E0E0E0" |
| 4105 | WIDTH="90%" |
| 4106 | ><TR |
| 4107 | ><TD |
| 4108 | ><PRE |
| 4109 | CLASS="PROGRAMLISTING" |
| 4110 | > 1 #!/bin/bash |
| 4111 | 2 # manview.sh: Formats the source of a man page for viewing. |
| 4112 | 3 |
| 4113 | 4 # This script is useful when writing man page source. |
| 4114 | 5 # It lets you look at the intermediate results on the fly |
| 4115 | 6 #+ while working on it. |
| 4116 | 7 |
| 4117 | 8 E_WRONGARGS=85 |
| 4118 | 9 |
| 4119 | 10 if [ -z "$1" ] |
| 4120 | 11 then |
| 4121 | 12 echo "Usage: `basename $0` filename" |
| 4122 | 13 exit $E_WRONGARGS |
| 4123 | 14 fi |
| 4124 | 15 |
| 4125 | 16 # --------------------------- |
| 4126 | 17 groff -Tascii -man $1 | less |
| 4127 | 18 # From the man page for groff. |
| 4128 | 19 # --------------------------- |
| 4129 | 20 |
| 4130 | 21 # If the man page includes tables and/or equations, |
| 4131 | 22 #+ then the above code will barf. |
| 4132 | 23 # The following line can handle such cases. |
| 4133 | 24 # |
| 4134 | 25 # gtbl < "$1" | geqn -Tlatin1 | groff -Tlatin1 -mtty-char -man |
| 4135 | 26 # |
| 4136 | 27 # Thanks, S.C. |
| 4137 | 28 |
| 4138 | 29 exit $? # See also the "maned.sh" script.</PRE |
| 4139 | ></TD |
| 4140 | ></TR |
| 4141 | ></TABLE |
| 4142 | ><HR></DIV |
| 4143 | ><P |
| 4144 | >See also <A |
| 4145 | HREF="contributed-scripts.html#MANED" |
| 4146 | >Example A-39</A |
| 4147 | >.</P |
| 4148 | ></DD |
| 4149 | ><DT |
| 4150 | ><A |
| 4151 | NAME="LEXREF" |
| 4152 | ></A |
| 4153 | ><B |
| 4154 | CLASS="COMMAND" |
| 4155 | >lex</B |
| 4156 | >, <A |
| 4157 | NAME="YACCREF" |
| 4158 | ></A |
| 4159 | ><B |
| 4160 | CLASS="COMMAND" |
| 4161 | >yacc</B |
| 4162 | ></DT |
| 4163 | ><DD |
| 4164 | ><P |
| 4165 | ><A |
| 4166 | NAME="FLEXREF" |
| 4167 | ></A |
| 4168 | ></P |
| 4169 | ><P |
| 4170 | >The <B |
| 4171 | CLASS="COMMAND" |
| 4172 | >lex</B |
| 4173 | > lexical analyzer produces |
| 4174 | programs for pattern matching. This has been replaced |
| 4175 | by the nonproprietary <B |
| 4176 | CLASS="COMMAND" |
| 4177 | >flex</B |
| 4178 | > on Linux |
| 4179 | systems.</P |
| 4180 | ><P |
| 4181 | ><A |
| 4182 | NAME="BISONREF" |
| 4183 | ></A |
| 4184 | ></P |
| 4185 | ><P |
| 4186 | >The <B |
| 4187 | CLASS="COMMAND" |
| 4188 | >yacc</B |
| 4189 | > utility creates a |
| 4190 | parser based on a set of specifications. This has been |
| 4191 | replaced by the nonproprietary <B |
| 4192 | CLASS="COMMAND" |
| 4193 | >bison</B |
| 4194 | > |
| 4195 | on Linux systems.</P |
| 4196 | ></DD |
| 4197 | ></DL |
| 4198 | ></DIV |
| 4199 | ></DIV |
| 4200 | ><H3 |
| 4201 | CLASS="FOOTNOTES" |
| 4202 | >Notes</H3 |
| 4203 | ><TABLE |
| 4204 | BORDER="0" |
| 4205 | CLASS="FOOTNOTES" |
| 4206 | WIDTH="100%" |
| 4207 | ><TR |
| 4208 | ><TD |
| 4209 | ALIGN="LEFT" |
| 4210 | VALIGN="TOP" |
| 4211 | WIDTH="5%" |
| 4212 | ><A |
| 4213 | NAME="FTN.AEN11502" |
| 4214 | HREF="textproc.html#AEN11502" |
| 4215 | >[1]</A |
| 4216 | ></TD |
| 4217 | ><TD |
| 4218 | ALIGN="LEFT" |
| 4219 | VALIGN="TOP" |
| 4220 | WIDTH="95%" |
| 4221 | ><P |
| 4222 | >This is only true of the GNU version of |
| 4223 | <B |
| 4224 | CLASS="COMMAND" |
| 4225 | >tr</B |
| 4226 | >, not the generic version often found on |
| 4227 | commercial UNIX systems.</P |
| 4228 | ></TD |
| 4229 | ></TR |
| 4230 | ></TABLE |
| 4231 | ><DIV |
| 4232 | CLASS="NAVFOOTER" |
| 4233 | ><HR |
| 4234 | ALIGN="LEFT" |
| 4235 | WIDTH="100%"><TABLE |
| 4236 | SUMMARY="Footer navigation table" |
| 4237 | WIDTH="100%" |
| 4238 | BORDER="0" |
| 4239 | CELLPADDING="0" |
| 4240 | CELLSPACING="0" |
| 4241 | ><TR |
| 4242 | ><TD |
| 4243 | WIDTH="33%" |
| 4244 | ALIGN="left" |
| 4245 | VALIGN="top" |
| 4246 | ><A |
| 4247 | HREF="timedate.html" |
| 4248 | ACCESSKEY="P" |
| 4249 | >Prev</A |
| 4250 | ></TD |
| 4251 | ><TD |
| 4252 | WIDTH="34%" |
| 4253 | ALIGN="center" |
| 4254 | VALIGN="top" |
| 4255 | ><A |
| 4256 | HREF="index.html" |
| 4257 | ACCESSKEY="H" |
| 4258 | >Home</A |
| 4259 | ></TD |
| 4260 | ><TD |
| 4261 | WIDTH="33%" |
| 4262 | ALIGN="right" |
| 4263 | VALIGN="top" |
| 4264 | ><A |
| 4265 | HREF="filearchiv.html" |
| 4266 | ACCESSKEY="N" |
| 4267 | >Next</A |
| 4268 | ></TD |
| 4269 | ></TR |
| 4270 | ><TR |
| 4271 | ><TD |
| 4272 | WIDTH="33%" |
| 4273 | ALIGN="left" |
| 4274 | VALIGN="top" |
| 4275 | >Time / Date Commands</TD |
| 4276 | ><TD |
| 4277 | WIDTH="34%" |
| 4278 | ALIGN="center" |
| 4279 | VALIGN="top" |
| 4280 | ><A |
| 4281 | HREF="external.html" |
| 4282 | ACCESSKEY="U" |
| 4283 | >Up</A |
| 4284 | ></TD |
| 4285 | ><TD |
| 4286 | WIDTH="33%" |
| 4287 | ALIGN="right" |
| 4288 | VALIGN="top" |
| 4289 | >File and Archiving Commands</TD |
| 4290 | ></TR |
| 4291 | ></TABLE |
| 4292 | ></DIV |
| 4293 | ></BODY |
| 4294 | ></HTML |
| 4295 | > |