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?

Day4:grepと正規表現(検索力を身につける)

0
Posted at

LPIC-1 100日チャレンジ Day4

grepと正規表現(検索力を身につける)


🎯 今日のゴール

  • grepの基本的な使い方を理解する
  • よく使うオプションを覚える
  • 正規表現の基礎を理解する

📌 grepとは?

grepはファイル内から「特定の文字列」を検索するコマンド。


grep 検索文字列 ファイル名

例:


grep root /etc/passwd

→ /etc/passwd の中から "root" を含む行を表示


📌 よく使うオプション

オプション 内容
-i 大文字小文字を区別しない
-v 含まない行を表示
-n 行番号表示
-r ディレクトリ内を再帰検索

例:


grep -i root /etc/passwd
grep -n root /etc/passwd


📌 正規表現(基礎)

grepは正規表現が使える。

🔹 メタ文字

記号 意味
^ 行頭
$ 行末
. 任意の1文字
* 直前の文字の0回以上繰り返し

🔹 例

行頭がroot


grep ^root /etc/passwd

行末がbash


grep bash$ /etc/passwd

rで始まる行


grep ^r /etc/passwd


📌 拡張正規表現(-E)


grep -E "root|daemon" /etc/passwd

→ root または daemon を検索


💻 今日の実践


grep root /etc/passwd
grep -n bash /etc/passwd
grep ^r /etc/passwd
grep bash$ /etc/passwd


🧠 試験で狙われるポイント

  • -i と -v の違い
  • ^ と $ の意味
  • grep -r の用途
  • OR検索は -E

🧪 Day4 確認テスト

【選択問題】

Q1

大文字小文字を区別しないオプションは?

A. -v
B. -n
C. -i
D. -r


Q2

行末を表す正規表現は?

A. ^
B. *
C. $
D. .


Q3

ディレクトリ内を再帰的に検索するオプションは?

A. -r
B. -a
C. -f
D. -l


【記述問題】

Q4

^ と $ の違いを説明せよ。



✅ 解答

Q1:C
Q2:C
Q3:A

Q4例:
^ は行頭を表す。
$ は行末を表す。


🔥 明日のテーマ

リダイレクトとパイプ(>, >>, |)

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?