日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

linux中grep命令使用實戰詳解_Linux

作者:沐風筆記 ? 更新時間: 2023-05-21 編程語言

一. grep命令介紹

Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。

grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。

英文注解:

grep ['grep] 搜索目標行命令· global [?glo?bl] 全球的,球狀的 regular 美 [?r?ɡj?l?] 有規律的,規則的, 正規軍(n) expression 美 [?k?spr???n] 表達,表現,表情,臉色,態度

例句: It's enough to make you wet yourself, if you'll pardon the expression

linux支持三種形式的grep命令: grep , egrep ,grep -E

二. 語法格式及常用選項

依據慣例,我們還是先查看幫助,使用grep --help

[root@mufeng test]# grep --help
用法: grep [選項]... PATTERN [FILE]...
在每個 FILE 或是標準輸入中查找 PATTERN。
默認的 PATTERN 是一個基本正則表達式(縮寫為 BRE)。
例如: grep -i 'hello world' menu.h main.c

正則表達式選擇與解釋:
  -E, --extended-regexp     PATTERN 是一個可擴展的正則表達式(縮寫為 ERE)
  -F, --fixed-strings       PATTERN 是一組由斷行符分隔的定長字符串。
  -G, --basic-regexp        PATTERN 是一個基本正則表達式(縮寫為 BRE)
  -P, --perl-regexp         PATTERN 是一個 Perl 正則表達式
  -e, --regexp=PATTERN      用 PATTERN 來進行匹配操作
  -f, --file=FILE           從 FILE 中取得 PATTERN
  -i, --ignore-case         忽略大小寫
  -w, --word-regexp         強制 PATTERN 僅完全匹配字詞
  -x, --line-regexp         強制 PATTERN 僅完全匹配一行
  -z, --null-data           一個 0 字節的數據行,但不是空行

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

輸出控制:
  -m, --max-count=NUM       NUM 次匹配后停止
  -b, --byte-offset         輸出的同時打印字節偏移
  -n, --line-number         輸出的同時打印行號
      --line-buffered       每行輸出清空
  -H, --with-filename       為每一匹配項打印文件名
  -h, --no-filename         輸出時不顯示文件名前綴
      --label=LABEL         將LABEL 作為標準輸入文件名前綴
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

文件控制:
  -B, --before-context=NUM  打印以文本起始的NUM 行
  -A, --after-context=NUM   打印以文本結尾的NUM 行
  -C, --context=NUM         打印輸出文本NUM 行
  -NUM                      same as --context=NUM
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)



為了更直觀一些,我們把常用的參數用表格來展示:

參數 描述
-i 忽略大小寫
-E 啟用POSTIX擴展正則表達式
-P 啟用perl正則
-o 只輸出正則表達式的匹配的內容
-w 整字匹配
-v 取反,也就是不匹配的
-n 輸出行號

有了具體的參數之后,我們再來看實戰案例:

三. 參考案例

3.1 搜索文件中以root開頭的文件

以root開頭的文件,可以用 ^root 比如查看/etc/passwd 中以root開頭的文件,操作如下:

[root@mufenggrow ~]# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash

3.2 搜索文件中出現的root

搜某個單詞,我們直接在grep后面跟上單詞名字即可:

案例一: 搜索/etc/passwd中的root用戶

[root@mufenggrow ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@mufenggrow ~]# 

案例二: 從多個文件中搜索root

root@mufenggrow ~]# echo root >> a.txt
[root@mufenggrow ~]# echo root >> b.txt
[root@mufenggrow ~]# grep "root" /etc/passwd  a.txt b.txt
/etc/passwd:root:x:0:0:root:/root:/bin/bash
/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin
a.txt:root
b.txt:root
[root@mufenggrow ~]# 

3.3 搜索除了匹配行之外的行

此處使用-v 參數,比如取反

案例一: 統計文件的行數且不包含空行

空行的表示方法: ^$

[root@mufenggrow ~]# cp /etc/passwd ./
## 源文件一共35行
[root@mufenggrow ~]# cat /etc/passwd |wc -l
35
## 追加空行進去
[root@mufenggrow ~]# echo "" >> /etc/passwd
[root@mufenggrow ~]# cat /etc/passwd |wc -l
36
## 去掉空行測試
[root@mufenggrow ~]# grep -v ^$  /etc/passwd |wc -l
35
[root@mufenggrow ~]# 

有時候我們修改了配置文件,文件中包含大量的# ,我們想去掉#查看內容,就可以使用

[root@mufenggrow ~]# grep -v ^# passwd |wc -l
35

3.4 匹配的部分使用顏色顯示

這里可以使用 --color=auto,我們來查看一下包含root的行,并高亮顯示要查找的root。

[root@mufenggrow ~]# grep root /etc/passwd  --color=auto
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@mufenggrow ~]# 

這樣顯示,效果不明顯,我們看下圖:

以看到,所有的root都是紅色表示的。

3.5 只輸出文件中匹配到的地方

比如我們要查詢root,但我不想顯示包含root的行,而是只顯示要查詢的內容:

此時需要使用 -o 參數,代碼如下

[root@mufenggrow ~]# grep -o root /etc/passwd
root
root
root
root

要注意,如果一行中有10個root,這里就顯示10個,而不是只顯示一個,所以3.4的案例中我們查詢的時候,包含root的有兩行,但有4個root,在3.5案例中,顯示了所有的root。

3.6 輸出包含匹配字符串的行,并顯示所在的行數

此處可以使用-n 參數, -n 會在一行的前面加上 行號: 比如“4:”

我們來看下代碼示例:

[root@mufenggrow ~]# grep -n "root" passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

我們要統計一個文件一共有多少行,也可以使用-n 參數

root@mufenggrow ~]# grep -n "" passwd |awk -F : '{print $1}' |tail -n 1
35

3.7 統計文件或者文本中包含匹配字符串的行數

此時可以用-c參數:

[root@mufenggrow ~]# grep -c "root" passwd
2

包含root的有兩行, 如果我們要統計文本的行數:

[root@mufenggrow ~]# grep -c "$" passwd
35

相當于查找 $的行數,可以看到一共有35個$符號,也就是35行。

總結

grep命令在日常工作中,應用的比較廣泛,一定要認真學習,記熟記牢常用參數。

原文鏈接:https://blog.51cto.com/zmedu/6063987

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新