linux常用命令
文件內(nèi)容查看命令
文件查看命令主要為cat 溺忧、more突梦、less。關(guān)于這幾個(gè)命令在文件命令寫(xiě)得比較詳細(xì)笛辟,大家可以參考功氨。
個(gè)人對(duì)以上幾個(gè)命令進(jìn)行了基本實(shí)踐操作
cat
- 1 創(chuàng)建文章不清除原有內(nèi)容 cat>>filename 創(chuàng)建之后可直接進(jìn)行編輯,如果是已存在的文件手幢,則是在原有內(nèi)容下面進(jìn)行內(nèi)容新增(退出cat可以直接按下contral+D)捷凄,cat filename直接查看內(nèi)容
? Desktop cat >>test1
test1
hello test
test 3
? Desktop cat >> test1
test 4
? Desktop cat test1
test1
hello test
test 3
test 4
? Desktop
- 2 創(chuàng)建文章清除原有內(nèi)容 cat >filename,創(chuàng)建之后可以直接進(jìn)行編輯,如果是已存在的文件围来,則原有內(nèi)容會(huì)被清空
? Desktop cat >test2
test11
測(cè)試
? Desktop cat >test2
新增
? Desktop cat test2
新增
所有 cat >>與cat >的區(qū)別是對(duì)文件原有內(nèi)容的處理不一樣跺涤,如果是不想要原有內(nèi)容則可以直接用cat > ,如果想要原有內(nèi)容則用cat >>.為了保險(xiǎn)起見(jiàn)還是建議用cat >>命令匈睁,大不了可以對(duì)原有內(nèi)容進(jìn)行刪除
- 3 文件合并
cat file1 file2 >>file3 將file1與file2文件合并為file3,如果file3未創(chuàng)建則直接創(chuàng)建桶错,如果已創(chuàng)建則航唆,在原有內(nèi)容上新增。
? Desktop cat test1 test2 >>test3
? Desktop cat test3
test1
hello test
test 3
test 4
新增
? Desktop cat >> test3
我再式下
? Desktop cat test1 test2 >>test3
? Desktop cat test3
test1
hello test
test 3
test 4
新增
我再式下
test1
hello test
test 3
test 4
新增
同樣我們可以用 cat file1 file2 > file3 ,合并時(shí)如果file3中有內(nèi)容牛曹,則會(huì)被清除佛点,與新建文件是類似醇滥。前面都是在一個(gè)目錄下面進(jìn)行文章的合并黎比,如果是不同目錄,則只需要加上目錄路徑就可以
? study cat /Users/huxy/Desktop/test1 /Users/huxy/code/test4 > test5
? study cat test5
test1
hello test
test 3
test 4
件下
其他
- 1 cat -n file 列出行號(hào)
- 2 cat -b file 與 -n類似鸳玩,對(duì)空白行不進(jìn)行編號(hào)
- 3 cat -s file 當(dāng)遇到有連續(xù)兩行以上的空白行阅虫,就代換為一行的空白行
? Desktop cat -n test3
1 test1
2 hello test
3
4 test 3
5 test 4
6 新增
7 我再式下
8 test1
9 hello test
10
11 test 3
12 test 4
13 新增
? Desktop cat -b test3
1 test1
2 hello test
3 test 3
4 test 4
5 新增
6 我再式下
7 test1
8 hello test
9 test 3
10 test 4
11 新增
? Desktop cat -s test3
test1
hello test
test 3
test 4
新增
我再式下
test1
hello test
test 3
test 4
新增
nl
nl 與cat -n很類似
- nl testcat7.txt 顯示行號(hào)(空白行不展示)
- nl -b a testcat7.txt 顯示行號(hào),空白行也展示
- nl -b a -n rz testcat7.txt 顯示行號(hào)不跟,行號(hào)前面自動(dòng)補(bǔ)0,統(tǒng)一輸出格式
- nl -b a -n rz -w 3 testcat7.txt 顯示固定寬度颓帝,現(xiàn)在固定3位數(shù)的寬度
? Desktop nl test3
1 test1
2 hello test
3 test 3
4 test 4
5 新增
6 我再式下
7 test1
8 hello test
9 test 3
10 test 4
11 新增
12 test11
? Desktop nl -b test3
? Desktop nl -b a test3
1 test1
2 hello test
3
4 test 3
5 test 4
6 新增
7 我再式下
8 test1
9 hello test
10
11 test 3
12 test 4
13 新增
14
15
16
17 test11
? Desktop nl -b a -n rz test3
000001 test1
000002 hello test
000003
000004 test 3
000005 test 4
000006 新增
000007 我再式下
000008 test1
000009 hello test
000010
000011 test 3
000012 test 4
000013 新增
000014
000015
000016
000017 test11
? Desktop nl -b a -n rz -w 3 test3
001 test1
002 hello test
003
004 test 3
005 test 4
006 新增
007 我再式下
008 test1
009 hello test
010
011 test 3
012 test 4
013 新增
014
015
016
017 test11
more
more命令與cat類似,cat是展示整個(gè)文件窝革,而more則是以一頁(yè)一頁(yè)的顯示方式來(lái)顯示內(nèi)容购城。在網(wǎng)上有一篇比較好的介紹more命令的文章more
- 分頁(yè)獲取 more -n file 分頁(yè)獲取
- 上下翻頁(yè) control+f 向下翻n行,control+b 向上翻一行
- 查看當(dāng)前行數(shù) 按下“=”鍵
- 查看user message 加上參數(shù)-d
- 不滾動(dòng)頁(yè)面虐译,直接對(duì)歷史數(shù)據(jù)進(jìn)行刪除 more -20 -c file可以直接只會(huì)直接清屏瘪板,不是進(jìn)行滾動(dòng)屏幕
- 按下enter,默認(rèn)加載往下加載一行( 差點(diǎn)把公司日志搞出去)
less
more命令是一次性加載完所有的記錄漆诽,二less在查看之前是不會(huì)加載整個(gè)文件侮攀。
- 分屏查看文件 less filename
- 查看文件并標(biāo)識(shí)行號(hào) less -N filename
- 讀取文件的百分比、行號(hào)及總行數(shù) less -M filename
在less后面有些后續(xù)操作厢拭,在網(wǎng)上有些比較好的介紹less
結(jié)合查看日志的情況兰英,進(jìn)行舉例說(shuō)明 - 打開(kāi)日志文件 less filename
- 定位到最后一行 直接按G鍵,定位到最后
- 向上翻頁(yè)查看記錄 control+f
這么多命令有時(shí)候壓根就記不住供鸠,所以就需要按下h來(lái)查看具體的幫助
head
head 命令顯示開(kāi)頭的幾行
- head filename 顯示默認(rèn)前10行:head test3
- head -n filename 顯示前 n行 head -15 test3
- head -n k filename 顯示前k行 head -n 15 test3
- head -c n filename 顯示文件前n個(gè)字節(jié) head -c 10 test3
head命令的重點(diǎn)是輸出文件前面的內(nèi)容
tail
與head對(duì)應(yīng)的命令就是tail 畦贸,查看文章最后幾行
- tail filename 默認(rèn)查看最后10行 tail test3
- tail -n filename 查看最后n行 tail -12 test3,如果為正數(shù)則是tail +12 test3 從test3的12行開(kāi)始用
- tail -f filename 不停讀取最新內(nèi)容,達(dá)到實(shí)時(shí)監(jiān)視的效果 tail -f test3楞捂,control +c 退出監(jiān)控
- tail -c filename 輸出最后的c字節(jié) tail -c 10 test3
- tail -f -n 指定刷新的行數(shù)tal -f -n 5 test3
現(xiàn)在head 與tail的命令都用了家制,所以可以聯(lián)合一起使用。比如
- head -n filename| tail -k 取filename前n行中的最后k行 比如head -20 test3|tail -5取的是test3中的前20行的最后5行
grep
grep為一個(gè)強(qiáng)查詢命令泡一,是查詢?nèi)罩境S妹?/p>
單個(gè)簡(jiǎn)單查詢
grep XX filename 檢索關(guān)鍵字 grep activityId=1929 citybuy.log 查詢?nèi)罩局兴邪?activityId=1929的日志
grep XX filename1 filename2在多個(gè)文件中檢索關(guān)鍵字
grep -v XX filename 不包含關(guān)鍵詞的行
grep ^XX filename 以指定模式開(kāi)頭行
grep -r XX /filename 遞歸查找颤殴,grep -r test /etc etc下面所有包行test都會(huì)查找出來(lái)
grep test$ finlename 以關(guān)鍵詞模式結(jié)尾的行
grep -n XX filename 檢索關(guān)鍵字并輸出行號(hào) grep activityId=1929 citybuy.log,這樣就可以知道具體的行號(hào)鼻忠,可以具體結(jié)合tail涵但,head命令查詢相關(guān)前后日志杈绸,或是直接使用grep -A,grep -B來(lái)查詢前后相關(guān)日志矮瘟,比如查到行號(hào)是100瞳脓,可以用 cat citybuy.log |tail -n +100 |head -n 20,從第100行起的前20行日志澈侠。
grep -c xx filename 檢索關(guān)鍵字出現(xiàn)的次數(shù)劫侧,grep -c activityId=1929 citybuy.log
grep -i XX file 不區(qū)分大小寫(xiě) grep -i activityId=1929 citybuy.log
grep -w XX file 匹配必須是整個(gè)單詞 grep -w activityId=1929 citybuy.log
-A,-B哨啃,-C檢索關(guān)鍵字并輸出前烧栋、后圣拄、前后n行完疫,grep -n activityId=1929 citybuy.log -C 5檢索關(guān)鍵字并輸出相應(yīng)的前后5行
grep -l XX file1 file2 file3 列出包含關(guān)鍵詞的文檔,可初步定位到內(nèi)容在哪個(gè)文件 grep -l '"branchId":8' citybuy.log citybuy.log.2018-07-02 結(jié)果是會(huì)列出包行branchId:8的文件旨椒,如果是L祝峻,則是未匹配到關(guān)鍵詞的文件
grep -f file1 file2 找出file2中存在的file1中的元素魔吐,如果有重復(fù)值也正常輸出,其實(shí)就類似與兩個(gè)文件中的交集莱找,同樣 grep -v -file1 file2 就是取在file2中不在file1中酬姆,有重復(fù)值時(shí)也全部不展示。有種取差集的概念奥溺。
多個(gè)查詢
或查詢
grep -E 'xx1|xx2' filename 在filename 中查詢包含關(guān)鍵字xx1或是xx2的記錄辞色,grep -c -E 'activityId=1929|"branchId":8' citybuy.log。
或者直接是使用egrep -c 'activityId=1929|"branchId":8' citybuy.log
或者使用grep -e activityId=1929 -e "branchId":8 filename
與查詢
grep XX1 filename |gep XX2 查找同時(shí)包含關(guān)鍵字xx1與xx2的行
例如 grep activityId=1929 citybuy.log|grep "branchId":8'找出日志中含有branchId為8且activityId為 1929的日志
grep -E 'pattern1.pattern2' filename grep -E 'activityId=1929."branchId":8' citybuy.log 這個(gè)是有順序的谚赎,先pattern1然后才是pattern2.
grep -E 'pattern1.pattern2|pattern2.pattern1' filename可以實(shí)現(xiàn)查詢包含pattern1淫僻,pattern2的文件。
正則表達(dá)式
在使用grep命令時(shí)經(jīng)常用到的就是正則表達(dá)式壶唤,在網(wǎng)上找到了一篇文章對(duì)此進(jìn)行了說(shuō)明grep 正則表達(dá)式
pattern正則表達(dá)式主要參數(shù):
\: 忽略正則表達(dá)式中特殊字符的原有含義雳灵。
^:匹配正則表達(dá)式的開(kāi)始行。
$: 匹配正則表達(dá)式的結(jié)束行闸盔。
\<:從匹配正則表達(dá)式的行開(kāi)始悯辙。
\>:到匹配正則表達(dá)式的行結(jié)束。
[ ]:?jiǎn)蝹€(gè)字符迎吵,如[A]即A符合要求 躲撰。
[ - ]:范圍,如[A-Z]击费,即A拢蛋、B、C一直到Z都符合要求 蔫巩。
.:所有的單個(gè)字符谆棱。
* :有字符快压,長(zhǎng)度可以為0。
其他操作
以上都是對(duì)grep的搜索功能進(jìn)行說(shuō)明垃瞧,有的時(shí)候我們是需要對(duì)日志進(jìn)行二次分析的蔫劣,所以會(huì)將過(guò)濾數(shù)據(jù)進(jìn)行存儲(chǔ)
cat -n info.log |grep "XX" >xxx.txt 將過(guò)濾出的日志進(jìn)行存儲(chǔ)
cat -n info.log |grep "XX" |more 內(nèi)容過(guò)多時(shí)進(jìn)行可以加more進(jìn)行分頁(yè)查看
awk
awk 命令是行處理器,對(duì)行的定義是/n來(lái)區(qū)分个从÷龃保可以對(duì)行進(jìn)行分割取值,然后獲取對(duì)應(yīng)的值嗦锐。通過(guò)查閱相關(guān)資料嫌松,有篇文章對(duì)此描述還不錯(cuò)awk 命令。awk 入門
分割打印基本使用
默認(rèn)是空格進(jìn)行分割意推,可用 -F‘ XX’進(jìn)行分割豆瘫,0是全部珊蟀,1是分割后得到的第一個(gè)參數(shù),$2是分割后得到的第二個(gè)參數(shù)菊值,以此類推,當(dāng)然還可以按一定格式進(jìn)行打印
? ~ cat test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
測(cè)試
我們不一樣
哈哈哈 育灸,我去
測(cè)試腻窒,哈哈
test , test
? ~ awk '{print $0,$1, $3}' test3
2 this is a tes 2 is
3 Are you like awk 3 you
This's a test This's test
10 there are orange,apple ,mongo 10 are
測(cè)試 測(cè)試
我們不一樣 我們不一樣
哈哈哈 ,我去 哈哈哈
測(cè)試磅崭,哈哈 測(cè)試儿子,哈哈
test , test test test
? ~ awk '{printf"%-8s %-10s\n", $1,$4}' test3
2 a
3 like
This's
10 orange,apple
測(cè)試
我們不一樣
哈哈哈
測(cè)試,哈哈
test
? ~ awk -F ',' '{print $1,$2}' test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange apple
測(cè)試
我們不一樣
哈哈哈 砸喻,我去
測(cè)試柔逼,哈哈
test test
內(nèi)建變量
內(nèi)建變量有多種,下面就些最基本的進(jìn)行測(cè)試割岛。
- FILENAME 文件名
- FNR 各文件分別計(jì)數(shù)的行號(hào)
- FS 字段分割符
- NF 一條記錄字段數(shù)目
- NR 已經(jīng)讀出的記錄數(shù)愉适,即行號(hào),從1開(kāi)始
awk 'BEGIN{printf "%4s %4s %4s %4s %4s \n","FILENAME","FNR","FS","NF","NR";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s \n",FILENAME,FNR,FS,NF,NR}' test3
FILENAME FNR FS NF NR
---------------------------------------------
test3 1 5 1
test3 2 5 2
test3 3 3 3
test3 4 5 4
test3 5 1 5
test3 6 1 6
test3 7 2 7
test3 8 1 8
test3 9 3 9
正則匹配
字符匹配有2種
- 純字符直接匹配 //純字符匹配 癣漆!//純字符不匹配
- 字段值匹配 ~//字段值匹配 维咸!~//字段值不匹配
? ~ awk '/are/' test3
10 there are orange,apple ,mongo
? ~ awk !'/are/' test3
2 this is a tes
3 Are you like awk
This's a test
測(cè)試
我們不一樣
哈哈哈 ,我去
測(cè)試惠爽,哈哈
test , test
? ~ awk '$2 ~/Are|this/ {print $0}' test3
2 this is a tes
3 Are you like awk
? ~ awk '$2 !~/Are|this/ {print $0}' test3
This's a test
10 there are orange,apple ,mongo
測(cè)試
我們不一樣
哈哈哈 癌蓖,我去
測(cè)試,哈哈
test , test
條件表達(dá)式
== 婚肆!= > >=用于過(guò)濾
~ awk '$1==3||$1=="測(cè)試"' test3
3 Are you like awk
測(cè)試
? ~ awk '$1租副!=3||$1!="測(cè)試"' test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
測(cè)試
我們不一樣
哈哈哈 较性,我去
測(cè)試用僧,哈哈
test , test
? ~ awk '$1>3' test3
This's a test
10 there are orange,apple ,mongo
測(cè)試
我們不一樣
哈哈哈 讨越,我去
測(cè)試,哈哈
test , test
邏輯運(yùn)算符
&& || 多條件過(guò)濾
? ~ cat test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
測(cè)試
我們不一樣
哈哈哈 永毅,我去
測(cè)試把跨,哈哈
test , test
test1 test2 hahah
test1 test3 go
test test
? ~ awk '$1 ~/test/||$2~/test2/' test3
test , test
test1 test2 hahah
test1 test3 go
test test
? ~ awk '$1 ~/test/&&$2!~/test2/' test3
test , test
test1 test3 go
test test
條件語(yǔ)句
if語(yǔ)句與while 語(yǔ)句
? ~ awk 'BEGIN {A=1}{while (A<NF) print NF,$0 ,A++}' test3
5 2 this is a tes 1
5 2 this is a tes 2
5 2 this is a tes 3
5 2 this is a tes 4
? ~ awk 'BEGIN {A=0;B=0}{if ($1>10){ A++} else{B++}} END {print A,"\t"B}' test3
9 3
? ~ awk 'BEGIN {A=1}{while (A<NF) print NF,$0 ,A++}' test3
5 2 this is a tes 1
5 2 this is a tes 2
5 2 this is a tes 3
5 2 this is a tes 4
之所以學(xué)習(xí)以上幾個(gè)命令,其實(shí)是源于日志查詢的需要沼死。
對(duì)某個(gè)字段出現(xiàn)次數(shù)進(jìn)行統(tǒng)計(jì)
~ awk -F ' ' '{sum[$1]++} END{for (i in sum) print i "\t" sum[i]}' test3
2 1
3 1
哈哈哈 1
This's 1
測(cè)試 1
10 1
我們不一樣 1
測(cè)試着逐,哈哈 1
test1 2
test 2
sum[$i] 為對(duì)第一列中字符出現(xiàn)的次數(shù)進(jìn)行統(tǒng)計(jì)
grep與awk 結(jié)合使用
zgrep 'com.gexin.rp.base.log.LogManager.slow' rp-slow-20171212-0.log.gz |awk -F':' '{print($1)}'|sort|uniq -c|sort -t' ' -k1n
以上命令的意思就是包含 查找包含com.gexin.rp.base.log.LogManager.slow每行日志,按照: 分域后取第一個(gè)域的值意蛀,并計(jì)算對(duì)應(yīng)的的總數(shù)按照統(tǒng)計(jì)值從大到小排列
zgrep 'save_list_body|QX3GgC1oXf9IYWX1LCPVE8' *rp-message-20171211* | awk -F'|' '{print($6)}' | awk -F'=' '{print($5)}'|sort| uniq -c | more
zgrep 'QX3GgC1oXf9IYWX1LCPVE8' *rp-bi-20180105* |grep 'RASL'|grep '2018-01-05 '|wc -l
sort 命令補(bǔ)充
cat file1.txt file2.txt | sort | uniq > file.txt 并集
cat file1.txt file2.txt | sort | uniq -d >file.txt 交集
差集:求file1.txt相對(duì)于file2.txt的差集耸别,可先求出兩者的交集temp.txt,然后在file1.txt中除去temp.txt即可县钥。
cat file1.txt file2.txt | sort | uniq -d >temp.txt
cat file1.txt temp.txt | sort | uniq -u >file.txt
自己在網(wǎng)上查到一篇關(guān)于sort命令sort命令