grep
1)缺點:不能對已知結(jié)果進行更改掖肋。
2)常用命令
查找指定進程和個數(shù)
[root@VM_0_15_centos ~]# ps -ef | grep mysql
[root@VM_0_15_centos ~]# ps -ef | grep -c mysql
2.2)查找多個文件相同的部分(每行比較)
[root@VM_0_15_centos demo]# cat 1.txt | grep -f 2.txt
從單個文件或多個文件查找指定內(nèi)容并顯示行號
[root@VM_0_15_centos demo]# cat 1.txt | grep 1 -n
1:我是相同的內(nèi)容1
2:我是不同的內(nèi)容1
指定字符串查找開頭酿箭,非開頭和結(jié)尾的內(nèi)容
指定字符串開頭:
[root@VM_0_15_centos demo]# grep "^a" 1.txt
a開頭
非指定字符串開頭:
[root@VM_0_15_centos demo]# grep "[a]" 1.txt
我是b同的內(nèi)容1
我是相同的內(nèi)容2結(jié)尾c
我是不同的內(nèi)容2
指定字符串結(jié)尾:
[root@VM_0_15_centos demo]# grep "c$" 1.txt
我是相同的內(nèi)容2結(jié)尾c
過濾指定日志里面的ip個數(shù)
[root@VM_0_15_centos demo]# cat 1.txt | grep -c "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
3
[root@VM_0_15_centos demo]# cat 1.txt | grep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"
10.10.20.138
我是相同的內(nèi)10.10.20.134容2結(jié)尾c
10.10.20.139
過濾指定路徑下所有文件里面包含指定字符內(nèi)容
[root@VM_0_15_centos demo]# grep -r -n "10.10.20" 1.txt
1:10.10.20.138
3:我是相同的內(nèi)10.10.20.134容2結(jié)尾c
4:10.10.20.139
顯示文件總行數(shù)
[root@VM_0_15_centos demo]# grep -n "" 1.txt
1:root
2:
3:root:X:sdsdsd
4:
5:我是相同的內(nèi)10.10.20.134容2結(jié)尾c
6:root`
7:
8:
9:
sed
1咐扭、sed默認不編輯原文件辩蛋,而是逐行操作,復(fù)制一份到指定內(nèi)存(模式空間)喜颁;
2苍鲜、模式空間內(nèi)進行模式匹配,即和指定條件做匹配:
不滿足康愤,輸出到標準輸出儡循;
滿足:進行指定的模式操作,再輸出到標準輸出征冷;
3择膝、命令行:[root@www ~]# sed [-nefr] [動作]
常用命令:
將文件內(nèi)容列出并且列印行號,同時將第2~3行刪除
[root@VM_0_15_centos demo]# nl 1.txt | sed '2,3d'
1 10.10.20.138
4 10.10.20.139
刪除第2行到最后一行
[root@VM_0_15_centos demo]# nl 1.txt | sed '2,$d'
1 10.10.20.138
刪除第2行
[root@VM_0_15_centos demo]# cat -n 1.txt | sed '2d'
1 10.10.20.138
3 我是相同的內(nèi)10.10.20.134容2結(jié)尾c
4 10.10.20.139
在第2行后(即加上第3行)加上‘drink tea’
[root@VM_0_15_centos demo]# cat -n 1.txt | sed '2adrink tea'
1 10.10.20.138
2 我是b同的內(nèi)容1
drink tea
3 我是相同的內(nèi)10.10.20.134容2結(jié)尾c
4 10.10.20.139
在第2行前加上‘drink tea’
[root@VM_0_15_centos demo]# cat -n 1.txt | sed '2idrink tea'
1 10.10.20.138
drink tea
2 我是b同的內(nèi)容1
3 我是相同的內(nèi)10.10.20.134容2結(jié)尾c
4 10.10.20.139
在第2行后增加兩行
[root@VM_0_15_centos demo]# cat -n 1.txt | sed '2adrink tea or \
drink beer'
1 10.10.20.138
2 我是b同的內(nèi)容1
drink tea or
drink beer
3 我是相同的內(nèi)10.10.20.134容2結(jié)尾c
4 10.10.20.139
將2-3行的內(nèi)容提還調(diào)
[root@VM_0_15_centos demo]# cat -n 1.txt | sed '2,3c asasas'
1 10.10.20.138
asasas
4 10.10.20.139
將2-3行的內(nèi)容顯示出來
[root@VM_0_15_centos demo]# cat -n 1.txt | sed -n '2,3p'
2 我是b同的內(nèi)容1
3 我是相同的內(nèi)10.10.20.134容2結(jié)尾c
查找指定行區(qū)間以XX開頭的內(nèi)容
[root@VM_0_15_centos demo]# sed -n '1,4{/^root/p}' 1.txt
root
root:X:sdsdsd
root`
匹配行之后增加顯示內(nèi)容
[root@VM_0_15_centos demo]# sed -n '/3/,$p' 1.txt
我是相同的內(nèi)10.10.20.134容2結(jié)尾c
root`
[root@VM_0_15_centos demo]# sed -n '/3/,+1p' 1.txt
我是相同的內(nèi)10.10.20.134容2結(jié)尾c
root
文本逆序輸出(行逆序)
[root@VM_0_15_centos demo]# sed '1!G;h;$!d' 1.txt
root`
我是相同的內(nèi)10.10.20.134容2結(jié)尾c
root:X:sdsdsd
root
顯示行號
顯示空行
[root@VM_0_15_centos demo]# sed '=' 1.txt
屏蔽空行
[root@VM_0_15_centos demo]# sed '/./=' 1.txt
顯示文件總行數(shù)
[root@VM_0_15_centos demo]# sed -n "$=" 1.txt
9
顯示偶數(shù)和奇數(shù)行
偶數(shù)行
[root@VM_0_15_centos demo]# sed -n 'n;p' 1.txt
[root@VM_0_15_centos demo]# sed -n '2~2p' 1.txt
奇數(shù)行
[root@VM_0_15_centos demo]# sed -n 'p;n' 1.txt
[root@VM_0_15_centos demo]# sed -n '1~2p' 1.txt
文件中每行內(nèi)容逆向顯示
[root@VM_0_15_centos demo]# cat 1.txt | sed '/\n/!G;s/(.)(.*\n)/&\2\1/;//D;s/.//'
指定內(nèi)容替換(將root全部替換成mysql)
[root@VM_0_15_centos demo]# cat 1.txt | sed 's/root/mysql/g'
awk
1)支持過濾方式“列”匹配
2)可以嵌套循環(huán)使用
3)內(nèi)置變量
FS 保存或設(shè)置分隔符检激,例如FS=","肴捉;
1,0 當前讀入整行的文本內(nèi)容齿穗;
NF 記錄當前處理行的字段個(列)數(shù)傲隶;
NR 記錄當前處理行的數(shù)量;
FNR 保存當前處理行在原文本內(nèi)的行號窃页;
FILENAME 當前處理的文本名跺株;
ENVIRON 調(diào)用shell環(huán)境變量。
4)語法:
awk [選項參數(shù)] 'script' var=value file(s)
或
awk [選項參數(shù)] -f scriptfile var=value file(s)###列匹配(列以空格或tab分割)
展示所有內(nèi)容
[root@VM_0_15_centos demo]# cat 1.txt | awk '{print 1}'
查找當前剩余內(nèi)存
free | grep Mem | awk '{print"當前剩余內(nèi)存:\n",$7}'
訪問IP過濾
grep "Accepted" /var/log/secure | awk '{print $11}'
統(tǒng)計每行有多少列
[root@VM_0_15_centos demo]# cat 1.txt | awk '{print NF}'
統(tǒng)計root出現(xiàn)的次數(shù)
[root@VM_0_15_centos demo]# cat 1.txt | awk -F[:/] '{i=1}{while(i<=NF){if($i~/root/){j++};i++}}END{print j}'
將列以[,]分割脖卖,然后展示第1列和第2列
[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' '{print 2}'
定義變量乒省,將列以[,]分割,然后展示第1列和第1+a列
[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' -va=1 '{print (1+a)}'
將列以[,]分割畦木,并將第1列大于2的行輸出
[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' '$1>2'
將列以[,]分割袖扛,并將第1列等于2的行輸出
[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' '1==2 {print 3}'
2 4
[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' '{print NR,FNR,2,$3}'
輸出包含th的行
[root@VM_0_15_centos demo]# cat 2.txt | awk '/th/'
打印99乘法表
[root@VM_0_15_centos demo]# seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81