This section discusses the commands that will alter a file, perform a certain operation on the file, or display statistics on the file.
grep [-nvwx] [-number] expression [file1 file2 ...fileN]
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 file. The easiest way to use grep is like this:
One disadvantage of this is, although it shows you all the lines containing your word, it doesn't tell you where to look in the file--no line number. Depending on what you're doing, this might be fine. 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 specified 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.
Remember, if you don't specify any files, grep will examine stdin.
wc [-clw] [file1 file2 ...fileN]
wc stands for word count. It simply counts the number of words, lines, and characters in the file(s). If there aren't any files specified 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 find how many files are in the present directory: ls | wc -w. If you wanted to see how many files that ended with .c there were, try ls *.c | wc -w.
spell [file1 file2 ...fileN]
spell is a very simple Unix spelling program, usually for American English. spell is a filter, like most of the other programs we've talked about, which sucks in an ASCII text file and outputs all the words it considers misspellings. spell operates on the files 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 offer possible correct spellings and a fancy menu interface if a filename is specified on the command line or will run as a filter-like program if no files are specified.
While operation of ispell should be fairly obvious, consult the man page if you need more help.
cmp file1 [file2]
cmp compares two files. The first 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 files first differ.
diff file1 file2
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 differences are instead of merely telling you where the first 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 differences between them on a line-by-line basis. For instance:
As you can see, diff outputs nothing when the two files are identical. Then, when I compared two different files, it had a section header, 1c1,2 saying it was comparing line 1 of the left file, frog, to lines 1-2 of dog and what differences it noticed. Then it compared line 3 of frog to line 4 of dog. While it may seem strange at first to compare different line numbers, it is much more efficent then listing out every single line if there is an extra return early in one file.