国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Linux Shell 腳本編程學(xué)習(xí)之【第3章 正則表達(dá)式 (第二部分) grep命令】

這篇具有很好參考價(jià)值的文章主要介紹了Linux Shell 腳本編程學(xué)習(xí)之【第3章 正則表達(dá)式 (第二部分) grep命令】。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

4 grep命令

1、文本搜索工具
2、GREP 是Global search Regular Expression and Print out the line的簡(jiǎn)稱(chēng),即全面搜索正則表達(dá)式并把行打印出來(lái)。
3、grep命令的模式十分靈活,可以是字符串,也可以是變量,還可以是正則表達(dá)式。模式中包含空格,則必須用雙引號(hào)括起來(lái)。

4.1 基本用法

選 項(xiàng) 意 義
-c 只輸出匹配行的數(shù)量
-i 搜索時(shí)忽略大小寫(xiě)
-h 查詢(xún)多文件時(shí)不顯示文件名
-I 只列出符合匹配的文件名,而不列出具體的匹配行
-n 列出所有的匹配行,并顯示行號(hào)
-s 不顯示不存在或無(wú)匹配文本的錯(cuò)誤信息
-v 顯示不包含匹配文本的所有行
-w 匹配整詞
-x 匹配整行
-r 遞歸搜索,不僅搜索當(dāng)前工作目錄,而且搜索子目錄
-q 禁止輸出任何結(jié)果,以退出狀態(tài)表示搜索是否成功
-b 打印匹配行距文件頭部的偏移量,以字節(jié)為單位
-0 與-b選項(xiàng)結(jié)合使用,打印匹配的詞距文件頭部的偏移量,以字節(jié)為單位
-E 支持?jǐn)U展的正則表達(dá)式
-F 不支持正則表達(dá)式,按照字符串的字面意思進(jìn)行匹配

4.2 參考命令

命令模式指的是將命令封裝成一個(gè)整體。

4.2.1 雙引號(hào)

// 含空格時(shí)使用雙引號(hào)
[rhel@localhost ~]$ grep -i "user for" /etc/passwd
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin

4.2.2 -c 輸出匹配行數(shù)

// -c輸出匹配的行數(shù)

[rhel@localhost ~]$ grep -c -i "user for" /etc/passwd
3

// -n 列出匹配項(xiàng)并加上行號(hào)。
[rhel@localhost ~]$ grep -n -i "user for" /etc/passwd
16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
19:colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
27:geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin

4.2.3 -h 或 -l 不顯示或只顯示文件名

// -h 顯示文件名 -l 只列出符合規(guī)范的文件名
[rhel@localhost etc]$ grep -i home passwd*
passwd:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
passwd:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
passwd:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
passwd:db2as:x:1003:903::/home/db2as:/bin/bash
passwd-:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
passwd-:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
passwd-:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
[rhel@localhost etc]$ grep -h home passwd*
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
db2inst1:x:1001:901::/home/db2inst1:/bin/bash
db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
db2as:x:1003:903::/home/db2as:/bin/bash
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
db2inst1:x:1001:901::/home/db2inst1:/bin/bash
db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
[rhel@localhost etc]$ grep -l home passwd*
passwd
passwd-

4.2.4 -s 不顯示錯(cuò)誤信息

[rhel@localhost etc]$ grep user else passwd
grep: else: 沒(méi)有那個(gè)文件或目錄
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd:radvd:x:75:75:radvd user:/:/sbin/nologin
passwd:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
passwd:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
passwd:qemu:x:107:107:qemu user:/:/sbin/nologin
passwd:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

[rhel@localhost etc]$ grep -s user else passwd
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd:radvd:x:75:75:radvd user:/:/sbin/nologin
passwd:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
passwd:usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
passwd:qemu:x:107:107:qemu user:/:/sbin/nologin
passwd:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

4.2.5 -r 遞歸顯示本級(jí)目錄及下級(jí)目錄

[root@localhost etc]# grep  auth pa*
grep: pam.d: 是一個(gè)目錄
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd-:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[root@localhost etc]# grep -s auth pa*
passwd:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
passwd-:saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[root@localhost etc]# grep -r auth pa*
pam.d/config-util:auth          sufficient      pam_rootok.so
pam.d/config-util:auth          sufficient      pam_timestamp.so
pam.d/config-util:auth          include         system-auth
pam.d/config-util:session               optional        pam_xauth.so

4.2.6 -w 匹配完整詞 -x 匹配完整行

[root@localhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin
[root@localhost etc]# grep -w tcpd* passwd
[root@localhost etc]# 
[root@localhost etc]# grep tcpd* passwd
tcpdump:x:72:72::/:/sbin/nologin

[root@localhost etc]# grep -x tcpdump passwd
[root@localhost etc]# grep -x tcpdump:x:72:72::/:/sbin/nologin passwd
tcpdump:x:72:72::/:/sbin/nologin

4.2.7 -q 退出狀態(tài)

grep命令后一旦加上-q選項(xiàng), grep將不再輸出任何結(jié)果,而是以退出狀態(tài)表示搜索是否成功,退出狀態(tài)0表示搜索成功,退出狀態(tài)1表示未搜索到滿(mǎn)足模式的文本行,退出狀態(tài)2表示命令或程序由于錯(cuò)誤而未能執(zhí)行。

[root@localhost etc]# grep -q -x tcpdump:x:72:72::/:/sbin/nologin passwd
[root@localhost etc]# echo $?
0           --退出狀態(tài)0
[root@localhost etc]# grep -q -x tcpdump passwd
[root@localhost etc]# echo $?
1           --退出狀態(tài)1
[root@localhost etc]# grep -q -s -x tcpdump passwdmmm
[root@localhost etc]# echo $?
2           --退出狀態(tài)2

4.2.8 -b 距文件頭部的偏移量

grep-b 選項(xiàng)打印匹配行距文件頭部的偏移量,以字節(jié)為單位。
如果在-b 選項(xiàng)后再加上-o 選項(xiàng) ,grep命令將打印匹配的詞距文件頭部的偏移量。

[root@localhost etc]# grep -b bash passwd
0:root:x:0:0:root:/root:/bin/bash
2016:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
2059:db2inst1:x:1001:901::/home/db2inst1:/bin/bash
2105:db2fenc1:x:1002:902::/home/db2fenc1:/bin/bash
2151:db2as:x:1003:903::/home/db2as:/bin/bash
[root@localhost etc]# grep -b -o bash passwd
27:bash
2054:bash
2100:bash
2146:bash
2186:bash
[root@localhost etc]# 

4.2.9 -E -F

grep命令的-E 和-F 選項(xiàng)分別等價(jià)于grep命令族中的egrep和 fgrep命令

4.3 grep與正則表達(dá)式

4.3.1 匹配行首

// 匹配以n開(kāi)頭的行首
[root@localhost etc]# grep ^n passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@localhost etc]# 

4.3.2 查找空白行(^)

// 統(tǒng)計(jì)空白行的數(shù)量
[root@localhost etc]# grep -c ^$ profile
11

// 統(tǒng)計(jì)非空白行行數(shù)
[root@localhost etc]# grep -c ^[^$] profile
65

4.3.3 設(shè)置大小寫(xiě)(-n)

[root@localhost etc]# grep -i RHEL passwd
rhel:x:1000:1000:rhel:/home/rhel:/bin/bash
[root@localhost etc]# grep -n [Rr]hel passwd
40:rhel:x:1000:1000:rhel:/home/rhel:/bin/bash

4.3.4 轉(zhuǎn)義符(\)

[root@localhost etc]# grep cj\.name makedumpfile.conf.sample
##              erase cj.name

[root@localhost rhel]# grep '\-\{5\}' 1.sh
-----dddfsdf
-----sdfsdfasdfaew

4.3.5 POSIX字符

為了保持不同國(guó)家的字符編碼的一致性, POSIX(Portable Operating System Interface)增 加了特殊的字符類(lèi),以[:classname]的格式給出, grep 命令支持POSIX 字符類(lèi)

類(lèi) 名 意 義
[:upper:] 表示大寫(xiě)字母[A~Z]
[:lower:] 表示小寫(xiě)字母[a~z]
[:digit:] 表示阿拉伯?dāng)?shù)字[0~9]
[:alnum:] 表示大小寫(xiě)字母和阿拉伯?dāng)?shù)字[0~9a~zA~Z]
[:space:] 表示空格或Tab鍵
[:alpha:] 表示大小寫(xiě)字母[a~zA~Z]
[:cntrl:] 表示Ctrl鍵
[:graph:]或[:print:] 表示ASCIⅡI碼33~126之間的字符
[:xdigit:] 表示16進(jìn)制數(shù)字[0~9 A~Fa~f]

//利用POSIX 字符類(lèi)搜索以大寫(xiě)字母開(kāi)頭的行
[root@zawu  globus]#  grep  ^[[:upper:]]  00.pem
This is a Certificate Request file:
It   should   be   mailed   to   xd  nidseu edu cn
Certificate Subject:         _            .      .
The above string is known as your user certificate subject, and it To install this user certificate, please save this e-mail message If you have any questions about the certificate contactMIIB4ZCCAUwCAQAwcTENMAsGA1UEChMER3JpZDETMBEGA1UECxMKR2xvYnVzVGCxMKc2V1LmVkdS5jbjEPMAOGA1UEAxMGZ2XvYnVZMIGfMAOGCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCw50H88r7sAGjQGLZTmMxTiw9AgDqppBMhP6Fg3eJTqBvqBdhaYTRtleSBT/AJUi3rTDRIABJPgu8cZkwb1AE8uEJSeCKwgk3J9QHK2NcZXwIDAQABBAIWADANBgkqhkiG9wOBAQQFAAOBgQBoHRUaaB/Tyu+LuALwnT3Muw/OjDIYxc5aYaA4dwCB6/2yVYyfmyRCNox3rIsyUvqL9p81d/hpNiAB/0OazMBialq5Gcpaansd
[rootezawu   globus]#

//搜索以空格開(kāi)頭文本行
[rootazawu  globus]#  grep  ^[[:space:]]  00.pem
/0=Grid/OU=GlobusTest/OU=simpleCA-seugrid1 .seu .edu .cn/OU=seu .edu .cn/CN=globus
You need not edit this message in any way . Simply
save  this  e-mail  message  to  the  file.

4.3.6 精確匹配

[root@localhost rhel]# grep the 1.sh
Linel:there   are   four   lines   in   this   file 
Line2:this   the   line   2
Line3:this    is    another    line
[root@localhost rhel]# grep "\<the\>" 1.sh 
Line2:this   the   line   2

[root@localhost rhel]# grep -w the 1.sh
Line2:this   the   line   2

4.3.7 或字符

字符“ | ”是擴(kuò)展的正則表達(dá)式中定義的, grep需要加上-E 選項(xiàng)才能支持它,下面給出grep命令使用“ | ”字符的例子。

[root@localhost rhel]# grep -E "there | another " 1.sh
Linel:there   are   four   lines   in   this   file 
Line3:this    is    another    line

4.4 grep 命令族簡(jiǎn)介

Linux 系統(tǒng)支持三種形式的grep命令,通常將這三種形式的 grep命令稱(chēng)為 grep命令族, 這三種形式具體為:
● grep: 標(biāo)準(zhǔn)grep 命令,支持基本正則表達(dá)式,上面兩小節(jié)已經(jīng)對(duì)此命令進(jìn)行了詳細(xì) 的討論。
● egrep: 擴(kuò)展grep 命令,支持基本和擴(kuò)展正則表達(dá)式。
● fgrep: 快速grep 命令,不支持正則表達(dá)式,按照字符串的字面意思進(jìn)行匹配。
egrep 命令與 grep -E 等價(jià), fgrep 命令與 grep -F 等價(jià),在某些Linux 發(fā)行版中, egrep 和fgrep都是grep命令的別名,分別將其符號(hào)鏈接到grep-E 和 grep-F 命令。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-600920.html

[root@localhost rhel]# egrep  "there | another " 1.sh
Linel:there   are   four   lines   in   this   file 
Line3:this    is    another    line
[root@localhost rhel]# fgrep another 1.sh
Line3:this    is    another    line

到了這里,關(guān)于Linux Shell 腳本編程學(xué)習(xí)之【第3章 正則表達(dá)式 (第二部分) grep命令】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Linux——Shell腳本編程(1)

    Linux——Shell腳本編程(1)

    1)Linux運(yùn)維工程師在進(jìn)行服務(wù)器集群管理時(shí),需要編寫(xiě)Shell程序來(lái)進(jìn)行服務(wù)器管理。 2)對(duì)于 JavaEE 和 Python 程序員來(lái)說(shuō),工作的需要,要求你編寫(xiě)一些 Shell腳本進(jìn)行程序或者是服務(wù)器的維護(hù),比如編寫(xiě)一個(gè)定時(shí)備份數(shù)據(jù)庫(kù)的腳本。 3) 對(duì)于大數(shù)據(jù)程序員來(lái)說(shuō),需要編寫(xiě)Shell程序來(lái)管

    2024年02月09日
    瀏覽(44)
  • Linux_5_Shell腳本編程

    Linux_5_Shell腳本編程

    程序:算法+數(shù)據(jù)結(jié)構(gòu) 數(shù)據(jù):是程序的核心 算法:處理數(shù)據(jù)的方式 數(shù)據(jù)結(jié)構(gòu): 數(shù)據(jù)在計(jì)算機(jī)中的類(lèi)型和組織方式 面向過(guò)程語(yǔ)言 做一件事情,排出個(gè)步驟,第一步干什么,第二步干什么,如果出現(xiàn)情況A,做什么處理,如果出現(xiàn)了情況B,做什么處理 問(wèn)題規(guī)模小,可以步驟化,按部

    2024年02月13日
    瀏覽(19)
  • Linux實(shí)驗(yàn)4 shell腳本編程基礎(chǔ)

    Linux實(shí)驗(yàn)4 shell腳本編程基礎(chǔ)

    1.假設(shè)在/tmp下有以當(dāng)前用戶(hù)的帳號(hào)命名的目錄,請(qǐng)?jiān)诿钚兄信R時(shí)修改環(huán)境變量PATH的值,要求該目錄的路徑附加到該變量的最后。 2.請(qǐng)?jiān)诿钚兄信R時(shí)設(shè)置命令輸入提示行格式為:“當(dāng)前系統(tǒng)時(shí)間-用戶(hù)#”。 3.在命令行定義一個(gè)字符串變量str,并且賦值為“test for shell”,然

    2024年04月17日
    瀏覽(20)
  • Linux系統(tǒng)Shell腳本編程之條件語(yǔ)句

    Linux系統(tǒng)Shell腳本編程之條件語(yǔ)句

    Shell 環(huán)境根據(jù)命令執(zhí)行后的返回狀態(tài)值 \\\" $? \\\" 來(lái)判斷是否執(zhí)行成功,當(dāng)返回值為0時(shí)表示成功,否則表示失敗或異常(非0值)。 使用專(zhuān)門(mén)的測(cè)試工具 test 命令,可以對(duì)特定條件進(jìn)行測(cè)試,并根據(jù)返回值(值為0)來(lái)判斷是否成立。 test命令格式 文件測(cè)試指的是根據(jù)給定的路徑名

    2024年01月25日
    瀏覽(23)
  • 【Linux命令行與Shell腳本編程】第十六章 Shell函數(shù)

    【Linux命令行與Shell腳本編程】第十六章 Shell函數(shù)

    腳本函數(shù)基礎(chǔ) 函數(shù)返回值 在函數(shù)中使用變量 數(shù)組變量和函數(shù) 函數(shù)遞歸 創(chuàng)建庫(kù) 在命令行中使用函數(shù) 可以將shell腳本代碼放入函數(shù)中封裝起來(lái),這樣就能在腳本的任意位置多次使用. 函數(shù)是一個(gè)腳本代碼塊,可以并在腳本中的任何位置重用它。當(dāng)需要在腳本中使用該代碼塊時(shí)

    2024年02月14日
    瀏覽(39)
  • 3.7 Linux shell腳本編程(分支語(yǔ)句、循環(huán)語(yǔ)句)

    3.7 Linux shell腳本編程(分支語(yǔ)句、循環(huán)語(yǔ)句)

    目錄 分支語(yǔ)句(對(duì)標(biāo)C語(yǔ)言中的if) 多路分支語(yǔ)句(對(duì)標(biāo)C語(yǔ)言中的swich case) 分支語(yǔ)句(對(duì)標(biāo)C語(yǔ)言中的if) 語(yǔ)法結(jié)構(gòu): ? ? ? ? ?if ? ?表達(dá)式 ? ? ? ? ??? ??? ?then ?命令表 ? ? ? ? ?fi ? ? 如果表達(dá)式為真, 則執(zhí)行命令表中的命令; 否則退出if語(yǔ)句, 即執(zhí)行fi后面的語(yǔ)句。

    2024年02月02日
    瀏覽(24)
  • Linux shell編程學(xué)習(xí)筆記29:shell自帶的 腳本調(diào)試 選項(xiàng)

    Linux shell編程學(xué)習(xí)筆記29:shell自帶的 腳本調(diào)試 選項(xiàng)

    Linux shell腳本的調(diào)試方法比較多,上次我們探討和測(cè)試了shell內(nèi)建命令set所提供的一些調(diào)試選項(xiàng),其實(shí) shell 本身也提供了一些調(diào)試選項(xiàng)。我們以bash為例來(lái)看看。 purleEndurer @ csdn ~ $ bash --help GNU bash, version 4.2.46(2)-release-(x86_64-redhat-linux-gnu) Usage: ?bash [GNU long option] [option] ... ? ? ?

    2024年02月04日
    瀏覽(16)
  • 【Linux命令行與Shell腳本編程】第十四章,呈現(xiàn)數(shù)據(jù)

    【Linux命令行與Shell腳本編程】第十四章,呈現(xiàn)數(shù)據(jù)

    第十四章 呈現(xiàn)數(shù)據(jù) 1.1,標(biāo)準(zhǔn)文件描述符 文件描述符 縮寫(xiě) 描述 0 STDIN 標(biāo)準(zhǔn)輸入 1 STDOUT 標(biāo)準(zhǔn)輸出 2 STDERR 標(biāo)準(zhǔn)錯(cuò)誤 1.1.1,STDIN 標(biāo)準(zhǔn)輸入 1.1.2,STDOUT 標(biāo)準(zhǔn)輸出 1.1.3,STDERR 錯(cuò)誤輸出 1.2,重定向錯(cuò)誤 1.2.1.只重定向錯(cuò)誤 n 1.2.2.重定向錯(cuò)誤消息和正常輸出 2.1,臨時(shí)重定向 2.2,永久重定向 exec 4

    2023年04月25日
    瀏覽(25)
  • linux|shell編程|shell腳本內(nèi)的加減乘除運(yùn)算實(shí)現(xiàn)示例

    shell腳本內(nèi)的加減乘除是由于在編寫(xiě)kubernetes巡檢腳本的時(shí)候,某些部分需要做一點(diǎn)簡(jiǎn)單的運(yùn)算,突然發(fā)現(xiàn)我其實(shí)對(duì)這些不太熟悉。 因此,查閱了一些資料,現(xiàn)在就加減乘除運(yùn)算在shell腳本內(nèi)如何應(yīng)用做一個(gè)簡(jiǎn)單的總結(jié),寫(xiě)的不對(duì)的地方請(qǐng)各位輕點(diǎn)噴 首先,我們看一個(gè)錯(cuò)誤的示

    2024年02月17日
    瀏覽(18)
  • Linux shell編程學(xué)習(xí)筆記14:編寫(xiě)和運(yùn)行第一個(gè)shell腳本hello world!

    Linux shell編程學(xué)習(xí)筆記14:編寫(xiě)和運(yùn)行第一個(gè)shell腳本hello world!

    ?* 20231020?寫(xiě)這篇博文斷斷續(xù)續(xù)花了好幾天,為了說(shuō)明不同shell在執(zhí)行同一腳本文件時(shí)的差別,我分別在csdn提供線(xiàn)上Linux環(huán)境 (使用的shell是zsh)和自己的電腦上(使用的shell是bash)做測(cè)試。功夫不負(fù)有心人,在其中一些實(shí)例中可以體現(xiàn)出zsh和bash的對(duì)腳本文件支持的差別,收

    2024年02月07日
    瀏覽(28)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包