
sed 是 stream editor 的縮寫,流編輯器,主要用于對標準輸出或文件進行逐行處理。
(資料圖片僅供參考)
原文件test_sed.sh內容:
HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test
在testfile文件的第四行后添加一行,并將結果輸出到標準輸出,在命令行提示符下輸入如下命令:
[root@liang shell]# sed -e "4 a newLine" test_sed.shHELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test newLine
a 動作是在匹配的行之后追加字符串,追加的字符串中可以包含換行符(實現追加多行的情況)追加一行的話前后都不需要添加換行符 \n追加多行
sed -e "4 a newline\nnewline2" test_sed.sh
同理,指定行前插入內容
sed -e "4 i newline\nnewline2" test_sed.sh
打印第3-4行
Linux test [root@liang shell]# sed -n "3,4p" test_sed.sh
將第二行內容修改為111
sed "2c 111" test_sed.sh
sed "2,5d" test_sed.sh 刪除2~5行sed "/^#/d" test_sed.sh 刪除以#開頭的行sed "/^#/,/8$/d" test_sed.sh 刪除以#開頭以8結尾的行,如果找不到8結尾的,會刪除#開頭的之后的所有行sed "/^$/d" test_sed.sh 刪除空行
sed "s/#Port 22/Port 2200/g" test_sed.sh #將‘#Port 22’替換為‘Port 2200’
搜索有root關鍵字的行
sed -n "/root/p" test_sed.sh
匹配有連續5位數字的行
sed -nr "/[0-9]{5}/ p" test_sed.sh
sed -i "/root/ {p;s/#Port 22/Port 2200/g;/^#/d;/^$/d}" test_sed.sh
或者
sed -n "/root\|daemon/p" test_sed.shsed -n "/root/{/daemon/p}" test_sed.sh
================================================================================
================================================================================
=================================================================================
grep的全稱是global regular expression print,是linux中最強大的文本搜索命令之一,常用于搜索文本文件中是否含有某些特定模式的字符串。該命令以行為單位讀取文本并使用正則表達式進行匹配,匹配成功后打印出該行文本。
grep [option] pattern [file1, file2, ...]
-n :顯示行號-o :只顯示匹配的內容-q :靜默模式,沒有任何輸出,得用$?來判斷執行成功沒有,即有沒有過濾到想要的內容-r :遞歸搜索對目錄下的所有文件進行搜索-l :如果匹配成功,則只將文件名打印出來,失敗則不打印,通常-rl一起用,grep -rl "root" /etc-A :如果匹配成功,則將匹配行及其后n行一起打印出來-B :如果匹配成功,則將匹配行及其前n行一起打印出來-C :如果匹配成功,則將匹配行及其前后n行一起打印出來-c :如果匹配成功,只輸出匹配的行的數量統計-E :等于egrep,支持拓展正則表達式-i :忽略大小寫-v :取反,不匹配-w :匹配整個單詞
表達式 | 一個普通標題 |
^ | 錨定行的開始 如:"^grep"匹配所有以grep開頭的行。 |
$ | 錨定行的結束 如:"grep$"匹配所有以grep結尾的行。 |
. | 匹配一個非換行符的字符 如:"gr.p"匹配gr后接一個任意字符,然后是p。 |
? | 匹配0或者一個字符 如:"gr?p"匹配g后接一個或0個r字符,然后是p。 |
* | 匹配零個或多個先前字符 如:"*grep"匹配所有一個或多個空格后緊跟grep的行。 |
.* | 一起用代表任意字符。 |
[] | 匹配一個指定范圍內的字符,如"[Gg]rep"匹配Grep和grep。 |
[^] | 匹配一個不在指定范圍內的字符,如:"[^A-FH-Z]rep"匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。 |
(..) | 標記匹配字符,如"(love)",love被標記為1。 |
< | 錨定單詞的開始,如:" |
> | 錨定單詞的結束,如"grep>"匹配包含以grep結尾的單詞的行。 |
x{m} | 重復字符x,m次,如:"o{5}"匹配包含5個o的行。 |
x{m,} | 重復字符x,至少m次,如:"o{5,}"匹配至少有5個o的行。x{m,n} # 重復字符x,至少m次,不多于n次,如:"o{5,10}"匹配5--10個o的行。 |
\w | 匹配文字和數字字符,也就是[A-Za-z0-9],如:"G\w*p"匹配以G后跟零個或多個文字或數字字符,然后是p。 |
\W | \w的反置形式,匹配一個或多個非單詞字符,如點號句號等。 |
\b | 單詞鎖定符,如: "\bgrep\b"只匹配grep。 |
[root@liang shell]# grep xy text0001.txt 4 xy 100 50 60 70 [root@liang shell]# grep "xy" text0001.txt 4 xy 100 50 60 70
在多個文件中搜索,-n顯示行號
[root@liang shell]# grep -n "xy" text0001.txt text0002.txt text0003.txt text0001.txt:5:4 xy 100 50 60 70 text0002.txt:1:xytext0002.txt:5:xyxyxytext0002.txt:7:grepxytext0003.txt:1:xytext0003.txt:2:xxytext0003.txt:4:xxyytext0003.txt:5:xyxyxytext0003.txt:7:grepxy
搜索一個目錄下的所有文件,列出包含指定字符串的文件名 -l
[root@liang shell]# grep -rl "xxy" ././text0003.txt
統計單個文件
[root@liang shell]# grep -r -c "xxy" text0003.txt2
統計多個文件
[root@liang shell]# grep -c "xy" text0001.txt text0002.txt text0003.txt text0001.txt:1text0002.txt:3text0003.txt:5
統計一個目錄下的所有文件
[root@liang shell]# grep -cr "xy" dirpath
正則匹配符合條件的字符串顯示匹配到的行
[root@liang shell]# cat text0001.txt ID Name PHP Linux MySQL Average1 Liming 82 95 86 87.662 Sc 74 96 87 85.663 Gao 99 83 93 91.664 xy 100.00 50 60 70 this is a. test line.1105[root@liang shell]# grep -E "[0-9]{3}" text0001.txt4 xy 100.00 50 60 70 1105
-o只輸出匹配到的內容
[root@liang shell]# echo this is a. test line. | grep -o -E "[a-z]+\."a.line.
請對照常用的pattern規則表達式理解
grep -E "x?y" text0002.txt x出現0-1次,y出現1次grep -E "x*y" text0002.txt x出現0-任意次,y出現1次grep -E "x{n}y" text0002.txt x出現n次,y出現1次grep -E "x+y" text0002.txt x出現1-任意次,y出現1次grep -E "x.y" text0002.txt xy中間有1個任意非換行符grep -E "x.*y" text0002.txt xy中間有任意個任意非換字符
[root@liang shell]# grep -E "x.*y" text0002.txtxyxcxcyxxyxyxyabx344dycgrepx2yx.*yxxxxxxxxy[root@liang shell]# grep -F "x.*y" text0002.txtx.*y
[root@liang shell]# echo "hello world" | grep -i "HELLO"hello world
打印之前的行
[root@liang shell]# grep "test" -A 2 text0001.txtthis is a. test line.1105DSF
打印之后的行
[root@liang shell]# grep "test" -B 2 text0001.txt3 Gao 99 83 93 91.664 xy 100.00 50 60 70 this is a. test line.
打印前后的行
[root@liang shell]# grep "test" -C 2 text0001.txt3 Gao 99 83 93 91.664 xy 100.00 50 60 70 this is a. test line.1105DSF
如果匹配結果有多個,會用“--”作為各匹配結果之間的分隔符:
[root@liang shell]# grep "gr" text0002.txtgrpgr dfdxxgrpp[root@liang shell]# grep -w "gr" text0002.txtgr dfd