码迷,mamicode.com
首页 > 其他好文 > 详细

awk记录-1

时间:2018-03-27 18:49:39      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:awk

a. 准备工作 marks.txt 本文件有4列
1)    Amit     Physics    80
2)    Rahul    Maths      90
3)    Shyam    Biology    87
4)    Kedar    English    85
5)    Hari     History    89

marks2.txt 本文件有部分列有5列

1)    Andy     Physics    80
2)    Jacky    Maths      90    10
3)    Hill    Biology    87
4)    John    English    85 12
5)    Mary     History

b. 样例

  • (1).文本加标题行 "序号 姓名 科目 分数" 显示:
    >awk ‘BEGIN {printf "序号\t姓名\t科目\t分数\n"} {print}‘ marks.txt 
    序号  姓名  科目  分数
    1)    Amit     Physics    80
    2)    Rahul    Maths      90
    3)    Shyam    Biology    87
    4)    Kedar    English    85
    5)    Hari     History    89
  • (2).执行awk脚本:将单引号内的脚本写到一个文件,然后通过 -f 引用文件执行:
    
    > cat command.awk 
    BEGIN {printf "序号\t姓名\t科目\t分数\n"} {print}

awk -f command.awk marks.txt
序号 姓名 科目 分数
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89

- (3). `-v`声明一个变量的值,BEGIN前分配 

awk -v user=root ‘BEGIN{printf "name=%s\n", user}‘ `
name=root

- (4).  计算:   + - * / %

awk ‘BEGIN {a=52; b=50; print "a%b=", (a%b)}‘ `
a%b= 2

- (5).  BEGIN END 定义变量a,在处理行的过程中增加(可以与行内的内容进行计算),END时输出:(计算总分数)

awk ‘BEGIN { sumScore = 0; printf "总分数sumScore = %d\n", a } {sumScore=sumScore+$NF;print} END{print ">总分数sumScore=",sumScore}‘ marks.txt`
总分数sumScore = 0
1) Amit Physics 80
2) Rahul Maths 90
3) Shyam Biology 87
4) Kedar English 85
5) Hari History 89
总分数sumScore= 431


![疲劳的分割线](http://upload-images.jianshu.io/upload_images/3858200-38e21a294ccf66ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

awk内置变量: NR FNR NF

NR = Number of Records
NF = Number of Fields
FNR = File Number of Records
下面摘自: The GNU Awk User’s Guide

awk divides the input for your program into records and fields. It keeps track of the number of records that have been read so far from the current input file. This value is stored in a predefined variable called FNR, which is reset to zero every time a new file is started. Another predefined variable, NR, records the total number of input records read so far from all data files. It starts at zero, but is never automatically reset to zero.
......
The value of the built-in variable NF is the number of fields in the current record.

翻译与小结
awk将程序的输入分为记录和字段。
它跟踪从当前输入文件到目前为止已经读取的记录数量。

FNR 该值存储在一个名为FNR的预定义变量中,每启动一个新文件时,该变量将重置为零。【每行的行号-单个文件

NR 另一个预定义变量NR记录从所有数据文件中读取的输入记录总数。 它从零开始,但 从不 自动重置为零。【每行的行号-全部文件

NF 内置变量NF的值是当前记录中的字段数。在执行块中$NF可以得到当前行末字段的值

  • (6). 打印全局行号 当前行号 行内列数 行末字段值
    
    > awk ‘BEGIN{print "全局行号\t当前行号\t行内列数\t行末字段值"} {print NR,"\t", FNR,"\t", NF,"\t", $NF}‘ marks.txt marks2.txt

全局行号 当前行号 行内列数 行末字段值
1 1 4 80
2 2 4 90
3 3 4 87
4 4 4 85
5 5 4 89
6 1 4 80
7 2 5 10
8 3 4 87
9 4 5 12
10 5 3 History


![疲劳分割线](http://upload-images.jianshu.io/upload_images/3858200-1ff80eb81fe3ad88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

awk记录-1

标签:awk

原文地址:http://blog.51cto.com/coffee/2091730

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!