TOP - Unix系OS - Linux - grep(1)

パターンマッチする行を表示します。

grep オプション1 パターン1 ファイルパス1
grep オプション1 -e パターン1 ファイルパス1
grep オプション1 -f ファイルパス2 ファイルパス1
オプション1(任意)
    -A 値1
        マッチした行に続く(値1)行分を付加して出力

    -B 値1
        マッチした行の前の(値1)行分を付加して出力

    -C 値1
        マッチした行の前、および続く(値1)行分を付加して出力

    -c
        マッチした行数を表示

    -e パターン2
        (パターン2)をパターンとして指定
        「-」で始まるパターンを指定可能

    -f ファイルパス3
        (ファイルパス3)で検索パターンのファイルを指定
        行単位でマッチを確認

    -H
        マッチした行のファイル名も表示

    -i
        大文字、小文字の区別をしない

    -L
        マッチしないファイル名の出力
        マッチ行の出力は無し

    -l
        マッチしたファイル名の出力
        マッチ行の出力は無し

    -n
        行の出力の際に行番号を付加

    -o
        マッチした部分のみを出力

    -R
        ディレクトリ内の全てのファイルを再帰的に読み込み

    -s
        エラーメッセージを抑止

    -v
        マッチしない行を出力

    -w
        ワードに完全にマッチする行を出力

    -x
        行全体に完全にマッチする行を出力

パターン1
    検索に使用するパターン

ファイルパス1
    検索の対象となるファイル

ファイルパス2
    検索パターンを含むファイル

※ オプションなどは個人的に重要と考えられるものを記述しており、記載の無いものは、manページや関連書籍などを参照願います。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はUbuntu 12.10です。

(例)オプション無し
実行結果
$ grep Library /usr/include/stdio.h
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   The GNU C Library is distributed in the hope that it will be useful,
   License along with the GNU C Library; if not, write to the Free

実行結果
$ grep -A 2 Library /usr/include/stdio.h
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
--
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

実行結果
$ grep -B 2 Library /usr/include/stdio.h
/* Define ISO C stdio on top of C++ iostreams.
   Copyright (C) 1991, 1994-2010, 2011 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
--
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
--

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free

実行結果
$ grep -C 2 Library /usr/include/stdio.h
/* Define ISO C stdio on top of C++ iostreams.
   Copyright (C) 1991, 1994-2010, 2011 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

実行結果
$ grep -e -1 /usr/include/stdio.h
   02111-1307 USA.  */
   Some things throughout the library rely on this being -1.  */
# define EOF (-1)
   null terminator), or -1 on error or EOF.

実行結果
$ cat grep_ptn.txt                  
Library
GNU

$ grep -f grep_ptn.txt /usr/include/stdio.h
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   The GNU C Library is distributed in the hope that it will be useful,
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
# ifdef __GNUC__
#ifdef __USE_GNU
#ifdef __USE_GNU
#ifdef    __USE_GNU
#ifdef __USE_GNU
#if defined __USE_ISOC99 && !defined __USE_GNU \
   GNU extension which conflicts with valid %a followed by letter
# if !defined __USE_GNU \
   GNU extension which conflicts with valid %a followed by letter
#ifdef __USE_GNU
#ifdef __USE_GNU
#ifdef    __USE_GNU
#endif /* Use GNU.  */
#if defined __USE_XOPEN && !defined __USE_XOPEN2K && !defined __USE_GNU
   follow.  In GNU mode we don't do this nonsense.  */
#endif    /* X/Open, but not issue 6 and not for GNU.  */

実行結果
$ grep -H Library /usr/include/stdio.h
/usr/include/stdio.h:   This file is part of the GNU C Library.
/usr/include/stdio.h:   The GNU C Library is free software; you can redistribute it and/or
/usr/include/stdio.h:   The GNU C Library is distributed in the hope that it will be useful,
/usr/include/stdio.h:   License along with the GNU C Library; if not, write to the Free

実行結果
$ grep -i library /usr/include/stdio.h
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   The GNU C Library is distributed in the hope that it will be useful,
   License along with the GNU C Library; if not, write to the Free
   Some things throughout the library rely on this being -1.  */

実行結果
$ grep -L stdlib /usr/include/stdio.h /usr/include/stdlib.h
/usr/include/stdio.h

実行結果
$ grep -l stdlib /usr/include/stdio.h /usr/include/stdlib.h
/usr/include/stdlib.h

実行結果
$ grep -n Library /usr/include/stdio.h
3:   This file is part of the GNU C Library.
5:   The GNU C Library is free software; you can redistribute it and/or
10:   The GNU C Library is distributed in the hope that it will be useful,
16:   License along with the GNU C Library; if not, write to the Free

実行結果
$ grep -n -o Library /usr/include/stdio.h
3:Library
5:Library
10:Library
16:Library

実行結果
$ grep -R -l Library /usr/include/net
/usr/include/net/if.h
/usr/include/net/ethernet.h
/usr/include/net/route.h
/usr/include/net/if_slip.h
/usr/include/net/if_arp.h
/usr/include/net/if_packet.h
/usr/include/net/if_shaper.h

実行結果
$ grep Library /usr/include/stdio.hh
grep: /usr/include/stdio.hh: No such file or directory

$ grep -s Library /usr/include/stdio.hh

実行結果
$ grep -n -v Library /usr/include/stdio.h
1:/* Define ISO C stdio on top of C++ iostreams.
2:   Copyright (C) 1991, 1994-2010, 2011 Free Software Foundation, Inc.
4:
6:   modify it under the terms of the GNU Lesser General Public
7:   License as published by the Free Software Foundation; either
8:   version 2.1 of the License, or (at your option) any later version.
9:
11:   but WITHOUT ANY WARRANTY; without even the implied warranty of
12:   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13:   Lesser General Public License for more details.
14:
15:   You should have received a copy of the GNU Lesser General Public
17:   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18:   02111-1307 USA.  */
     :
     :

実行結果
$ grep -w stdio /usr/include/stdio.h
/* Define ISO C stdio on top of C++ iostreams.
 *    ISO C99 Standard: 7.19 Input/output    <stdio.h>
# include <bits/stdio.h>
# include <bits/stdio-ldbl.h>
#endif /* <stdio.h> included.  */

$ grep stdio /usr/include/stdio.h
/* Define ISO C stdio on top of C++ iostreams.
 *    ISO C99 Standard: 7.19 Input/output    <stdio.h>
#include <bits/stdio_lim.h>
# include <bits/stdio.h>
# include <bits/stdio2.h>
# include <bits/stdio-ldbl.h>
#endif /* <stdio.h> included.  */

実行結果
$ grep -n -x "#endif" /usr/include/stdio.h
55:#endif
86:#endif
106:#endif
114:#endif
118:#endif
129:#endif
136:#endif
147:#endif
153:#endif
188:#endif
203:#endif
207:#endif
217:#endif
230:#endif
     :
     :

Copyright(C) 2006-2014 CLAYPOT. All Rights Reserved.