柚子快報邀請碼778899分享:php 數(shù)據(jù)庫 正則表達(dá)式
柚子快報邀請碼778899分享:php 數(shù)據(jù)庫 正則表達(dá)式
一:正則表達(dá)式grep
-a 不要忽略二進(jìn)制數(shù)據(jù)。
-A<顯示列數(shù)> 除了顯示符合范本樣式的那一行之外,并顯示該行之后的內(nèi)容。
-b 在顯示符合范本樣式的那一行之外,并顯示該行之前的內(nèi)容。
-c 計算符合范本樣式的列數(shù)。
-C<顯示列數(shù)>或-<顯示列數(shù)> ?除了顯示符合范本樣式的那一列之外,并顯示該列之前后的內(nèi)容。
-d<進(jìn)行動作> 當(dāng)指定要查找的是目錄而非文件時,必須使用這項參數(shù),否則grep命令將回報信息并停止動作。-e<范本樣式> 指定字符串作為查找文件內(nèi)容的范本樣式。
-E 將范本樣式為延伸的普通表示法來使用,意味著使用能使用擴(kuò)展正則表達(dá)式。
-f<范本文件> 指定范本文件,其內(nèi)容有一個或多個范本樣式,讓grep查找符合范本條件的文件內(nèi)容,格式為每一列的范本樣式。
-F 將范本樣式視為固定字符串的列表。
-G 將范本樣式視為普通的表示法來使用。
-h 在顯示符合范本樣式的那一列之前,不標(biāo)示該列所屬的文件名稱。
-H 在顯示符合范本樣式的那一列之前,標(biāo)示該列的文件名稱。
-i 忽略字符大小寫的差別。
-l 列出文件內(nèi)容符合指定的范本樣式的文件名稱。
-L 列出文件內(nèi)容不符合指定的范本樣式的文件名稱。
-n 在顯示符合范本樣式的那一列之前,標(biāo)示出該列的編號。
-q 不顯示任何信息。
-R/-r 此參數(shù)的效果和指定“-d recurse”參數(shù)相同。
-s 不顯示錯誤信息。
-v 反轉(zhuǎn)查找。
-w?只顯示全字符合的列。
-x 只顯示全列符合的列。
-y 此參數(shù)效果跟“-i”相同。
-o 只輸出文件中匹配到的部分。
1:正則表達(dá)式概念
正則表達(dá)式是使用單個字符串來描述、匹配一系列符合某個句法規(guī)則的字符串,簡單來說,
是一種匹配字符串的方法,通過一些特殊符號,實現(xiàn)快速查找、刪除、替換某個特定字符串。
2:基礎(chǔ)正則表達(dá)式
基礎(chǔ)正則表達(dá)式是常用正則表達(dá)式最基礎(chǔ)的部分。在 Linux 系統(tǒng)中常見的文件
處理工具中 grep 與 sed 支持基礎(chǔ)正則表達(dá)式,而 egrep 與 awk 支持?jǐn)U展正則表達(dá)式。
3:創(chuàng)建測試文件
[root@localhost ~]#?cat test.txt
he was short and fat.
he was weating a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
PI=3.14
a wood cross!
Actions speak louder than words
#woood #
#woooooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
3:查找特定字符
(1)查看包含the的行
[root@localhost ~]# grep -ni 'the' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
注釋:
-n:顯示行號
-i:不區(qū)分大小寫
-v:不包含指定字符
(2)利用[ ]查找集合字符
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:he was weating a blue polo shirt with black pants.
注釋:
[ ]:中括號內(nèi)不管寫幾個字符,都只匹配一個,表示匹配其中的任何一個字符
(3)查找字母oo前不是字母w的內(nèi)容
[root@localhost ~]#?grep -n '[^w]oo' test.txt?
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
9:#woood #
10:#woooooooood #
12:I bet this place is really spooky late at night!
(4)查看字母oo前不是小寫字母的內(nèi)容
[root@localhost ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
注釋:字母前直接用^,表示取反
??????[ ??]前用^,表示以括號中的字符開頭
4:查找行首與行位
(1)查看以the為行首的行
[root@localhost ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
(2)查詢以小寫字母開頭的行
[root@localhost ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
2:he was weating a blue polo shirt with black pants.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
7:a wood cross!
(3)查詢以大寫字母開頭的行
[root@localhost ~]#?grep -n '^[A-Z]' test.txt
3:The home of Football on BBC Sport online.
6:PI=3.14
8:Actions speak louder than words
11:AxyzxyzxyzxyzC
12:I bet this place is really spooky late at night!
13:Misfortunes never come alone/single.
14:I shouldn't have lett so tast.
(4)查看以非字母開頭的行
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
9:#woood #
10:#woooooooood #
(5)查看以點結(jié)尾的行
[root@localhost ~]# grep -n '\.$' test.txt
1:he was short and fat.
2:he was weating a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
13:Misfortunes never come alone/single.
14:I shouldn't have lett so tast.
(5)查詢空行
[root@localhost ~]# grep -n '^$' test.txt
注釋:查詢非空行
[root@localhost ~]# grep -n -v '^$' test.txt
5:查找任意字符和重復(fù)字符
(1)查找包含四字符的單詞的行,單詞以w開頭,以d結(jié)尾
[root@localhost ~]#?grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
7:a wood cross!
8:Actions speak louder than words
注釋:
一個點代表一個字符
(2)查詢至少包含兩個字母o(oo)字符串的行
[root@localhost ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
7:a wood cross!
10:#woood #
11:#woooooooood #
13:I bet this place is really spooky late at night!
注釋:
ooo*:前兩個o是條件,表示包含兩個o;然后是o*,表示后面有零個或多個重復(fù)o
(3)查找行,行中單詞包含w開頭和d結(jié)尾,中間至少一個字母o
[root@localhost ~]# grep -n 'woo*d' test.txt
7:a wood cross!
10:#woood #
11:#woooooooood #
(4)查詢以w開頭,d結(jié)尾,中間字符可有可無
[root@localhost ~]# grep -n 'w.*d' test.txt?
1:he was short and fat.
5:google is the best tools for search keyword.
7:a wood cross!
8:Actions speak louder than words
10:#woood #
11:#woooooooood #
(5)查詢包含數(shù)字的行
[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
6:PI=3.14
6:查找連續(xù)字符范圍
(1)查詢包含兩個o的字符
[root@localhost ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
7:a wood cross!
10:#woood #
11:#woooooooood #
13:I bet this place is really spooky late at night!
注釋:
'o\{2\}':表示兩個字母o
(2)w開頭,d結(jié)尾中間有2--5個o
[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
7:a wood cross!
10:#woood #
(3)w開頭,d結(jié)尾中間有2個以上o
[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
7:a wood cross!
10:#woood #
11:#woooooooood #
7:擴(kuò)展正則表達(dá)式
[root@localhost ~]#?grep -v '^$' /etc/ssh/sshd_config | grep -v '^#'
用擴(kuò)展正則表達(dá)式表示:
[root@localhost ~]#?egrep -v '^$|^#' /etc/ssh/sshd_config
二:文本處理器sed
sed(Stream EDitor)是一個強大而簡單的文本解析轉(zhuǎn)換工具,可以讀取文本,并根據(jù)
指定的條件對文本內(nèi)容進(jìn)行編輯(刪除、替換、添加、移動等),最后輸出所有行或者僅輸
出處理的某些行。sed 也可以在無交互的情況下實現(xiàn)相當(dāng)復(fù)雜的文本處理操作,被廣泛應(yīng)用
于 Shell 腳本中,用以完成各種自動化處理任務(wù)。
sed 的工作流程主要包括讀取、執(zhí)行和顯示三個過程。
讀?。簊ed 從輸入流(文件、管道、標(biāo)準(zhǔn)輸入)中讀取一行內(nèi)容并存儲到臨時的緩沖區(qū)中(又稱模式空間,pattern space)。 執(zhí)行:默認(rèn)情況下,所有的 sed 命令都在模式空間中順序地執(zhí)行,除非指定了行的地址,否則 sed 命令將會在所有的行上依次執(zhí)行。 顯示:發(fā)送修改后的內(nèi)容到輸出流。在發(fā)送數(shù)據(jù)后,模式空間將會被清空。
1:sed常用選項
-n :使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN 的數(shù)據(jù)一般都會被列出到終端上。但如果加上 -n 參數(shù)后,則只有經(jīng)過sed 特殊處理的那一行(或者動作)才會被列出來。 -e :直接在命令列模式上進(jìn)行 sed 的動作編輯; -f :直接將 sed 的動作寫在一個文件內(nèi), -f filename 則可以運行 filename 內(nèi)的 sed 動作; -r :sed 的動作支持的是延伸型正規(guī)表示法的語法。(默認(rèn)是基礎(chǔ)正規(guī)表示法語法) -i :直接修改讀取的文件內(nèi)容,而不是輸出到終端。
2:sed常用操作
a :新增行, a 的后面可以是字串,而這些字串會在新的一行出現(xiàn)(目前的下一行) c :取代行, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行 d :刪除行,因為是刪除,所以 d 后面通常不接任何參數(shù),直接刪除地址表示的行; i :插入行, i 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的上一行); p :打印,亦即將某個選擇的數(shù)據(jù)印出。通常 p 會與參數(shù) sed -n 一起運行 s :替換,可以直接進(jìn)行替換的工作,通常這個 s 的動作可以搭配正規(guī)表示法,例如 1,20s/old/new/g 一般是替換符合條件的字符串而不是整行
y:字符轉(zhuǎn)換
3:輸出符合條件的文本
(1)輸出所有內(nèi)容
[root@localhost ~]# sed -n 'p' test.txt
(2)輸出第三行
[root@localhost ~]# sed -n '3p' test.txt
(3)輸出3~5行
[root@localhost ~]# sed -n '3,5p' test.txt
(4)輸出所有奇數(shù)行
[root@localhost ~]#?sed -n 'p;n' test.txt
(5)輸出所有偶數(shù)行
[root@localhost ~]# sed -n 'n;p' test.txt
(6)輸出第1~5行之間的奇數(shù)行
[root@localhost ~]# sed -n '1,5{p;n}' test.txt
(7)輸出第10行至文件尾之間的偶數(shù)行
[root@localhost ~]# sed -n '10,${n;p}' test.txt
注釋:
此命令中,讀取的第一行是文件的第10行,讀取的第二行,是文件的第11行,依次類推
(8)輸出包含the的行
[root@localhost ~]# sed -n '/the/p' test.txt
(9)輸出從第4行開始至第一個包含the的行
[root@localhost ~]#?sed -n ' 4,/the/p' test.txt
(10)輸出包含the的行所在的行號
[root@localhost ~]# sed -n '/the/=' test.txt
注釋:
=用來輸出行號
(11)輸出以PI開頭的行
[root@localhost ~]#?sed -n '/^PI/p' test.txt
(12)輸出包含單詞wood的行
[root@localhost ~]# sed -n '/\
4:刪除符合條件的文本
(1)刪除第3行
[root@localhost ~]# nl test.txt | sed '3d' ##顯示行號
或
[root@localhost ~]# sed '3d' test.txt ##不顯示行號
(2)刪除3~5行
[root@localhost ~]# nl test.txt |sed '3,5d'
(3)刪除包含cross的行
[root@localhost ~]# nl test.txt |sed '/cross/d'
注釋:刪除不包含cross的行
[root@localhost ~]# nl test.txt |sed '/cross/! d'
(4)刪除以小寫字母開頭的行
[root@localhost ~]# sed '/^[a-z]/d' test.txt
(5)刪除以點結(jié)尾的行
[root@localhost ~]# sed '/\.$/d' test.txt
(6)刪除空行
[root@localhost ~]#?sed '/^$/d' test.txt
5:替換符合條件的文本
(1)將每行的第一個the換成THE
[root@localhost ~]#?sed 's/the/THE/' test.txt
(2)將每行中的第2個l換成L
[root@localhost ~]# sed 's/l/L/2' test.txt
(3)將文中所有的the換成THE
[root@localhost ~]# sed 's/the/THE/g' test.txt
(4)將文中所有的o刪除
[root@localhost ~]# sed 's/o//g' test.txt
(5)在每行的行首插入#
[root@localhost ~]# sed 's/^/#/' test.txt?
注釋
在每行行尾添加#
[root@localhost ~]# sed 's/$/#/' test.txt?
(6)在包含the的每行的行首插入#
[root@localhost ~]# sed '/the/s/^/#/' test.txt?
(7)在每行的行尾插入字符串EOF
[root@localhost ~]# sed 's/$/EOF/' test.txt
(8)將第3~5行中的所有the替換成THE
[root@localhost ~]# sed '3,5s/the/THE/g' test.txt
(9)將包含the的所有行中的o都替換成O
[root@localhost ~]#?sed '/the/s/o/O/g' test.txt
5:遷移符合條件的文本
H:復(fù)制到剪切板
g:將剪切板中的內(nèi)容覆蓋到指定行
G:將剪切板中的內(nèi)容追加到指定行
w:保存文件
r:讀取指定文件
a:追加指定內(nèi)容
(1)將包含the的行遷移至文件的末尾
[root@localhost ~]# sed '/the/{H;d};$G' test.txt
(2)將第1~5行的內(nèi)容轉(zhuǎn)移至第17行后
[root@localhost ~]#?sed '1,5{H;d};17G' test.txt
(3)將包含the的行另存為文件out.txt
[root@localhost ~]# sed '/the/w out.txt' test.txt
(4)將文件/etc/hostname的內(nèi)容添加到包含the的每一行后
[root@localhost ~]# sed '/the/r /etc/hostname' test.txt?
(5)在第3行后插入一個新行,內(nèi)容為#chkconfig:35 82 20
[root@localhost ~]#?sed '3a#chkconfig:35 82 20' test.txt
(6)在包含the的每行后插入一個新行,內(nèi)容為New
[root@localhost ~]# sed '/the/aNew' test.txt?
(7)在第3行后插入多行內(nèi)容
[root@localhost ~]# sed '3aNew1\nNew2' test.txt
注釋:\n為換行,添加兩行為New1和New2
三:文本處理器awk
awk 是一個功能強大的編輯工具,逐行讀取輸入文本,并根據(jù)
指定的匹配模式進(jìn)行查找,對符合條件的內(nèi)容進(jìn)行格式化輸出或者過濾處理
1:awk常見用法
$0 ? ? ? ? ? 表示整個當(dāng)前行
$1 ? ? ? ? ? 每行第一個字段
NF ? ? ? ? ?字段數(shù)量變量
NR ? ? ? ? ?每行的記錄號,多文件記錄遞增
FNR ? ? ? ?與NR類似,不過多文件記錄不遞增,每個文件都從1開始
\t ? ? ? ? ? ?制表符
\n ? ? ? ? ? 換行符
FS ? ? ? ? ?BEGIN時定義分隔符
RS???????輸入的記錄分隔符, 默認(rèn)為換行符(即文本是按一行一行輸入)
~ ? ? ? ? ? ?匹配,與==相比不是精確比較
!~ ? ? ? ? ? 不匹配,不精確比較
== ? ? ? ? 等于,必須全部相等,精確比較
!= ? ? ? ? ? 不等于,精確比較
&& ? ? 邏輯與
|| ? ? ? ? ? ? 邏輯或
+ ? ? ? ? ? ?匹配時表示1個或1個以上
/[0-9][0-9]+/ ? 兩個或兩個以上數(shù)字
/[0-9][0-9]*/ ? ?一個或一個以上數(shù)字
FILENAME 文件名
OFS? ? ??輸出字段分隔符, 默認(rèn)也是空格,可以改為制表符等
ORS? ? ? ? 輸出的記錄分隔符,默認(rèn)為換行符,即處理結(jié)果也是一行一行輸出到屏幕
-F'[:#/]' ? 定義三個分隔符
2:awk案例
(1)按行輸出文本
awk -F":" '{print}' /etc/passwd //輸出所有
awk -F":" '{print?$0}' /etc/passwd //輸出所有
awk -F: 'NR==3,NR==6{print}' /etc/passwd ?//顯示第3行到第6行
awk -F: 'NR>=3&&NR<=6{print}' /etc/passwd ? ? ??//顯示第3行到第6行
awk -F: 'NR==3||NR==6{print}' /etc/passwd ? ? ??//顯示第3行和第6行
awk '(NR%2)==1{print}' /etc/passwd //顯示奇數(shù)行
awk '(NR%2)==0{print}' /etc/passwd //顯示偶數(shù)行
awk '/^root/{print}' /etc/passwd //顯示以root開頭的行
awk '/nologin$/{print}' /etc/passwd //顯示以nologin結(jié)尾的行
awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd ?????//統(tǒng)計以/bin/bash結(jié)尾的行數(shù)
awk 'BEGIN{RS=""};END{print NR}' /etc/ssh/sshd_config //統(tǒng)計以空行分隔的文本段落數(shù)
awk '{print NR,$0}' /etc/passwd ? ? ? ? ? ? ? ? ? ? ? ? ? ???? ?//輸出每行的行號
awk -F: '{print NR,NF,$NF,"\t",$0}' /etc/passwd ? ? ?//依次打印行號,字段數(shù),最后字段值,制表符,每行內(nèi)容
awk -F: 'NR==5{print}'??/etc/passwd ? ? ? ? ? ? ? ? ? ? ?? ?//顯示第5行
route -n|awk 'NR!=1{print}' ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? //不顯示第一行
awk -F: '{print NF}' /etc/passwd ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? //顯示每行有多少字段
awk?-F:?'{print $NF}' /etc/passwd ? ? ? ? ? ? ? ? ? ? ? ? ?? ? //將每行第NF個字段的值打印出來
?awk -F: 'NF==4 {print }' /etc/passwd ? ? ? ? ? ? ? ? ?? ?? //顯示只有4個字段的行
awk -F: 'NF>2{print $0}' /etc/passwd ? ? ? ? ? ? ? ? ? ? ??//顯示每行字段數(shù)量大于2的行
(2)按字段輸出文本
awk -F":" '{print $3}' /etc/passwd //顯示第三列
awk -F":" '{print $1 $3}'?/etc/passwd ? ? ? ? ? ? ? ? ? ? ??//$1與$3相連輸出,無空格,
awk -F":" '{print $1,$3}'?/etc/passwd ? ? ? ? ? ? ? ? ? ? ? //多了一個逗號,輸出第1和第3個字段,有空格
awk -F: '$2=="!!" {print}' /etc/shadow //統(tǒng)計密碼為空的shadow記錄
awk 'BEGIN {FS=":"}; $2=="!!" {print}' /etc/shadow ##顯示密碼為空的用戶的shadow信息
awk -F ":" '$7~"/bash"?{print $1}' /etc/passwd ##顯示第七個字段為/bash的行的第一個字段
awk -F: 'NR==5{print}' /etc/passwd?? ? ? ? ? ? ? ? ? ? ?? ?//顯示第5行
awk -F":" '{print $1 " " $3}' /etc/passwd ? ? ? ? ? ? ? ? ?//$1與$3之間手動添加空格分隔
(3)通過管道、雙引號調(diào)用shell命令
awk -F: '/bash$/{print | "wc -l"}' /etc/passwd ????##統(tǒng)計bash用戶的個數(shù)
awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}}'???##統(tǒng)計在線用戶的數(shù)量
awk 'BEGIN {"hostname" | getline;print $0}' ?????##輸出當(dāng)前主機名
awk -F: '$1~/mail/ && $3>6?{print }' /etc/passwd ? ? ? ? //邏輯與,$1匹配mail,并且$3>6
awk -F: '{if($1~/mail/ && $3>8) print }' /etc/passwd??
awk -F: '$1~/mail/ || $3>1000 {print }' /etc/passwd ? ? ? //邏輯或,統(tǒng)計以mail開頭或第3列大于1000的行
awk -F: '{if($1~/mail/ || $3>1000) print }' /etc/passwd?
4.2.3 sort 工具
在 Linux 系統(tǒng)中,常用的文件排序工具有三種:sort、uniq、wc 。本章將介紹前兩種
工具的用法。
sort 是一個以行為單位對文件內(nèi)容進(jìn)行排序的工具,也可以根據(jù)不同的數(shù)據(jù)類型來排
序。例如數(shù)據(jù)和字符的排序就不一樣。sort 命令的語法為“sort [選項] 參數(shù)”,其中常用的選
項包括以下幾種。
-f:忽略大小寫;
?-b:忽略每行前面的空格;
?-M:按照月份進(jìn)行排序;
?-n:按照數(shù)字進(jìn)行排序;
?-r:反向排序;
?-u:等同于 uniq,表示相同的數(shù)據(jù)僅顯示一行;
?-t:指定分隔符,默認(rèn)使用[Tab]鍵分隔;
?-o <輸出文件>:將排序后的結(jié)果轉(zhuǎn)存至指定文件;
?-k:指定排序區(qū)域。
示例 1:將/etc/passwd 文件中的賬號進(jìn)行排序。
[root@localhost ~]# sort /etc/passwd
示例 2:將/etc/passwd 文件中第三列進(jìn)行反向排序。
[root@localhost ~]# sort -t ':' -rk 3 /etc/passwd
備注:
?-r:反向排序;
?-t:指定分隔符,默認(rèn)使用[Tab]鍵分隔;
?-k:指定排序區(qū)域。
示例 3:將/etc/passwd 文件中第三列進(jìn)行排序,并將輸出內(nèi)容保存至 user.txt 文件中。
[root@localhost ~]# sort -t ':' -k 3 /etc/passwd -o user.txt
[root@localhost ~]# cat user.txt
備注:
?-t:指定分隔符,默認(rèn)使用[Tab]鍵分隔;
-k:指定排序區(qū)域。
4.2.4 uniq 工具
Uniq 工具在 Linux 系統(tǒng)中通常與 sort 命令結(jié)合使用,用于報告或者忽略文件中的重復(fù)
行。具體的命令語法格式為:uniq [選項] 參數(shù)。其中常用選項包括以下幾種。
?-c:進(jìn)行計數(shù);
?-d:僅顯示重復(fù)行;
?-u:僅顯示出現(xiàn)一次的行。
示例 1:刪除 testfile 文件中的重復(fù)行
[root@localhost ~]# cat testfile
Linux 10
Linux 20
Linux 30
Linux 30
Linux 30
CentOS 6.5
CentOS 6.5
CentOS 6.5
CentOS 7.3
CentOS 7.3
CentOS 7.3
[root@localhost ~]# uniq testfile
Linux 10
Linux 20
Linux 30
CentOS 6.5
CentOS 7.3
示例 2:刪除 testfile 文件中的重復(fù)行,并在行首顯示該行重復(fù)出現(xiàn)的次數(shù)。
[root@localhost ~]# uniq -c testfile
1 Linux 10
1 Linux 20
3 Linux 30
3 CentOS 6.5
3 CentOS 7.3
備注:
?-c:進(jìn)行計數(shù);
示例 3:查找 testfile 文件中的重復(fù)行。
[root@localhost ~]# uniq -d testfile
Linux 30
CentOS 6.5
CentOS 7.3
備注:
? -d:僅顯示重復(fù)行;
4.2.5 tr 工具
tr 命令常用來對來自標(biāo)準(zhǔn)輸入的字符進(jìn)行替換、壓縮和刪除??梢詫⒁唤M字符替換之后
變成另一組字符,經(jīng)常用來編寫優(yōu)美的單行命令,作用很強大。
其常用選項包括以下內(nèi)容。
?-c:取代所有不屬于第一字符集的字符;
?-d:刪除所有屬于第一字符集的字符;
?-s:把連續(xù)重復(fù)的字符以單獨一個字符表示;
?-t:先刪除第一字符集較第二字符集多出的字符。
示例 1:將輸入字符由大寫轉(zhuǎn)換為小寫。
[root@localhost ~]# echo "KGC" | tr 'A-Z' 'a-z'
kgc
示例 2:壓縮輸入中重復(fù)的字符。
[root@localhost ~]# echo "thissss is a text linnnnnnne." | tr -s 'sn'
this is a text line.
備注:
? -s:把連續(xù)重復(fù)的字符以單獨一個字符表示;
示例 3:刪除字符串中某些字符。
[root@localhost ~]# echo 'hello world' | tr -d 'od'
hell wrl
備注:
?-d:刪除所有屬于第一字符集的字符;
柚子快報邀請碼778899分享:php 數(shù)據(jù)庫 正則表達(dá)式
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。