SHELL實戰day4

                                             一  正則介紹_grep

1:grep用來過濾關鍵詞
grep [-cinvABC] 'word' filename
-c 行數 (count)
-i 不區分大小寫
-n 顯示行號
-v 取反
-r 遍歷所有子目錄
SHELL實戰day4
-A 後面跟數字,過濾出符合要求的行以及下面n行
-B 同上,過濾出符合要求的行以及上面n行
-C 同上,同時過濾出符合要求的行以及上下各n行
2:示例
grep -n 'root' /etc/passwd
grep -nv 'nologin' /etc/passwd
grep '[0-9]'/etc/inittab
grep -v '[0-9]'/etc/inittab
grep -v '^#' /etc/inittab
grep -v '^#' /etc/inittab|grep -v '^$'
grep '^[^a-zA-Z]' test.txt
grep 'r.o' test.txt
.表示任意一個字符
grep 'oo' test.txt
表示左邊的字符重複0到n次
grep '.' test.txt
.
表示任意字符
grep 'o{2}' /etc/passwd
{}表示前面字符的重複範圍
egrep 'o{2}' /etc/passwd
和上面一樣的效果
egrep 'o+' /etc/passwd
+符號前面字符的一次或多次
egrep 'oo?' /etc/passwd
?表示前面字符重複0或1次
egrep 'root|nologin' /etc/passwd
|表示或者的意思
egrep '(oo){2}' /etc/passwd

                                      二 sed

1: sed -n '5'p test.txt
sed -n '1,5'p test.txt
sed -n '1,$'p test.txt
sed -n '/root/'p test.txt
sed -n '/root/'Ip test.txt
匹配大小寫
SHELL實戰day4
sed -n '/^1/'p test.txt
sed -n 'in$'p test.txt
sed -n '/r..o/'p test.txt
sed -n 'oo*'p test.txt
sed -e '1'p -e '/111/'p -n test.txt
SHELL實戰day4

2: sed '1'd test.txt
sed '1,3'd test.txt
sed -i '1,3'd test.txt
刪除文件內容
SHELL實戰day4
sed '/oot/'d test.txt
sed '1,2s/ot/to/g' test.txt
sed 's#ot#to#g' test.txt
sed 's/[0-9]//g' test.txt
sed 's/[a-zA-Z]//g' test.txt
sed -r 's/(rot)(.)(bash)/\3\2\1/' test.txt
SHELL實戰day4
sed 's/^.
$/123&/' test.txt
sed -i 's/ot/to/g' test.txt

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章