-->
Page 75
Obviously, this is not the result you want, because the multiple searches have printed duplicate filenames. To find exactly what you want, use a regular expression that tells ls to list any file with a number appearing in the filename, for example:
# ls *[0123456789]* 0001file.0009 32days.msg day67.txt 08100097.db 96hours.txt message.76 14days.txt backup001.file phonelog.111597
This shows all files containing numbers in the filename, because you've specified a range of characters, or in this case, numbers in your search pattern. You can also use a regular expression shorthand to build a shorter expression to do the same thing, for example:
# ls *[0-9]* 0001file.0009 32days.msg day67.txt 08100097.db 96hours.txt message.76 14days.txt backup001.file phonelog.111597
How you specify your pattern characters in your expression is important. If you only want a list of files ending in a number, you can use
# ls *[0-9] 0001file.0009 message.76 phonelog.111597
If you only want a list of files beginning with a number, you can use
# ls [0-9]* 0001file.0009 08100097.db 14days.txt 32days.msg 96hours.txt
Here's a fun exercise: What if you want to list only those files with numbers inside or on both ends of a filename? Try these:
# ls *[-a-z][0-9]* backup001.file day67.txt # ls [0-9]*[a-z]*[0-9] 0001file.0009
Finally, how do you match patterns when the pattern you're looking for contains a pattern-matching character? Easy! Use the backlash to "escape" the character, for example:
# ls *\?* cathy?.message
As you can see, using regular expressions can take some practice, but your efforts will be well rewarded. Try experimenting with different expressions to see if you can come
up with results similar to the examples shown in this section.
This section introduces you to the family of grep commands. You'll learn about grep, egrep, and fgrep. In order to use these commands, you should know how to use some of the pattern-
Page 76
matching techniques already discussed. You'll use these commands to search through files and extract text. Each of these programs works by searching each line in a file. You can search single files or a range of files.
Each of the grep commands is basically the same and has nearly 20 different command-line options. The only real difference is that egrep uses a slightly different syntax for its pattern matching, whereas the frep command uses fixed strings. You'll see examples of each program, using some of the common options. For specific details about the pattern-matching capabilities of these programs, see the grep manual page.
To show you the difference in each program's search pattern syntax, I'll look for different patterns in Matt Welsh's Linux Basic Installation Guide (available at http://sunsite.unc.edu/LDP). For example, if you want to find all lines in the guide that begin with a number, use the following syntax:
# grep ^[0-9] guide.txt 1 Introduction to Linux 1 2 Obtaining and Installing Linux 40 3 Linux Tutorial 85 4 System Administration 137 ... # egrep ^[0-9] guide.txt 1 Introduction to Linux 1 2 Obtaining and Installing Linux 40 3 Linux Tutorial 85 4 System Administration 137 ... # fgrep ^[0-9] guide.txt
You can see that grep and egrep returned a search (I've deleted all the output except the first four lines). Notice, however, that fgrep cannot handle regular expressions. You must use fixed patterns, or strings with the fgrep command, for example:
# fgrep friend guide.txt large extent by the window manager. This friendly program is in copy Linux from a friend who may already have the software, or share (Unfortunately, the system was being unfriendly.)
Now use egrep to try searching for the pattern of the letter b in parentheses in the file:
# egrep "\([b]\)" guide.txt (see Section 1.8 for a list of compatible boards), or (b) there is an connect to the network, or (b) you have a ``dynamic'' IP address,
You see that there are exactly two lines in the file that match (b). See what happens when you search with grep:
# grep "\([b]\)" guide.txt This is version 2.2.2 of the book, "Linux Installation and Getting to PostScript printers. This document was generated by a set of tools from LaTeX source, so there may be a number of formatting problems. This is not the "official" version of the book! Please see ...
Page 77
Whoa! Not exactly what you wanted, is it? As you can see, grep does not use the same syntax as the egrep command. But you can use a simpler approach:
# grep "(b)" guide.txt (see Section 1.8 for a list of compatible boards), or (b) there is an connect to the network, or (b) you have a ``dynamic'' IP address,
This pattern works with grep and fgrep. If you try this pattern with egrep, you'll get the same results as if you tried extended regular expressions with grep (each line with a b).
Each grep program accepts nearly the same set of command-line options. One popular option is -n, or line numbering. This is handy because you'll see which lines in the file contain matches. This example works for each grep program:
# egrep -n "friend" guide.txt 1242:large extent by the window manager. This friendly program is in 1942:copy Linux from a friend who may already have the software, or share 5161:(Unfortunately, the system was being unfriendly.)
You can see that matches were made on lines 1242, 1942, and 5161. Another feature of these programs is that you don't have to retype your patterns each time you want to search. As a simple example, if you need to repeatedly search files for different words, you can put these into a file for grep to use. First, create a text file, then use the -f option to specify the file:
# cat > mywords wonderful Typewriter War # grep -nf mywords guide.txt 574:Typewriter Used to represent screen interaction, as in 617:software since the original Space War, or, more recently, Emacs. It 1998:Now you must be convinced of how wonderful Linux is, and all of the 2549:inanimate object is a wonderful way to relieve the occasional stress 3790: Warning: Linux cannot currently use 33090 sectors of this 7780:to wear the magic hat when it is not needed, despite the wonderful 10091:wonderful programs and configurations are available with a bit of work
Because you also used the line-numbering option, you should note that it had to come before the -f, or file option, or grep would report an error, complain it couldn't find file n, and quit.
You can make grep act like fgrep with the -F option, or like egrep with the -E option. You'll also find a unique version of grep on your system, called zgrep, which you can use to search compressed files, the topic of the next section.