linux三劍客

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=","肴捉;
N 指定分隔符的第N個字段腹侣,例如1,5代表第一列和第三列;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 0}' 展示第一列 [root@VM_0_15_centos demo]# cat 1.txt | awk '{print1}'

查找當前剩余內(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 1,2}'

定義變量乒省,將列以[,]分割,然后展示第1列和第1+a列

[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' -va=1 '{print 1,(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' 2,3,4,56,7 將列以[,]分割,并將第1列等于2的行的第1和3列輸出 [root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' '1==2 {print 1,3}'
2 4

[root@VM_0_15_centos demo]# cat 2.txt | awk -F '[,]' '{print NR,FNR,1,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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末馋劈,一起剝皮案震驚了整個濱河市攻锰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌妓雾,老刑警劉巖娶吞,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異械姻,居然都是意外死亡妒蛇,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門楷拳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绣夺,“玉大人,你說我怎么就攤上這事欢揖√账#” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵她混,是天一觀的道長烈钞。 經(jīng)常有香客問我,道長坤按,這世上最難降的妖魔是什么毯欣? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮臭脓,結(jié)果婚禮上酗钞,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好砚作,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布窘奏。 她就那樣靜靜地躺著,像睡著了一般葫录。 火紅的嫁衣襯著肌膚如雪蔼夜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天压昼,我揣著相機與錄音,去河邊找鬼瘤运。 笑死窍霞,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的拯坟。 我是一名探鬼主播但金,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼郁季!你這毒婦竟也來了冷溃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤梦裂,失蹤者是張志新(化名)和其女友劉穎似枕,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體年柠,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡凿歼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了冗恨。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片答憔。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖掀抹,靈堂內(nèi)的尸體忽然破棺而出虐拓,到底是詐尸還是另有隱情,我是刑警寧澤傲武,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布蓉驹,位于F島的核電站,受9級特大地震影響谱轨,放射性物質(zhì)發(fā)生泄漏戒幔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一土童、第九天 我趴在偏房一處隱蔽的房頂上張望诗茎。 院中可真熱鬧,春花似錦、人聲如沸敢订。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽楚午。三九已至昭齐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間矾柜,已是汗流浹背阱驾。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留怪蔑,地道東北人里覆。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像缆瓣,于是被迫代替她去往敵國和親喧枷。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容