[ Pobierz całość w formacie PDF ]

7.5 Information Commands
This section discusses the commands that will alter a le, perform a certain operation on the le,
or display statistics on the le.
grep -nvwx -number expression le1 le2 . . . leN
One of the most useful commands in Unix is grep, the generalized regular expression parser.
This is a fancy name for a utility which can only search a text le. The easiest way to use grep is
like this:
home larry cat animals
Animals are very interesting creatures. One of my favorite animals is
the tiger, a fearsome beast with large teeth.
I also like the lion---it' s really neat!
home larry grep iger animals
the tiger, a fearsome beast with large teeth.
home larry
One disadvantage of this is, although it shows you all the lines containing your word, it doesn't
7.5. INFORMATION COMMANDS 67
tell you where to look in the le|no line number. Depending on what you're doing, this might be
ne. For instance, if you're looking for errors from a programs output, you might try a. out | grep
error, where a. out is your program's name.
If you're interested in where the match es are, use the n switch to grep to tell it to print line
numbers. Use the v switch if you want to see all the lines that don' t match the speci ed expression.
Another feature of grep is that it matches only parts of a word, like my example above where
iger matched tiger. To tell grep to only match whole words, use the w, and the x switch will tell
grep to only match whole lines.
If you don't specify any les, grep will examine stdin.
wc -clw le1 le2 . . . leN
wc stands for word count. It simply counts the number of words, lines, and characters in the
le s . If there aren't any les speci ed on the command line, it operates on stdin.
The three parameters, clw, stand for character, line, and word respectively, and tell wc which
of the three to count. Thus, wc -cw will count the number of characters and words, but not the
number of lines. wc defaults to counting everything|words, lines, and characters.
One nice use of wc is to nd how many les are in the present directory: ls | wc -w. If you
wanted to see how many les that ended with . c there are, try ls *. c | wc -w.
spell le1 le2 . . . leN
spell is a very simple Unix spelling program, usually for American English.3 spell is a lter,
like most of the other programs we've talked about, which sucks in an ASCII text le and outputs
all the words it considers misspellings. spell operates on the les listed in the command line, or, if
there weren't any there, stdin.
A more sophisticated spelling program, ispell is probably also available on your machine.
ispell will o er possible correct spellings and a fancy menu interface if a lename is speci ed on
the command line or will run as a lter-like program if no les are speci ed.
While operation of ispell should be fairly obvious, consult the man page if you need more help.
cmp le1 le2
cmp compares two les. The rst must be listed on the command line, while the second is either
listed as the second parameter or is read in from standard input. cmp is very simple, and merely
tells you where the two les rst di er.
3
While there are versions of this for several other European languages, the copy on your Linux machine is most
likely for American English.
68 CHAPTER 7. POWERFUL LITTLE PROGRAMS
diff le1 le2
One of the most complicated standard Unix commands is called diff. The GNU version of diff
has over twenty command line options! It is a much more powerful version of cmp and shows you
what the di erences are instead of merely telling you where the rst one is.
Since talking about even a good portion of diff is beyond the scope of this book, I'll just talk
about the basic operation of diff. In short, diff takes two parameters and displays the di erences
between them on a line-by-line basis. For instance:
home larry cat frog
Animals are very interesting creatures. One of my favorite animals is
the tiger, a fearsome beast with large teeth.
I also like the lion---it' s really neat!
home larry cp frog toad
home larry diff frog toad
home larry cat dog
Animals are very nteresting creatures. One of my favorite animals is
the tiger, a fearsome beast with large teeth.
I also like the lion---it's really neat!
home larry diff frog dog
1c1,2
Animals are very interesting creatures. One of my favorite animals is
---
Animals are very nteresting creatures. One of my favorite animals is
3c4
I also like the lion---it's really neat!
---
I also like the lion---it's really neat!
home larry
As you can see, diff outputs nothing when the two les are identical. Then, when I compared
two di erent les, it had a section header, 1c1, 2 saying it was comparing line 1 of the left le, frog,
to lines 1 2 of dog and what di erences it noticed. Then it compared line 3 of frog to line 4 of dog.
While it may seem strange at rst to compare di erent line numbers, it is much more e cent then
listing out every single line if there is an extra return early in one le.
gzip -v le1 le2 . . . leN
gunzip -v le1 le2 . . . leN
zcat le1 le2 . . . leN
These three programs are used to compress and decompress data. gzip, or GNU Zip, is the
7.5. INFORMATION COMMANDS 69
program that reads in the original le s and outputs les that are smaller. gzip deletes the les
speci ed on the command line and replaces them with les that have an identical name except that
they have . gz" appended to them.
tr string1 string2
The translate characters" command operates on standard input|it doesn't accept a lename as
a parameter. Instead, it's two parameters are arbitrary strings. It replaces all occurences of string1
in the input with string2. In addition to relatively simple commands such as tr frog toad, tr
can accept more complicated commands. For instance, here's a quick way of converting lowercase
characters into uppercase ones:
home larry tr : lower: : upper:
this is a WEIRD sentence.
THIS IS A WEIRD SENTENCE.
tr is fairly complex and usually used in small shell programs.
70 CHAPTER 7. POWERFUL LITTLE PROGRAMS
Chapter 8
Editing les with Emacs
FUNNY SOMETHING OR OTHER
8.1 What's Emacs?
In order to get anything done on a computer, you need a way to put text into les, and a way to
change text that's already in les. An editor is a program for doing this. Emacs is one of the most
popular editors around|partly because it's very easy for a complete beginner to get actual work
done with it. The classic Unix editor, vi, is covered in Appendix A.
To learn emacs, you need to nd a le of plain text letters, numbers, and the like , copy it to
your home directory1 we don't want to modify the actual le, if it contains important information ,
and invoke Emacs on the le:
home larry emacs README
Of course, if you decided to copy etc rc, etc inittab, or any other le, substitute that le
name for README. For instance, if you cp etc rc ~ rc, then emacs rc.
Invoking" Emacs can have di erent e ects depending on where where you do it. From a plain
console displaying only text characters, Emacs will just take over the whole console. If you invoke
it from X, Emacs will actually bring up its own window. I will assume that you are doing it from a
text console, but everything carries over logically into the X Windows version|just substitute the
word window" in the places I've written screen". Also, remeber that you have to move the mouse
pointer into Emacs's window to type in it! [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • ssaver.htw.pl