0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Linux][command] テキスト処理_検索_grep, egrep, fgrep, cut

Last updated at Posted at 2025-04-11

行の検索

grepコマンド

$ grep [オプション] 検索パターン [ファイル名]
$ grep [オプション] [-f ファイル名] [ファイル名]
オプション 由来 説明
default global regular expression print 正規表現のパターンにマッチする行を表示する
-v invert match マッチしなかった行を表示する
-c count マッチした行の行数を表示する
-n number マッチした行を表示する
マッチした行の行番号を表示する
-f file ファイルから読み込んで検索する(1行1パターン)
-i ignore case 大文字・小文字を区別せずに検索する
-r
-R
recursive サブディレクトリ内も再帰的に検索する
-E extended 拡張正規表現を使用する(egrepと同等)
-F fixed 正規表現ではなく固定文字列として検索する(fgrepと同等)

egrepコマンド

  • egrep(Extended GREP)は、拡張正規表現(ERE: Extended Regular Expressions)を使って検索するコマンド
  • grep -E と同等
$ egrep [オプション] 検索パターン [ファイル名]

fgrepコマンド

  • fgrep(Fixed-string GREP)は、特殊な正規表現を使用せずに固定文字列を検索するコマンド
  • grep -F と同等
$ fgrep [オプション] 検索パターン [ファイル名]

使用例

grepコマンドの場合

"a" と "b" の間に任意の1文字を含む行を検索

$ grep "a.b" file.txt

egrepコマンドの場合

"a" または "b" を含む行を検索(正規表現の | を使用)

$ egrep "a|b" file.txt

fgrepコマンドの場合

"a.b" という文字列そのままを検索

$ fgrep "a.b" file.txt

特定のフィールド・文字位置の検索

cutコマンド

# cut [オプション] [ファイル名]
オプション 由来 説明
default cut デフォルトでは何も出力されない
-c LIST characters 指定した文字位置の文字を抽出する
例:cut -c 3-7 file.txt(3〜7文字目を抽出)
-f LIST fields 指定したフィールド番号を抽出する(デフォルト区切りはタブ)
例:cut -f 1,3 file.txt(1列目と3列目を抽出)
-d CHAR delimiter 区切り文字を指定する(-f と組み合わせて使用)
例:cut -d "," -f 2 file.csv(CSVの2列目を抽出)

Ping-t

grep

egrep

fgrep

cut

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?