grep 過(guò)濾
-E 匹配正則表達(dá)式 相當(dāng)于egrep
-o 只輸出匹配到的內(nèi)容
-v 輸出不是匹配到的內(nèi)容
-i 忽略大小寫 ,在find中用iname忽略大小寫
-n 顯示行號(hào)
-w 以單詞的形式匹配
-A 8 after 同時(shí)輸出匹配到內(nèi)容后的8行
-B 9 before 同時(shí)輸出匹配到內(nèi)容前的9行
-C 3 同時(shí)輸出前后3行
-P
-r 遞歸查找
-l 只顯示文件名
注意:
find /greptest -type f -name "*.cc" |xargs grep "hello" 可以找出這個(gè)目錄下所有層的內(nèi)容
greptest]# grep "hello" /greptest/*.cc 只能在這個(gè)目錄下查找內(nèi)容
[root@m01 greptest]# tree /greptest
/greptest
├── 1.cc
├── 2.cc
├── 3.cc
├── 4.cc
├── 5.cc
└── alex
├── 1.cc
├── 2.cc
├── 3.cc
├── 4.cc
└── 5.cc
1 directory, 10 files
[root@m01 greptest]# find /greptest -type f -name "*.cc" |xargs grep "hello"
/greptest/alex/1.cc:hello 127.0.0.1
/greptest/alex/2.cc:hello 127.0.0.1
/greptest/alex/3.cc:hello 127.0.0.1
/greptest/alex/4.cc:hello 127.0.0.1
/greptest/alex/5.cc:hello 127.0.0.1
/greptest/1.cc:hello 127.0.0.1
/greptest/2.cc:hello 127.0.0.1
/greptest/3.cc:hello 127.0.0.1
/greptest/4.cc:hello 127.0.0.1
/greptest/5.cc:hello 127.0.0.1
[root@m01 greptest]# grep "hello" /greptest/*.cc
/greptest/1.cc:hello 127.0.0.1
/greptest/2.cc:hello 127.0.0.1
/greptest/3.cc:hello 127.0.0.1
/greptest/4.cc:hello 127.0.0.1
/greptest/5.cc:hello 127.0.0.1
-r 的用法立宜,不能指定文件類型姥闪,加*.cc
[root@m01 greptest]# grep -r "hello" /greptest/
/greptest/alex/1.cc:hello 127.0.0.1
/greptest/alex/2.cc:hello 127.0.0.1
/greptest/alex/3.cc:hello 127.0.0.1
/greptest/alex/4.cc:hello 127.0.0.1
/greptest/alex/5.cc:hello 127.0.0.1
/greptest/1.cc:hello 127.0.0.1
/greptest/2.cc:hello 127.0.0.1
/greptest/3.cc:hello 127.0.0.1
/greptest/4.cc:hello 127.0.0.1
/greptest/5.cc:hello 127.0.0.1
刪除空行
egrep -v "^$|^ +$" 1.cc
優(yōu)化后:
egrep -v "^ *$" 1.cc>test.cc
sed 過(guò)濾 替換 修改文件內(nèi)容 增刪改查
-n 取消默認(rèn)輸出
-i 修改文件內(nèi)容 -i.bak 修改文件并備份
-r 支持?jǐn)U展正則
-e 支持多項(xiàng)編輯
用法:
p:輸出
a:追加
i:插入
d:刪除
sed -n '/hello/p' test.txt
sed -n '/1/,/4/p' test.txt
*sed后置引用*
[root@m01 greptest]# stat /etc/hosts
文件:"/etc/hosts"
大懈险尽:158 塊:8 IO 塊:4096 普通文件
設(shè)備:fd00h/64768d Inode:16829686 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近訪問(wèn):2023-03-02 16:40:01.295856421 +0800
最近更改:2013-06-07 22:31:32.000000000 +0800
最近改動(dòng):2022-10-07 12:29:29.882369829 +0800
創(chuàng)建時(shí)間:-
[root@m01 greptest]# stat -c %a /etc/hosts
644
[root@m01 greptest]# stat -c %A /etc/hosts
-rw-r--r--
方法:
[root@m01 greptest]# stat /etc/hosts|sed -n '4p'|sed -r 's#^.*\(([0-9]+)/.*#\1#g'
0644
合并后:
[root@m01 greptest]# stat /etc/hosts|sed -nr '4s#^.*\(([0-9]+)/.*#\1#gp'
0644
awk 過(guò)濾 取列 統(tǒng)計(jì)計(jì)算
awk 參數(shù) '條件{動(dòng)作}'
參數(shù):
-F 選擇分割符號(hào) FS awk內(nèi)置命令
-v 創(chuàng)建或者修改變量
[root@m01 ~]# x=1
[root@m01 ~]# y=4
[root@m01 ~]# awk -vn1=$x -vn2=$y 'BEGIN{print n1/n2}' #begin:在讀取文件之前
0.25
內(nèi)置變量
NR 行號(hào)
RS 記錄行結(jié)束的標(biāo)志符
NF 每一行有多少列 $(NF-3)倒數(shù)第四列
OFS output FS 輸出的分隔符
[root@m01 ~]# awk -F: -vOFS=: '{tmp=$NF;$NF=$1;$1=tmp}1{print $0}' /etc/passwd