您好,欢迎访问一九零五行业门户网

Linux awk 命令

awk是一种处理文本文件的语言,是一个强大的文本分析工具。
之所以叫awk是因为其取了三位创始人 alfred aho,peter weinberger, 和 brian kernighan 的family name的首字符。
语法
awk [选项参数] 'script' var=value file(s) 或 awk [选项参数] -f scriptfile var=value file(s)
选项参数说明:
-f fs or --field-separator fs
指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-f:。
-v var=value or --asign var=value
赋值一个用户定义变量。
-f scripfile or --file scriptfile
从脚本文件中读取awk命令。
-mf nnn and -mr nnn
对nnn值设置内在限制,-mf选项限制分配给nnn的最大块数目;-mr选项限制记录的最大数目。这两个功能是bell实验室版awk的扩展功能,在标准awk中不适用。
-w compact or --compat, -w traditional or --traditional
在兼容模式下运行awk。所以gawk的行为和标准的awk完全一样,所有的awk扩展都被忽略。
-w copyleft or --copyleft, -w copyright or --copyright
打印简短的版权信息。
-w help or --help, -w usage or --usage
打印全部awk选项和每个选项的简短说明。
-w lint or --lint
打印不能向传统unix平台移植的结构的警告。
-w lint-old or --lint-old
打印关于不能向传统unix平台移植的结构的警告。
-w posix
打开兼容模式。但有以下限制,不识别:/x、函数关键字、func、换码序列以及当fs是一个空格时,将新行作为一个域分隔符;操作符**和**=不能代替^和^=;fflush无效。
-w re-interval or --re-inerval
允许间隔正则表达式的使用,参考(grep中的posix字符类),如括号表达式[[:alpha:]]。
-w source program-text or --source program-text
使用program-text作为源代码,可与-f命令混用。
-w version or --version
打印bug报告信息的版本。
基本用法
log.txt文本内容如下:
2 this is a test 3 are 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 --------------------------------------------- 2 this is a test 3 are 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 10 there are orange apple # 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割 $ awk -f '[ ,]' '{print $1,$2,$5}' log.txt --------------------------------------------- 2 this test 3 are awk this's a 10 there apple
用法三:
awk -v # 设置变量
实例:
$ awk -va=1 '{print $1,$1+a}' log.txt --------------------------------------------- 2 3 3 4 this's 1 10 11 $ awk -va=1 -vb=s '{print $1,$1+a,$1b}' log.txt --------------------------------------------- 2 3 2s 3 4 3s this's 1 this'ss 10 11 10s
用法四:
awk -f {awk脚本} {文件名}
实例:
$ awk -f cal.awk log.txt
运算符
过滤第一列大于2的行
$ awk '$1>2' log.txt #命令 #输出 3 are you like awk this's a test 10 there are orange,apple,mongo
过滤第一列等于2的行
$ awk '$1==2 {print $1,$3}' log.txt #命令 #输出 2 is
过滤第一列大于2并且第二列等于'are'的行
$ awk '$1>2 && $2=="are" {print $1,$2,$3}' log.txt #命令 #输出 3 are you
内建变量
$ awk 'begin{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","filename","argc","fnr","fs","nf","nr","ofs","ors","rs";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",filename,argc,fnr,fs,nf,nr,ofs,ors,rs}' log.txt filename argc fnr fs nf nr ofs ors rs --------------------------------------------- log.txt 2 1 5 1 log.txt 2 2 5 2 log.txt 2 3 3 3 log.txt 2 4 4 4 $ awk -f\' 'begin{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","filename","argc","fnr","fs","nf","nr","ofs","ors","rs";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",filename,argc,fnr,fs,nf,nr,ofs,ors,rs}' log.txt filename argc fnr fs nf nr ofs ors rs --------------------------------------------- log.txt 2 1 ' 1 1 log.txt 2 2 ' 1 2 log.txt 2 3 ' 2 3 log.txt 2 4 ' 1 4 # 输出顺序号 nr, 匹配文本行号 $ awk '{print nr,fnr,$1,$2,$3}' log.txt --------------------------------------------- 1 1 2 this is 2 2 3 are you 3 3 this's a test 4 4 10 there are # 指定输出分割符 $ awk '{print $1,$2,$5}' ofs=" $ " log.txt --------------------------------------------- 2 $ this $ test 3 $ are $ awk this's $ a $ 10 $ there $
使用正则,字符串匹配
# 输出第二列包含 "th",并打印第二列与第四列 $ awk '$2 ~ /th/ {print $2,$4}' log.txt --------------------------------------------- this a
~ 表示模式开始。// 中是模式。
# 输出包含"re" 的行 $ awk '/re/ ' log.txt --------------------------------------------- 3 are you like awk 10 there are orange,apple,mongo
忽略大小写
$ awk 'begin{ignorecase=1} /this/' log.txt --------------------------------------------- 2 this is a test this's a test
模式取反
$ awk '$2 !~ /th/ {print $2,$4}' log.txt --------------------------------------------- are like a there orange,apple,mongo $ awk '!/th/ {print $2,$4}' log.txt --------------------------------------------- are like a there orange,apple,mongo
awk脚本
关于awk脚本,我们需要注意两个关键词begin和end。
begin{ 这里面放的是执行前的语句 }
end {这里面放的是处理完所有的行后要执行的语句 }
{这里面放的是处理每一行时要执行的语句}
假设有这么一个文件(学生成绩表):
$ cat score.txt marry 2143 78 84 77 jack 2321 66 78 45 tom 2122 48 77 71 mike 2537 87 97 95 bob 2415 40 57 62
我们的awk脚本如下:
$ cat cal.awk #!/bin/awk -f #运行前 begin { math = 0 english = 0 computer = 0 printf "name no. math english computer total\n" printf "---------------------------------------------\n" } #运行中 { math+=$3 english+=$4 computer+=$5 printf "%-6s %-6s %4d %8d %8d %8d\n", $1, $2, $3,$4,$5, $3+$4+$5 } #运行后 end { printf "---------------------------------------------\n" printf " total:%10d %8d %8d \n", math, english, computer printf "average:%10.2f %8.2f %8.2f\n", math/nr, english/nr, computer/nr }
我们来看一下执行结果:
$ awk -f cal.awk score.txt name no. math english computer total --------------------------------------------- marry 2143 78 84 77 239 jack 2321 66 78 45 189 tom 2122 48 77 71 196 mike 2537 87 97 95 279 bob 2415 40 57 62 159 --------------------------------------------- total: 319 393 350 average: 63.80 78.60 70.00
另外一些实例
awk的hello world程序为:
begin { print "hello, world!" }
计算文件大小
$ ls -l *.txt | awk '{sum+=$6} end {print sum}' -------------------------------------------------- 666581
从文件中找出长度大于80的行
awk 'lenght>80' log.txt
打印九九乘法表
seq 9 | sed 'h;g' | awk -v rs='' '{for(i=1;i<=nf;i++)printf("%dx%d=%d%s", i, nr, i*nr, i==nr?"\n":"\t")}'
更多linux awk 命令。
其它类似信息

推荐信息