Bash常用命令總結
date:2019-05-02
grep
查找字符串
- ‘-r’:目錄查找,
grep -r "sample" .
- ‘-c’:顯示匹配數,
grep -c "sample" .
- ‘-i’:忽略大小寫
- ‘-n’:輸出行號
xargs
xargs將stdin的資料讀入,並以空白字元或者斷行字元作爲分辨,將stdin資料分隔爲arguments,其默認的命令爲echo,如:
echo "\t" | xargs
t
- ‘-n’ 選擇多行輸出
- ‘-d’ 選擇定界符
- ‘-I’ 佔位符,可以指定字符串進行替換
- ‘-P’ 可以進行並行操作
sed
sed是非交互的編輯器,不修改原始文件,而是通過將原始文件存儲到臨時緩存區中進行修逐行處理,每處理一行就刪除一行並將結果輸出到屏幕上。sed命令可以通過定址的方式對文件進行編輯。定址的形式有數字,正則表達式,或者兩者的結合。沒有定址的話默認輸出全部。
常見的命令:
- 打印:
p :
cat test.output
# [layer1],[Forward Timer],__conv__,3.108
# [layer2],Forward Timer],__conv__,3.17
# [layer2],Forward Timer],__conv__,13.17
sed -n '$p' test.output
# 打印出最後一行
# -n表示只把符合要求的行進行打印,否則會把所有行以及符合要求都打印出來,即符合要求的行會被打印兩遍
# [layer2],Forward Timer],__conv__,13.17
sed -n '/\[layer2/p' test.output
# 把包含[layer2的行打印出來,[需要\進行轉義
# [layer2],Forward Timer],__conv__,3.17
# [layer2],Forward Timer],__conv__,13.17
sed -n '/\[layer1/,2p' test.output
# 輸出包含[layer1的行到第二行
# [layer1],[Forward Timer],__conv__,3.108
# [layer2],Forward Timer],__conv__,3.17
- 刪除:
d:
cat test.output
# [layer1],[Forward Timer],__conv__,3.108
# [layer2],Forward Timer],__conv__,3.17
# [layer2],Forward Timer],__conv__,13.17
sed '$d' test.output
# 刪除最後一行
# [layer1],[Forward Timer],__conv__,3.108
# [layer2],Forward Timer],__conv__,3.17
sed '/\[layer2/d' test.output
# 把包含[layer2的行刪除
# [layer1],[Forward Timer],__conv__,3.108
sed -n '/\[layer1/,2d' test.output
# 刪除包含[layer1的行到第二行
# [layer2],Forward Timer],__conv__,13.17
- 替換:
s:默認是對每行搜索到的第一個符合要求的字符進行匹配
cat test.output
# [layer1],[Forward Timer],__conv__,3.108
# [layer2],[layer2],Forward Timer],__conv__,3.17
# [layer2],Forward Timer],__conv__,13.17
sed 's/\[layer2]/Zheng/' test.output
# 僅對行內第一個匹配到的字符進行替換
# [layer1],[Forward Timer],__conv__,3.108
# Zheng,[layer2],Forward Timer],__conv__,3.17
# Zheng,Forward Timer],__conv__,13.17
sed 's/\[layer2]/Zheng/g' test.output
# 代表對行內所有匹配得打的字符進行替換
# [layer1],[Forward Timer],__conv__,3.108
# Zheng,Zheng,Forward Timer],__conv__,3.17
# Zheng,Forward Timer],__conv__,13.17
sed -n '1,2s/[1-9]$/Zheng/p' test.output
# 處理1-2行,對一個數字結尾的行,將數字替換成Zheng,並且打印輸出
# [layer1],[Forward Timer],__conv__,3.10Zheng
# [layer2],[layer2],Forward Timer],__conv__,3.1Zheng
sed -n '1,2s/\([1-9]\)$/Zheng\1/p' test.output
# \1爲匹配符,能夠匹配前面字符內容,在該基礎上加上內容,但需要將前面的匹配的內容通過\(\)括號括出來
# [layer1],[Forward Timer],__conv__,3.10Zheng8
# [layer2],[layer2],Forward Timer],__conv__,3.1Zheng7
sed -n '1,2s/\(\.[0-9]\)/\1zheng/p' test.output
# 同上,只是在後面添加內容
# [layer1],[Forward Timer],__conv__,3.1zheng08
# [layer2],[layer2],Forward Timer],__conv__,3.1zheng7
- 其他常用修飾符
- ^:行首定位符
- $:行尾定位符
- .:匹配除換行以外的單個字符
- *:匹配零個或者多個前導字符,常與.配合使用.*表示任意多個字符
- []:匹配指定字符組內的任一字符,常見用法:[0-9],[a-zA-Z]
- x\{m\}:表示連續m個x
- x\{m,\}:表示至少連續m個x
- -i:直接對文本進行編輯
awk
awk命令的基本格式爲 =awk ‘條件1 {動作1} 條件2 {動作2} …’ 文件名
awk保留字:BEGIN,awk程序開始時,尚未讀取任何數據之前執行,BEGIN後的動作只在程序開始時執行一次;END,在awk程序處理完所有數據,即將結束時執行,END後動作只在程序結束時執行一次。
awk只要檢測不到完整的單引號就不會執行。
cat test.output
# [layer1] [backward_Timer] __conv__ 3.108
# [layer2] [Forward_Timer] __conv__ 19.17
# [layer3] [backward_Timer] __conv__ 13.17
# [layer3] [Forward_Timer] __conv__ 9.17
awk 'BEGIN {print "This is just for test!"} {print $2}' test.output
# awk比cut更加智能,cut指令不能很好地識別空格間隔,而awk則對空格和製表符都能很好的識別
# BEGIN爲執行命令前輸出
# This is just for test!
# [backward_Timer]
# [Forward_Timer]
# [Forward_Timer]
awk 'END {print "Just print layer2 and score>10"} $4>10 && /Forward_Timer/ {print }' test.output
# END 爲執行命令結束後輸出
# awk指令支持正則,需要將正則指令通過//框出
# &&表示多重與指令,或操作爲||
# [layer2] [Forward_Timer] __conv__ 19.17
# Just print layer2 and score>10
awk '{printf "row:%d,column:%d\n",NR,NF}' test.output
# NR:內置變量,爲處理的行數
# NF:內置變量,爲處理的列數
# %d爲格式話輸出,需要用printf,和print的區別是可以格式化輸出,且默認無換行
# row:1,column:4
# row:2,column:4
# row:3,column:4
# row:4,column:4
awk '/Forward_Timer/ {sum += $4;i++} END {printf "%.2f\n",sum/i}' test.output
# 多個命令需要;連接
# 14.17
find
最常用的通過名字進行查找文件(支持正則):
find . -name "*.txt"
# .爲當前目錄,*.txt爲所有txt文件
# ./test.txt
# ./log.txt
其他查找模式:
- -perm:權限,如755,主可讀可寫可執行,其他可讀可執行
- -user:用戶名,如find . -user zheng
- -size:大小,如find . -size +1000000c,大於1M的文件
- -depth:遞歸子目錄,如find . -name “*.txt” -depth