初めての grep の使い方ーオプション無しで使ってみるー

grepはFILENAMEからPATTERNを含む行を探すコマンドで、次のようにかきます

$ grep PATTERN FILE

今回は最も簡単な、オプション無しでgrepを使ってみます。
ubuntuのネットワーク関連ファイルを例にとって使ってみます。

1. /etc/network/interfacesの内容を見ます。

$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
     address 192.168.0.10
     netmask 255.255.255.0
     network 192.168.0.0
     broadcast 192.168.0.255
     gateway 192.168.0.1
     # dns-* options are implemented by the resolvconf package, if installed
     dns-nameservers 210.188.224.11
     dns-search sakura.ne.jp
post-up /usr/sbin/ethtool -K eth0 two of

2. /etc/network/interfacesのうち、addressを含む行を探します。

$ grep address /etc/network/interface
     address 192.168.0.10


また、grepはパイプでも使え、commandの結果からPATTERNの文字列を含む行を抽出します。

$ command | grep PATTERN 

ifconfigの結果から、eth0を含む行だけを抽出したいときは、このように書きます。

$ ifconfig | grep eth0
eth0      Link encap:Ethernet  HWaddr 52:54:00:03:92:00