# 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 [选项参数]'script'var=value file(s)或 awk [选项参数]-f scriptfile var=value file(s)
log.txt文本内容如下:
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相当于内置变量FS, 指定分割字符
实例:
# 使用","分割 $ awk -F,'{print $1,$2}' log.txt ---------------------------------------------2thisis a test 3Are you like awk This's a test 10 There are orange apple # 或者使用内建变量 $ 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 # 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割 $ awk -F '[ ,]''{print $1,$2,$5}' log.txt ---------------------------------------------2this test 3Are awk This's a 10 There apple
用法三:
awk -v # 设置变量
实例:
$ 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文本处理常用命令总结