# du -h test608K test/test6 308K test/test4 4.0K test/scf/lib 4.0K test/scf/service/deploy/product 4.0K test/scf/service/deploy/info 12K test/scf/service/deploy 16K test/scf/service 4.0K test/scf/doc 4.0K test/scf/bin 32K test/scf 8.0K test/test3 1.3M test
【awk命令】
AWK是一種處理文本文件的語言,是一個強大的文本分析工具。
awk [選項參數(shù)]'script'var=value file(s)或 awk [選項參數(shù)]-f scriptfile var=value file(s)
log.txt文本內(nèi)容如下:
2thisis a test 3Are you like awk This's a test 10 There are orange,apple,mongo
用法一:
awk '{[pattern] action}'{filenames}# 行匹配語句 awk '' 只能用單引號
實例:
# 每行按空格或TAB分割,輸出文本中的1、4項 $ awk '{print $1,$4}' log.txt ---------------------------------------------2 a 3 like This's 10 orange,apple,mongo # 格式化輸出 $ awk '{printf "%-8s %-10s\n",$1,$4}' log.txt --------------------------------------------- 2 a 3 like This's 10 orange,apple,mongo
用法二:
awk -F #-F相當于內(nèi)置變量FS, 指定分割字符
實例:
# 使用","分割 $ awk -F,'{print $1,$2}' log.txt ---------------------------------------------2thisis a test 3Are you like awk This's a test 10 There are orange apple # 或者使用內(nèi)建變量 $ awk 'BEGIN{FS=","}{print $1,$2}' log.txt --------------------------------------------- 2 this is a test 3 Are you like awk This's a test 10There are orange apple # 使用多個分隔符.先使用空格分割,然后對分割結(jié)果再使用","分割 $ awk -F '[ ,]''{print $1,$2,$5}' log.txt ---------------------------------------------2this test 3Are awk This's a 10 There apple
用法三:
awk -v # 設(shè)置變量
實例:
$ awk -va=1'{print $1,$1+a}' log.txt ---------------------------------------------2334This's 1 10 11 $ awk -va=1 -vb=s '{print $1,$1+a,$1b}' log.txt --------------------------------------------- 2 3 2s 3 4 3s This's 1This'ss 10 11 10s
上一個教程:如何學習linux命令之ls的使用
下一個教程:[文件]Linux文本處理常用命令總結(jié)