220
◾
Linux with Operating System Concepts
Notice among the responses shown in Figure 6.1 that many files contain IP addresses
(many of which are for localhost, 127.0.0.1) or define netmasks (we
discuss netmasks in
Chapter 12). One interesting match is for the file prelink.cache. This file is a binary file and
really should not have matched. To ignore binary files, we add the option –I.
There are many other useful options available in grep. The option –c responds with the
total number of matches rather than the matches themselves. If
grep executes on a number
of files, each filename is listed along with the number of matches in that file, including 0 if
no lines in the file matched the regular expression.
The –n option will insert line numbers before the file name. If we wish to discard the
filenames, we can use –h. The option –H forces grep to display the filenames. The option –h
is used by default if grep is only searching a single file while –H is used by default if grep is
searching multiple files. Figure 6.3 shows the output from the
same command as shown in
Figure 6.2, but with the –H, –I, and –n options applied.
The option –i forces egrep to ignore case. This allows you to specify letters in your regu-
lar expression without having to differentiate upper versus lower case. For instance,
egrep ‘[ABCabc]’
somefile
and
egrep –i ‘[abc]’
somefile
will accomplish the same task.
Recall the challenge of properly using
[^…]
to denote “NOT.” A far easier solution is to
use the –v option, which means “invert the match.” For instance, if
we want to search for all
strings that do not contain a period, we could try to write a regular expression that conveys
this, but we could also use the command
egrep –v ‘\.’
somefile
networks:loopback 127.0.0.0
networks:link-local 169.254.0.0
ntp.conf:restrict 127.0.0.1
ntp.conf:#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
ntp.conf:#broadcast 192.168.1.255 autokey #
broadcast server
ntp.conf:#broadcast 224.0.1.1 autokey
# multicast server
ntp.conf:#multicastclient 224.0.1.1
# multicast client
ntp.conf:#manycastserver 239.255.254.254 #
manycast server
ntp.conf:#server 127.127.1.0 # local clock
openct.conf: #
>=
linux-2.6.27.14
openct.conf: #
>=
linux-2.6.28.3
pam_ldap.conf:host 127.0.0.1
pam_ldap.conf:#uri ldap://127.0.0.1/
pam_ldap.conf:#uri ldaps://127.0.0.1/
Binary file prelink.cache matches
resolv.conf:nameserver 172.28.102.11
resolv.conf:nameserver 172.28.102.13
FIGURE 6.2
Do'stlaringiz bilan baham: