awk命令使用示例

#1---awk命令的基本格式,   
#options :表示可选参数  
#program :表示awk的可执行脚本代码  
#file : 表示需要处理的文件  
awk [options] 'program' file  
  
#2---逐行打印  
awk '{print $0}' score.txt  
(1) Amit : Physics 80
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89
  
#3---指定':'为分隔符,awk默认的分隔符为空格和制表符  
awk -F ':' '{print $2}' /etc/passwd  
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp  
  
#指定多个分隔符,'[:)]'  
awk -F '[:()]' '{print $1,$2,$3,$4}' score.txt  
 1  Amit   Physics 80
 2  Rahul   Maths 90
 3  Shyam   Biology 87
 4  Kedar   English 85
 5  Hari   History 89  
  
#打印每行第一项,第二项...  
awk '{print $1,$2,$3,$4,$5}' score.txt  
(1) Amit : Physics 80
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89  
  
#加入分隔符  
 awk '{print $1,"\n",$2,"\n",$3,"\n",$4,"\n",$5}' score.txt
(1) 
 Amit 
 : 
 Physics 
 80
(2) 
 Rahul 
 : 
 Maths 
 90
(3) 
 Shyam 
 : 
 Biology 
 87
(4) 
 Kedar 
 : 
 English 
 85
(5) 
 Hari 
 : 
 History 
 89
  
#4---awk 内置变量的使用  
   
#$0 这个表示文本处理时的当前行  
  
#$1 表示文本行被分隔后的第 1 个字段列  
  
#$2 表示文本行被分割后的第 2 个字段列  
  
#$3 表示文本行被分割后的第 3 个字段列  
  
#$n 表示文本行被分割后的第 n 个字段列  
  
#NR 表示文件中的行号,表示当前是第几行  
  
#NF 表示文件中的当前行列的个数,类似于 mysql 数据表里面每一条记录有多少个字段  
  
#FS 表示 awk 的输入分隔符,默认分隔符为空格和制表符,你可以对其进行自定义设置  
  
#OFS 表示 awk 的输出分隔符,默认为空格,你也可以对其进行自定义设置  
  
#FILENAME 表示当前文件的文件名称,如果同时处理多个文件,它也表示当前文件名称  
  
  
  
  
#打印行号,内置变量NR表示行号  
awk '{print NR , $1}' score.txt  
1 (1)
2 (2)
3 (3)
4 (4)
5 (5)  
  
#内置变量NF表示每一行的列数  
awk '{print NF}' score.txt  
5
5
5
5
5  
  
#内置变量NF的使用示例,打印最后一列  
awk '{print $NF,$(NF-1)}' score.txt 
80 Physics
90 Maths
87 Biology
85 English
89 History  
  
  
#指定输出分隔符为"**"  
awk -F ':' '{print $1,$2,$3,$4,OFS="**"}' score.txt   
(1) Amit ** Physics 80********
(2) Rahul ** Maths 90********
(3) Shyam ** Biology 87********
(4) Kedar ** English 85********
(5) Hari ** History 89********  
  
  
#5---同时处理多个文件  
awk '{print $0}' score.txt, students.txt  
(1) Amit : Physics 80
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89
linux
java
python
maven
git
shell  


#6---BEGIN关键字 : 在开始读取一个文件之前,运行一次 BEGIN后面的脚本,只执行一次
awk 'BEGIN{print "Hello World!"} {print $0}' score.txt 
Hello World!
(1) Amit : Physics 80
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89

#7---END关键字 : END 指令和 BEGIN 恰好相反,在 awk 读取并且处理完文件的所有内容行之后,才会执行 END 后面的脚本代码段
awk 'BEGIN {print "hello world!\n"} END {print "\nGood bye....."} {print $1,$2}' score.txt
Hello World!
(1) Amit : Physics 80
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89
Good bye...... 

awk 'BEGIN{print "Hello World!"} {print $0} END{print "Good bye......."} {print $0}' score.txt 
Hello World!
(1) Amit : Physics 80
(1) Amit : Physics 80
(2) Rahul : Maths 90
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(3) Shyam : Biology 87
(4) Kedar : English 85
(4) Kedar : English 85
(5) Hari : History 89
(5) Hari : History 89
Good bye.......

#8---awk使用变量,注意分割符号';'
awk '{msg="hello world!";print msg}' score.txt
awk 'BEGIN{msg="hello world!";print msg}' score.txt
awk 'BEGIN{msg="hello world!"} {print msg}' score.txt
awk 'BEGIN{msg="hello"} {print msg, $0}' score.txt 
hello (1) Amit : Physics 80
hello (2) Rahul : Maths 90
hello (3) Shyam : Biology 87
hello (4) Kedar : English 85
hello (5) Hari : History 89

awk 'BEGIN{a=1;b=2} {print a,"+",b,"=",a+b}' score.txt
1 + 2 = 3
1 + 2 = 3
1 + 2 = 3
1 + 2 = 3
1 + 2 = 3

#9---awk使用条件判断
awk '$5>80 {print $0}' score.txt
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89

awk '{if($5>80) print $0}' score.txt
(2) Rahul : Maths 90
(3) Shyam : Biology 87
(4) Kedar : English 85
(5) Hari : History 89

#10---awk中使用正则表达式
awk '/Maths/{print $0}' score.txt
(2) Rahul : Maths 90



#11---执行awk脚本
awk -f calculate.awk score.txt 

#!/usr/bin/awk 
#before calculate
BEGIN{
	math=0;
	english=0
	computer=0
	history=0
	printf"----------------------------------------------------------------------------\n"
	printf("%-10s %10s %10s %10s %10s %10s %10s\n","Name","NO.","Math","English","Computer","History","Total")
	printf"----------------------------------------------------------------------------\n"
}
#calculating
{
	math+=$3
	english+=$4
	computer+=$5
	history+=$6
	printf("%-10s %10s %10d %10d %10d %10d %10d\n",$1,$2,$3,$4,$5,$6,$3+$4+$5+$6)
}

#after calculating
END{
	printf"----------------------------------------------------------------------------\n"
	printf("%-21s %10d %10d %10d %10d\n","Total",math,english,computer,history)	
	printf("%-21s %10.2f %10.2f %10.2f %10.2f\n","Average",math/NR,english/NR,computer/NR,history/NR)	
	}

执行结果如下:

----------------------------------------------------------------------------
Name              NO.       Math    English   Computer    History      Total
----------------------------------------------------------------------------
Amit             1001         80         90         78        100        348
Rahul            1002         90         89         87         89        355
Shyam            1003         87         78         96         94        355
Kedar            1004         85         85         89         83        342
Hari             1005         69         94         78         90        331
----------------------------------------------------------------------------
Total                        411        436        428        456
Average                    82.20      87.20      85.60      91.20


 #12---拆分文件(使用重定向):例如,按照每个学生的姓名,输出该学生的成绩到一个单独的文件
awk '{print >$1}' score.txt  
执行之后,会按照姓名输出文件
ls
Amit   Hari  history.txt  Kedar  Rahul  score.txt  Shyam  

#13---找出文件中长度大于100的行    
awk 'length>100' calculate.awk   







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值