【Shell】【經驗】awk sed grep find sort常用配搭用法

secooler發表於2009-02-21
作為一位DBA,不熟練的掌握Shell是萬萬不可的,簡單列一些常用的Shell命令的用法。

awk '/[Gg]reen/' file

awk '$1 ~/^…a/' file
awk '$0 ~/(Green|green)/' file #等價於egrep "Green|green" file
awk '{print $1}' file | tee file2
awk '{if ($4 ~/Brown/) print $0}' file
awk '{if ($4 !~/Brown/) print $0}' file
awk '{if($1=="條件1" && $4=="條件2") print $0}' file
awk '{if($1=="條件1" || $4=="條件2") print $0}' file
awk '{print length}' file
awk 'BEGIN {print "this is a title"} {print $1} END{print "the end"}' read.txt
awk 'END {print NR}' file #等價於wc –l file
awk 'END {print NF}' file
awk 'END {print FILENAME}' file

sed –n '/e\{2\}/'p file1
sed –n '/e\{2,\}/'p file1
sed –n '/e\{2,3\}/'p file1
sed 's/Tony/Terry/'g file1

grep –w "精確匹配條件" file
grep –i "匹配字串" file
grep –v "匹配字串" file
grep –E "條件1|條件2" file
grep [^字串] file


find . –name "*.sh" | xargs grep expr
find ~ –mtime 2
find / –size 0c
find ~/app –type d #列出根目錄下的app目錄中所包含的所有資料夾

sort +4n file1
sort –u file

-- The End --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-557309/,如需轉載,請註明出處,否則將追究法律責任。

相關文章