Debian Codegraph @

HTML/textproc.html

raw symbols SCIP
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&nbsp;cat list-1 list-2 list-3 | sort | uniq &#62; final.list
211 2&nbsp;# Concatenates the list files,
212 3&nbsp;# sorts them,
213 4&nbsp;# removes duplicate lines,
214 5&nbsp;# 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&nbsp;#!/bin/bash
337 2&nbsp;# wf.sh: Crude word frequency analysis on a text file.
338 3&nbsp;# This is a more efficient version of the "wf2.sh" script.
339 4&nbsp;
340 5&nbsp;
341 6&nbsp;# Check for input file on command-line.
342 7&nbsp;ARGS=1
343 8&nbsp;E_BADARGS=85
344 9&nbsp;E_NOFILE=86
345 10&nbsp;
346 11&nbsp;if [ $# -ne "$ARGS" ] # Correct number of arguments passed to script?
347 12&nbsp;then
348 13&nbsp; echo "Usage: `basename $0` filename"
349 14&nbsp; exit $E_BADARGS
350 15&nbsp;fi
351 16&nbsp;
352 17&nbsp;if [ ! -f "$1" ] # Check if file exists.
353 18&nbsp;then
354 19&nbsp; echo "File \"$1\" does not exist."
355 20&nbsp; exit $E_NOFILE
356 21&nbsp;fi
357 22&nbsp;
358 23&nbsp;
359 24&nbsp;
360 25&nbsp;########################################################
361 26&nbsp;# main ()
362 27&nbsp;sed -e 's/\.//g' -e 's/\,//g' -e 's/ /\
363 28&nbsp;/g' "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
364 29&nbsp;# =========================
365 30&nbsp;# Frequency of occurrence
366 31&nbsp;
367 32&nbsp;# Filter out periods and commas, and
368 33&nbsp;#+ change space between words to linefeed,
369 34&nbsp;#+ then shift characters to lowercase, and
370 35&nbsp;#+ finally prefix occurrence count and sort numerically.
371 36&nbsp;
372 37&nbsp;# Arun Giridhar suggests modifying the above to:
373 38&nbsp;# . . . | sort | uniq -c | sort +1 [-f] | sort +0 -nr
374 39&nbsp;# This adds a secondary sort key, so instances of
375 40&nbsp;#+ equal occurrence are sorted alphabetically.
376 41&nbsp;# As he explains it:
377 42&nbsp;# "This is effectively a radix sort, first on the
378 43&nbsp;#+ least significant column
379 44&nbsp;#+ (word or string, optionally case-insensitive)
380 45&nbsp;#+ and last on the most significant column (frequency)."
381 46&nbsp;#
382 47&nbsp;# As Frank Wang explains, the above is equivalent to
383 48&nbsp;#+ . . . | sort | uniq -c | sort +0 -nr
384 49&nbsp;#+ and the following also works:
385 50&nbsp;#+ . . . | sort | uniq -c | sort -k1nr -k
386 51&nbsp;########################################################
387 52&nbsp;
388 53&nbsp;exit 0
389 54&nbsp;
390 55&nbsp;# Exercises:
391 56&nbsp;# ---------
392 57&nbsp;# 1) Add 'sed' commands to filter out other punctuation,
393 58&nbsp;#+ such as semicolons.
394 59&nbsp;# 2) Modify the script to also filter out multiple spaces and
395 60&nbsp;#+ 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&nbsp;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&nbsp;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&nbsp;# List all the users in /etc/passwd.
614 2&nbsp;
615 3&nbsp;FILENAME=/etc/passwd
616 4&nbsp;
617 5&nbsp;for user in $(cut -d: -f1 $FILENAME)
618 6&nbsp;do
619 7&nbsp; echo $user
620 8&nbsp;done
621 9&nbsp;
622 10&nbsp;# 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&nbsp;File: 1.data
832 2&nbsp;
833 3&nbsp;100 Shoes
834 4&nbsp;200 Laces
835 5&nbsp;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&nbsp;File: 2.data
850 2&nbsp;
851 3&nbsp;100 $40.00
852 4&nbsp;200 $1.00
853 5&nbsp;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&nbsp;#!/bin/bash
955 2&nbsp;# script-detector.sh: Detects scripts within a directory.
956 3&nbsp;
957 4&nbsp;TESTCHARS=2 # Test first 2 characters.
958 5&nbsp;SHABANG='#!' # Scripts begin with a "sha-bang."
959 6&nbsp;
960 7&nbsp;for file in * # Traverse all the files in current directory.
961 8&nbsp;do
962 9&nbsp; if [[ `head -c$TESTCHARS "$file"` = "$SHABANG" ]]
963 10&nbsp; # head -c2 #!
964 11&nbsp; # The '-c' option to "head" outputs a specified
965 12&nbsp; #+ number of characters, rather than lines (the default).
966 13&nbsp; then
967 14&nbsp; echo "File \"$file\" is a script."
968 15&nbsp; else
969 16&nbsp; echo "File \"$file\" is *not* a script."
970 17&nbsp; fi
971 18&nbsp;done
972 19&nbsp;
973 20&nbsp;exit 0
974 21&nbsp;
975 22&nbsp;# Exercises:
976 23&nbsp;# ---------
977 24&nbsp;# 1) Modify this script to take as an optional argument
978 25&nbsp;#+ the directory to scan for scripts
979 26&nbsp;#+ (rather than just the current working directory).
980 27&nbsp;#
981 28&nbsp;# 2) As it stands, this script gives "false positives" for
982 29&nbsp;#+ Perl, awk, and other scripting language scripts.
983 30&nbsp;# 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&nbsp;#!/bin/bash
1008 2&nbsp;# rnd.sh: Outputs a 10-digit random number
1009 3&nbsp;
1010 4&nbsp;# Script by Stephane Chazelas.
1011 5&nbsp;
1012 6&nbsp;head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
1013 7&nbsp;
1014 8&nbsp;
1015 9&nbsp;# =================================================================== #
1016 10&nbsp;
1017 11&nbsp;# Analysis
1018 12&nbsp;# --------
1019 13&nbsp;
1020 14&nbsp;# head:
1021 15&nbsp;# -c4 option takes first 4 bytes.
1022 16&nbsp;
1023 17&nbsp;# od:
1024 18&nbsp;# -N4 option limits output to 4 bytes.
1025 19&nbsp;# -tu4 option selects unsigned decimal format for output.
1026 20&nbsp;
1027 21&nbsp;# sed:
1028 22&nbsp;# -n option, in combination with "p" flag to the "s" command,
1029 23&nbsp;# outputs only matched lines.
1030 24&nbsp;
1031 25&nbsp;
1032 26&nbsp;
1033 27&nbsp;# The author of this script explains the action of 'sed', as follows.
1034 28&nbsp;
1035 29&nbsp;# head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'
1036 30&nbsp;# ----------------------------------&#62; |
1037 31&nbsp;
1038 32&nbsp;# Assume output up to "sed" --------&#62; |
1039 33&nbsp;# is 0000000 1198195154\n
1040 34&nbsp;
1041 35&nbsp;# sed begins reading characters: 0000000 1198195154\n.
1042 36&nbsp;# Here it finds a newline character,
1043 37&nbsp;#+ so it is ready to process the first line (0000000 1198195154).
1044 38&nbsp;# It looks at its &#60;range&#62;&#60;action&#62;s. The first and only one is
1045 39&nbsp;
1046 40&nbsp;# range action
1047 41&nbsp;# 1 s/.* //p
1048 42&nbsp;
1049 43&nbsp;# The line number is in the range, so it executes the action:
1050 44&nbsp;#+ tries to substitute the longest string ending with a space in the line
1051 45&nbsp;# ("0000000 ") with nothing (//), and if it succeeds, prints the result
1052 46&nbsp;# ("p" is a flag to the "s" command here, this is different
1053 47&nbsp;#+ from the "p" command).
1054 48&nbsp;
1055 49&nbsp;# sed is now ready to continue reading its input. (Note that before
1056 50&nbsp;#+ continuing, if -n option had not been passed, sed would have printed
1057 51&nbsp;#+ the line once again).
1058 52&nbsp;
1059 53&nbsp;# Now, sed reads the remainder of the characters, and finds the
1060 54&nbsp;#+ end of the file.
1061 55&nbsp;# It is now ready to process its 2nd line (which is also numbered '$' as
1062 56&nbsp;#+ it's the last one).
1063 57&nbsp;# It sees it is not matched by any &#60;range&#62;, so its job is done.
1064 58&nbsp;
1065 59&nbsp;# In few word this sed commmand means:
1066 60&nbsp;# "On the first line only, remove any character up to the right-most space,
1067 61&nbsp;#+ then print it."
1068 62&nbsp;
1069 63&nbsp;# A better way to do this would have been:
1070 64&nbsp;# sed -e 's/.* //;q'
1071 65&nbsp;
1072 66&nbsp;# Here, two &#60;range&#62;&#60;action&#62;s (could have been written
1073 67&nbsp;# sed -e 's/.* //' -e q):
1074 68&nbsp;
1075 69&nbsp;# range action
1076 70&nbsp;# nothing (matches line) s/.* //
1077 71&nbsp;# nothing (matches line) q (quit)
1078 72&nbsp;
1079 73&nbsp;# Here, sed only reads its first line of input.
1080 74&nbsp;# It performs both actions, and prints the line (substituted) before
1081 75&nbsp;#+ quitting (because of the "q" action) since the "-n" option is not passed.
1082 76&nbsp;
1083 77&nbsp;# =================================================================== #
1084 78&nbsp;
1085 79&nbsp;# An even simpler altenative to the above one-line script would be:
1086 80&nbsp;# head -c4 /dev/urandom| od -An -tu4
1087 81&nbsp;
1088 82&nbsp;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&nbsp;#!/bin/bash
1149 2&nbsp;
1150 3&nbsp;filename=sys.log
1151 4&nbsp;
1152 5&nbsp;cat /dev/null &#62; $filename; echo "Creating / cleaning out file."
1153 6&nbsp;# Creates the file if it does not already exist,
1154 7&nbsp;#+ and truncates it to zero length if it does.
1155 8&nbsp;# : &#62; filename and &#62; filename also work.
1156 9&nbsp;
1157 10&nbsp;tail /var/log/messages &#62; $filename
1158 11&nbsp;# /var/log/messages must have world read permission for this to work.
1159 12&nbsp;
1160 13&nbsp;echo "$filename contains tail end of system log."
1161 14&nbsp;
1162 15&nbsp;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&nbsp;var=$(head -n $m $filename | tail -n $n)
1220 2&nbsp;
1221 3&nbsp;# filename = name of file
1222 4&nbsp;# m = from beginning of file, number of lines to end of block
1223 5&nbsp;# 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&nbsp;grep pattern1 *.txt | grep -v pattern2
1492 2&nbsp;
1493 3&nbsp;# Matches all lines in "*.txt" files containing "pattern1",
1494 4&nbsp;# 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&nbsp;grep -c txt *.sgml # (number of occurrences of "txt" in "*.sgml" files)
1519 2&nbsp;
1520 3&nbsp;
1521 4&nbsp;# grep -cz .
1522 5&nbsp;# ^ dot
1523 6&nbsp;# means count (-c) zero-separated (-z) items matching "."
1524 7&nbsp;# that is, non-empty ones (containing at least 1 character).
1525 8&nbsp;#
1526 9&nbsp;printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz . # 3
1527 10&nbsp;printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '$' # 5
1528 11&nbsp;printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '^' # 5
1529 12&nbsp;#
1530 13&nbsp;printf 'a b\nc d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -c '$' # 9
1531 14&nbsp;# By default, newline chars (\n) separate items to match.
1532 15&nbsp;
1533 16&nbsp;# Note that the -z option is GNU "grep" specific.
1534 17&nbsp;
1535 18&nbsp;
1536 19&nbsp;# 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&nbsp;#!/bin/bash
1594 2&nbsp;# from.sh
1595 3&nbsp;
1596 4&nbsp;# Emulates the useful 'from' utility in Solaris, BSD, etc.
1597 5&nbsp;# Echoes the "From" header line in all messages
1598 6&nbsp;#+ in your e-mail directory.
1599 7&nbsp;
1600 8&nbsp;
1601 9&nbsp;MAILDIR=~/mail/* # No quoting of variable. Why?
1602 10&nbsp;# Maybe check if-exists $MAILDIR: if [ -d $MAILDIR ] . . .
1603 11&nbsp;GREP_OPTS="-H -A 5 --color" # Show file, plus extra context lines
1604 12&nbsp; #+ and display "From" in color.
1605 13&nbsp;TARGETSTR="^From" # "From" at beginning of line.
1606 14&nbsp;
1607 15&nbsp;for file in $MAILDIR # No quoting of variable.
1608 16&nbsp;do
1609 17&nbsp; grep $GREP_OPTS "$TARGETSTR" "$file"
1610 18&nbsp; # ^^^^^^^^^^ # Again, do not quote this variable.
1611 19&nbsp; echo
1612 20&nbsp;done
1613 21&nbsp;
1614 22&nbsp;exit $?
1615 23&nbsp;
1616 24&nbsp;# You might wish to pipe the output of this script to 'more'
1617 25&nbsp;#+ 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&nbsp;SUCCESS=0 # if grep lookup succeeds
1744 2&nbsp;word=Linux
1745 3&nbsp;filename=data.file
1746 4&nbsp;
1747 5&nbsp;grep -q "$word" "$filename" # The "-q" option
1748 6&nbsp; #+ causes nothing to echo to stdout.
1749 7&nbsp;if [ $? -eq $SUCCESS ]
1750 8&nbsp;# if grep -q "$word" "$filename" can replace lines 5 - 7.
1751 9&nbsp;then
1752 10&nbsp; echo "$word found in $filename"
1753 11&nbsp;else
1754 12&nbsp; echo "$word not found in $filename"
1755 13&nbsp;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&nbsp;#!/bin/bash
1792 2&nbsp;# grp.sh: Rudimentary reimplementation of grep.
1793 3&nbsp;
1794 4&nbsp;E_BADARGS=85
1795 5&nbsp;
1796 6&nbsp;if [ -z "$1" ] # Check for argument to script.
1797 7&nbsp;then
1798 8&nbsp; echo "Usage: `basename $0` pattern"
1799 9&nbsp; exit $E_BADARGS
1800 10&nbsp;fi
1801 11&nbsp;
1802 12&nbsp;echo
1803 13&nbsp;
1804 14&nbsp;for file in * # Traverse all files in $PWD.
1805 15&nbsp;do
1806 16&nbsp; output=$(sed -n /"$1"/p $file) # Command substitution.
1807 17&nbsp;
1808 18&nbsp; if [ ! -z "$output" ] # What happens if "$output" is not quoted?
1809 19&nbsp; then
1810 20&nbsp; echo -n "$file: "
1811 21&nbsp; echo "$output"
1812 22&nbsp; fi # sed -ne "/$1/s|^|${file}: |p" is equivalent to above.
1813 23&nbsp;
1814 24&nbsp; echo
1815 25&nbsp;done
1816 26&nbsp;
1817 27&nbsp;echo
1818 28&nbsp;
1819 29&nbsp;exit 0
1820 30&nbsp;
1821 31&nbsp;# Exercises:
1822 32&nbsp;# ---------
1823 33&nbsp;# 1) Add newlines to output, if more than one match in any given file.
1824 34&nbsp;# 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&nbsp;# Filename: tstfile
1877 2&nbsp;
1878 3&nbsp;This is a sample file.
1879 4&nbsp;This is an ordinary text file.
1880 5&nbsp;This file does not contain any unusual text.
1881 6&nbsp;This file is not unusual.
1882 7&nbsp;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&nbsp;#!/bin/bash
1971 2&nbsp;# cw-solver.sh
1972 3&nbsp;# This is actually a wrapper around a one-liner (line 46).
1973 4&nbsp;
1974 5&nbsp;# Crossword puzzle and anagramming word game solver.
1975 6&nbsp;# You know *some* of the letters in the word you're looking for,
1976 7&nbsp;#+ so you need a list of all valid words
1977 8&nbsp;#+ with the known letters in given positions.
1978 9&nbsp;# For example: w...i....n
1979 10&nbsp;# 1???5????10
1980 11&nbsp;# w in position 1, 3 unknowns, i in the 5th, 4 unknowns, n at the end.
1981 12&nbsp;# (See comments at end of script.)
1982 13&nbsp;
1983 14&nbsp;
1984 15&nbsp;E_NOPATT=71
1985 16&nbsp;DICT=/usr/share/dict/word.lst
1986 17&nbsp;# ^^^^^^^^ Looks for word list here.
1987 18&nbsp;# ASCII word list, one word per line.
1988 19&nbsp;# If you happen to need an appropriate list,
1989 20&nbsp;#+ download the author's "yawl" word list package.
1990 21&nbsp;# http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz
1991 22&nbsp;# or
1992 23&nbsp;# http://bash.deta.in/yawl-0.3.2.tar.gz
1993 24&nbsp;
1994 25&nbsp;
1995 26&nbsp;if [ -z "$1" ] # If no word pattern specified
1996 27&nbsp;then #+ as a command-line argument . . .
1997 28&nbsp; echo #+ . . . then . . .
1998 29&nbsp; echo "Usage:" #+ Usage message.
1999 30&nbsp; echo
2000 31&nbsp; echo ""$0" \"pattern,\""
2001 32&nbsp; echo "where \"pattern\" is in the form"
2002 33&nbsp; echo "xxx..x.x..."
2003 34&nbsp; echo
2004 35&nbsp; echo "The x's represent known letters,"
2005 36&nbsp; echo "and the periods are unknown letters (blanks)."
2006 37&nbsp; echo "Letters and periods can be in any position."
2007 38&nbsp; echo "For example, try: sh cw-solver.sh w...i....n"
2008 39&nbsp; echo
2009 40&nbsp; exit $E_NOPATT
2010 41&nbsp;fi
2011 42&nbsp;
2012 43&nbsp;echo
2013 44&nbsp;# ===============================================
2014 45&nbsp;# This is where all the work gets done.
2015 46&nbsp;grep ^"$1"$ "$DICT" # Yes, only one line!
2016 47&nbsp;# | |
2017 48&nbsp;# ^ is start-of-word regex anchor.
2018 49&nbsp;# $ is end-of-word regex anchor.
2019 50&nbsp;
2020 51&nbsp;# From _Stupid Grep Tricks_, vol. 1,
2021 52&nbsp;#+ a book the ABS Guide author may yet get around
2022 53&nbsp;#+ to writing . . . one of these days . . .
2023 54&nbsp;# ===============================================
2024 55&nbsp;echo
2025 56&nbsp;
2026 57&nbsp;
2027 58&nbsp;exit $? # Script terminates here.
2028 59&nbsp;# If there are too many words generated,
2029 60&nbsp;#+ redirect the output to a file.
2030 61&nbsp;
2031 62&nbsp;$ sh cw-solver.sh w...i....n
2032 63&nbsp;
2033 64&nbsp;wellington
2034 65&nbsp;workingman
2035 66&nbsp;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&nbsp;#!/bin/bash
2180 2&nbsp;# dict-lookup.sh
2181 3&nbsp;
2182 4&nbsp;# This script looks up definitions in the 1913 Webster's Dictionary.
2183 5&nbsp;# This Public Domain dictionary is available for download
2184 6&nbsp;#+ from various sites, including
2185 7&nbsp;#+ Project Gutenberg (http://www.gutenberg.org/etext/247).
2186 8&nbsp;#
2187 9&nbsp;# Convert it from DOS to UNIX format (with only LF at end of line)
2188 10&nbsp;#+ before using it with this script.
2189 11&nbsp;# Store the file in plain, uncompressed ASCII text.
2190 12&nbsp;# Set DEFAULT_DICTFILE variable below to path/filename.
2191 13&nbsp;
2192 14&nbsp;
2193 15&nbsp;E_BADARGS=85
2194 16&nbsp;MAXCONTEXTLINES=50 # Maximum number of lines to show.
2195 17&nbsp;DEFAULT_DICTFILE="/usr/share/dict/webster1913-dict.txt"
2196 18&nbsp; # Default dictionary file pathname.
2197 19&nbsp; # Change this as necessary.
2198 20&nbsp;# Note:
2199 21&nbsp;# ----
2200 22&nbsp;# This particular edition of the 1913 Webster's
2201 23&nbsp;#+ begins each entry with an uppercase letter
2202 24&nbsp;#+ (lowercase for the remaining characters).
2203 25&nbsp;# Only the *very first line* of an entry begins this way,
2204 26&nbsp;#+ and that's why the search algorithm below works.
2205 27&nbsp;
2206 28&nbsp;
2207 29&nbsp;
2208 30&nbsp;if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]]
2209 31&nbsp;# Must at least specify word to look up, and
2210 32&nbsp;#+ it must start with an uppercase letter.
2211 33&nbsp;then
2212 34&nbsp; echo "Usage: `basename $0` Word-to-define [dictionary-file]"
2213 35&nbsp; echo
2214 36&nbsp; echo "Note: Word to look up must start with capital letter,"
2215 37&nbsp; echo "with the rest of the word in lowercase."
2216 38&nbsp; echo "--------------------------------------------"
2217 39&nbsp; echo "Examples: Abandon, Dictionary, Marking, etc."
2218 40&nbsp; exit $E_BADARGS
2219 41&nbsp;fi
2220 42&nbsp;
2221 43&nbsp;
2222 44&nbsp;if [ -z "$2" ] # May specify different dictionary
2223 45&nbsp; #+ as an argument to this script.
2224 46&nbsp;then
2225 47&nbsp; dictfile=$DEFAULT_DICTFILE
2226 48&nbsp;else
2227 49&nbsp; dictfile="$2"
2228 50&nbsp;fi
2229 51&nbsp;
2230 52&nbsp;# ---------------------------------------------------------
2231 53&nbsp;Definition=$(fgrep -A $MAXCONTEXTLINES "$1 \\" "$dictfile")
2232 54&nbsp;# Definitions in form "Word \..."
2233 55&nbsp;#
2234 56&nbsp;# And, yes, "fgrep" is fast enough
2235 57&nbsp;#+ to search even a very large text file.
2236 58&nbsp;
2237 59&nbsp;
2238 60&nbsp;# Now, snip out just the definition block.
2239 61&nbsp;
2240 62&nbsp;echo "$Definition" |
2241 63&nbsp;sed -n '1,/^[A-Z]/p' |
2242 64&nbsp;# Print from first line of output
2243 65&nbsp;#+ to the first line of the next entry.
2244 66&nbsp;sed '$d' | sed '$d'
2245 67&nbsp;# Delete last two lines of output
2246 68&nbsp;#+ (blank line and first line of next entry).
2247 69&nbsp;# ---------------------------------------------------------
2248 70&nbsp;
2249 71&nbsp;exit $?
2250 72&nbsp;
2251 73&nbsp;# Exercises:
2252 74&nbsp;# ---------
2253 75&nbsp;# 1) Modify the script to accept any type of alphabetic input
2254 76&nbsp;# + (uppercase, lowercase, mixed case), and convert it
2255 77&nbsp;# + to an acceptable format for processing.
2256 78&nbsp;#
2257 79&nbsp;# 2) Convert the script to a GUI application,
2258 80&nbsp;# + using something like 'gdialog' or 'zenity' . . .
2259 81&nbsp;# The script will then no longer take its argument(s)
2260 82&nbsp;# + from the command-line.
2261 83&nbsp;#
2262 84&nbsp;# 3) Modify the script to parse one of the other available
2263 85&nbsp;# + 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&nbsp;#!/bin/bash
2438 2&nbsp;# lookup: Does a dictionary lookup on each word in a data file.
2439 3&nbsp;
2440 4&nbsp;file=words.data # Data file from which to read words to test.
2441 5&nbsp;
2442 6&nbsp;echo
2443 7&nbsp;echo "Testing file $file"
2444 8&nbsp;echo
2445 9&nbsp;
2446 10&nbsp;while [ "$word" != end ] # Last word in data file.
2447 11&nbsp;do # ^^^
2448 12&nbsp; read word # From data file, because of redirection at end of loop.
2449 13&nbsp; look $word &#62; /dev/null # Don't want to display lines in dictionary file.
2450 14&nbsp; # Searches for words in the file /usr/share/dict/words
2451 15&nbsp; #+ (usually a link to linux.words).
2452 16&nbsp; lookup=$? # Exit status of 'look' command.
2453 17&nbsp;
2454 18&nbsp; if [ "$lookup" -eq 0 ]
2455 19&nbsp; then
2456 20&nbsp; echo "\"$word\" is valid."
2457 21&nbsp; else
2458 22&nbsp; echo "\"$word\" is invalid."
2459 23&nbsp; fi
2460 24&nbsp;
2461 25&nbsp;done &#60;"$file" # Redirects stdin to $file, so "reads" come from there.
2462 26&nbsp;
2463 27&nbsp;echo
2464 28&nbsp;
2465 29&nbsp;exit 0
2466 30&nbsp;
2467 31&nbsp;# ----------------------------------------------------------------
2468 32&nbsp;# Code below line will not execute because of "exit" command above.
2469 33&nbsp;
2470 34&nbsp;
2471 35&nbsp;# Stephane Chazelas proposes the following, more concise alternative:
2472 36&nbsp;
2473 37&nbsp;while read word &#38;&#38; [[ $word != end ]]
2474 38&nbsp;do if look "$word" &#62; /dev/null
2475 39&nbsp; then echo "\"$word\" is valid."
2476 40&nbsp; else echo "\"$word\" is invalid."
2477 41&nbsp; fi
2478 42&nbsp;done &#60;"$file"
2479 43&nbsp;
2480 44&nbsp;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&nbsp;$ ls *.txt | wc -l
2638 2&nbsp;# Will work as long as none of the "*.txt" files
2639 3&nbsp;#+ have a linefeed embedded in their name.
2640 4&nbsp;
2641 5&nbsp;# Alternative ways of doing this are:
2642 6&nbsp;# find . -maxdepth 1 -name \*.txt -print0 | grep -cz .
2643 7&nbsp;# (shopt -s nullglob; set -- *.txt; echo $#)
2644 8&nbsp;
2645 9&nbsp;# 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&nbsp;... | grep foo | wc -l
2745 2&nbsp;# This frequently used construct can be more concisely rendered.
2746 3&nbsp;
2747 4&nbsp;... | grep -c foo
2748 5&nbsp;# Just use the "-c" (or "--count") option of grep.
2749 6&nbsp;
2750 7&nbsp;# 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" "*" &#60;filename</B
2806 ></TT
2807 >
2808 or <TT
2809 CLASS="USERINPUT"
2810 ><B
2811 >tr A-Z \* &#60;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&nbsp;echo "abcdef" # abcdef
2849 2&nbsp;echo "abcdef" | tr -d b-d # aef
2850 3&nbsp;
2851 4&nbsp;
2852 5&nbsp;tr -d 0-9 &#60;filename
2853 6&nbsp;# 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&nbsp;#!/bin/bash
3015 2&nbsp;# Changes a file to all uppercase.
3016 3&nbsp;
3017 4&nbsp;E_BADARGS=85
3018 5&nbsp;
3019 6&nbsp;if [ -z "$1" ] # Standard check for command-line arg.
3020 7&nbsp;then
3021 8&nbsp; echo "Usage: `basename $0` filename"
3022 9&nbsp; exit $E_BADARGS
3023 10&nbsp;fi
3024 11&nbsp;
3025 12&nbsp;tr a-z A-Z &#60;"$1"
3026 13&nbsp;
3027 14&nbsp;# Same effect as above, but using POSIX character set notation:
3028 15&nbsp;# tr '[:lower:]' '[:upper:]' &#60;"$1"
3029 16&nbsp;# Thanks, S.C.
3030 17&nbsp;
3031 18&nbsp;# Or even . . .
3032 19&nbsp;# cat "$1" | tr a-z A-Z
3033 20&nbsp;# Or dozens of other ways . . .
3034 21&nbsp;
3035 22&nbsp;exit 0
3036 23&nbsp;
3037 24&nbsp;# Exercise:
3038 25&nbsp;# Rewrite this script to give the option of changing a file
3039 26&nbsp;#+ to *either* upper or lowercase.
3040 27&nbsp;# 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&nbsp;#!/bin/bash
3067 2&nbsp;#
3068 3&nbsp;# Changes every filename in working directory to all lowercase.
3069 4&nbsp;#
3070 5&nbsp;# Inspired by a script of John Dubois,
3071 6&nbsp;#+ which was translated into Bash by Chet Ramey,
3072 7&nbsp;#+ and considerably simplified by the author of the ABS Guide.
3073 8&nbsp;
3074 9&nbsp;
3075 10&nbsp;for filename in * # Traverse all files in directory.
3076 11&nbsp;do
3077 12&nbsp; fname=`basename $filename`
3078 13&nbsp; n=`echo $fname | tr A-Z a-z` # Change name to lowercase.
3079 14&nbsp; if [ "$fname" != "$n" ] # Rename only files not already lowercase.
3080 15&nbsp; then
3081 16&nbsp; mv $fname $n
3082 17&nbsp; fi
3083 18&nbsp;done
3084 19&nbsp;
3085 20&nbsp;exit $?
3086 21&nbsp;
3087 22&nbsp;
3088 23&nbsp;# Code below this line will not execute because of "exit".
3089 24&nbsp;#--------------------------------------------------------#
3090 25&nbsp;# To run it, delete script above line.
3091 26&nbsp;
3092 27&nbsp;# The above script will not work on filenames containing blanks or newlines.
3093 28&nbsp;# Stephane Chazelas therefore suggests the following alternative:
3094 29&nbsp;
3095 30&nbsp;
3096 31&nbsp;for filename in * # Not necessary to use basename,
3097 32&nbsp; # since "*" won't return any file containing "/".
3098 33&nbsp;do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`
3099 34&nbsp;# POSIX char set notation.
3100 35&nbsp;# Slash added so that trailing newlines are not
3101 36&nbsp;# removed by command substitution.
3102 37&nbsp; # Variable substitution:
3103 38&nbsp; n=${n%/} # Removes trailing slash, added above, from filename.
3104 39&nbsp; [[ $filename == $n ]] || mv "$filename" "$n"
3105 40&nbsp; # Checks if filename already lowercase.
3106 41&nbsp;done
3107 42&nbsp;
3108 43&nbsp;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&nbsp;#!/bin/bash
3139 2&nbsp;# Du.sh: DOS to UNIX text file converter.
3140 3&nbsp;
3141 4&nbsp;E_WRONGARGS=85
3142 5&nbsp;
3143 6&nbsp;if [ -z "$1" ]
3144 7&nbsp;then
3145 8&nbsp; echo "Usage: `basename $0` filename-to-convert"
3146 9&nbsp; exit $E_WRONGARGS
3147 10&nbsp;fi
3148 11&nbsp;
3149 12&nbsp;NEWFILENAME=$1.unx
3150 13&nbsp;
3151 14&nbsp;CR='\015' # Carriage return.
3152 15&nbsp; # 015 is octal ASCII code for CR.
3153 16&nbsp; # Lines in a DOS text file end in CR-LF.
3154 17&nbsp; # Lines in a UNIX text file end in LF only.
3155 18&nbsp;
3156 19&nbsp;tr -d $CR &#60; $1 &#62; $NEWFILENAME
3157 20&nbsp;# Delete CR's and write to new file.
3158 21&nbsp;
3159 22&nbsp;echo "Original DOS text file is \"$1\"."
3160 23&nbsp;echo "Converted UNIX text file is \"$NEWFILENAME\"."
3161 24&nbsp;
3162 25&nbsp;exit 0
3163 26&nbsp;
3164 27&nbsp;# Exercise:
3165 28&nbsp;# --------
3166 29&nbsp;# 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&nbsp;#!/bin/bash
3192 2&nbsp;# rot13.sh: Classic rot13 algorithm,
3193 3&nbsp;# encryption that might fool a 3-year old
3194 4&nbsp;# for about 10 minutes.
3195 5&nbsp;
3196 6&nbsp;# Usage: ./rot13.sh filename
3197 7&nbsp;# or ./rot13.sh &#60;filename
3198 8&nbsp;# or ./rot13.sh and supply keyboard input (stdin)
3199 9&nbsp;
3200 10&nbsp;cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' # "a" goes to "n", "b" to "o" ...
3201 11&nbsp;# The cat "$@" construct
3202 12&nbsp;#+ permits input either from stdin or from files.
3203 13&nbsp;
3204 14&nbsp;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&nbsp;#!/bin/bash
3230 2&nbsp;# crypto-quote.sh: Encrypt quotes
3231 3&nbsp;
3232 4&nbsp;# Will encrypt famous quotes in a simple monoalphabetic substitution.
3233 5&nbsp;# The result is similar to the "Crypto Quote" puzzles
3234 6&nbsp;#+ seen in the Op Ed pages of the Sunday paper.
3235 7&nbsp;
3236 8&nbsp;
3237 9&nbsp;key=ETAOINSHRDLUBCFGJMQPVWZYXK
3238 10&nbsp;# The "key" is nothing more than a scrambled alphabet.
3239 11&nbsp;# Changing the "key" changes the encryption.
3240 12&nbsp;
3241 13&nbsp;# The 'cat "$@"' construction gets input either from stdin or from files.
3242 14&nbsp;# If using stdin, terminate input with a Control-D.
3243 15&nbsp;# Otherwise, specify filename as command-line parameter.
3244 16&nbsp;
3245 17&nbsp;cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"
3246 18&nbsp;# | to uppercase | encrypt
3247 19&nbsp;# Will work on lowercase, uppercase, or mixed-case quotes.
3248 20&nbsp;# Passes non-alphabetic characters through unchanged.
3249 21&nbsp;
3250 22&nbsp;
3251 23&nbsp;# Try this script with something like:
3252 24&nbsp;# "Nothing so needs reforming as other people's habits."
3253 25&nbsp;# --Mark Twain
3254 26&nbsp;#
3255 27&nbsp;# Output is:
3256 28&nbsp;# "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ."
3257 29&nbsp;# --BEML PZERC
3258 30&nbsp;
3259 31&nbsp;# To reverse the encryption:
3260 32&nbsp;# cat "$@" | tr "$key" "A-Z"
3261 33&nbsp;
3262 34&nbsp;
3263 35&nbsp;# This simple-minded cipher can be broken by an average 12-year old
3264 36&nbsp;#+ using only pencil and paper.
3265 37&nbsp;
3266 38&nbsp;exit 0
3267 39&nbsp;
3268 40&nbsp;# Exercise:
3269 41&nbsp;# --------
3270 42&nbsp;# Modify the script so that it will either encrypt or decrypt,
3271 43&nbsp;#+ 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&nbsp;#!/bin/bash
3299 2&nbsp;# jabh.sh
3300 3&nbsp;
3301 4&nbsp;x="wftedskaebjgdBstbdbsmnjgz"
3302 5&nbsp;echo $x | tr "a-z" 'oh, turtleneck Phrase Jar!'
3303 6&nbsp;
3304 7&nbsp;# 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&nbsp;#!/bin/bash
3417 2&nbsp;
3418 3&nbsp;WIDTH=40 # 40 columns wide.
3419 4&nbsp;
3420 5&nbsp;b=`ls /usr/local/bin` # Get a file listing...
3421 6&nbsp;
3422 7&nbsp;echo $b | fmt -w $WIDTH
3423 8&nbsp;
3424 9&nbsp;# Could also have been done by
3425 10&nbsp;# echo $b | fold - -s -w $WIDTH
3426 11&nbsp;
3427 12&nbsp;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&nbsp;#!/bin/bash
3539 2&nbsp;# colms.sh
3540 3&nbsp;# A minor modification of the example file in the "column" man page.
3541 4&nbsp;
3542 5&nbsp;
3543 6&nbsp;(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
3544 7&nbsp;; ls -l | sed 1d) | column -t
3545 8&nbsp;# ^^^^^^ ^^
3546 9&nbsp;
3547 10&nbsp;# The "sed 1d" in the pipe deletes the first line of output,
3548 11&nbsp;#+ which would be "total N",
3549 12&nbsp;#+ where "N" is the total number of files found by "ls -l".
3550 13&nbsp;
3551 14&nbsp;# The -t option to "column" pretty-prints a table.
3552 15&nbsp;
3553 16&nbsp;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 &#60;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&nbsp;#!/bin/bash
3694 2&nbsp;# line-number.sh
3695 3&nbsp;
3696 4&nbsp;# This script echoes itself twice to stdout with its lines numbered.
3697 5&nbsp;
3698 6&nbsp;echo " line number = $LINENO" # 'nl' sees this as line 4
3699 7&nbsp;# (nl does not number blank lines).
3700 8&nbsp;# 'cat -n' sees it correctly as line #6.
3701 9&nbsp;
3702 10&nbsp;nl `basename $0`
3703 11&nbsp;
3704 12&nbsp;echo; echo # Now, let's try it with 'cat -n'
3705 13&nbsp;
3706 14&nbsp;cat -n `basename $0`
3707 15&nbsp;# The difference is that 'cat -n' numbers the blank lines.
3708 16&nbsp;# Note that 'nl -ba' will also do so.
3709 17&nbsp;
3710 18&nbsp;exit 0
3711 19&nbsp;# -----------------------------------------------------------------</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&nbsp;# Convert a string from UTF-8 to UTF-16 and print to the BookList
3864 2&nbsp;function write_utf8_string {
3865 3&nbsp; STRING=$1
3866 4&nbsp; BOOKLIST=$2
3867 5&nbsp; echo -n "$STRING" | iconv -f UTF8 -t UTF16 | \
3868 6&nbsp; cut -b 3- | tr -d \\n &#62;&#62; "$BOOKLIST"
3869 7&nbsp;}
3870 8&nbsp;
3871 9&nbsp;# From Peter Knowles' "booklistgen.sh" script
3872 10&nbsp;#+ for converting files to Sony Librie/PRS-50X format.
3873 11&nbsp;# (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&nbsp;texexec --pdfarrange --result=Concatenated.pdf *pdf
3990 2&nbsp;
3991 3&nbsp;# Concatenates all the pdf files in the current working directory
3992 4&nbsp;#+ into the merged file, Concatenated.pdf . . .
3993 5&nbsp;# (The --pdfarrange option repaginates a pdf file. See also --pdfcombine.)
3994 6&nbsp;# 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&nbsp;#!/bin/bash
4111 2&nbsp;# manview.sh: Formats the source of a man page for viewing.
4112 3&nbsp;
4113 4&nbsp;# This script is useful when writing man page source.
4114 5&nbsp;# It lets you look at the intermediate results on the fly
4115 6&nbsp;#+ while working on it.
4116 7&nbsp;
4117 8&nbsp;E_WRONGARGS=85
4118 9&nbsp;
4119 10&nbsp;if [ -z "$1" ]
4120 11&nbsp;then
4121 12&nbsp; echo "Usage: `basename $0` filename"
4122 13&nbsp; exit $E_WRONGARGS
4123 14&nbsp;fi
4124 15&nbsp;
4125 16&nbsp;# ---------------------------
4126 17&nbsp;groff -Tascii -man $1 | less
4127 18&nbsp;# From the man page for groff.
4128 19&nbsp;# ---------------------------
4129 20&nbsp;
4130 21&nbsp;# If the man page includes tables and/or equations,
4131 22&nbsp;#+ then the above code will barf.
4132 23&nbsp;# The following line can handle such cases.
4133 24&nbsp;#
4134 25&nbsp;# gtbl &#60; "$1" | geqn -Tlatin1 | groff -Tlatin1 -mtty-char -man
4135 26&nbsp;#
4136 27&nbsp;# Thanks, S.C.
4137 28&nbsp;
4138 29&nbsp;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 >