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

awk技巧

时间:2014-08-10 13:03:40      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   for   2014   art   ar   

1通过awk脚本运行awk程序:awk-f program_file_name input_files

#!/bin/awk -f
BEGIN
{
    print "What is your name,sir?";
    while(getline>0)
        print "Hi",$1,"Nice to meet you!"
}

2 FILENAME, FNR

awk ‘{print FILENAME, FNR;}‘ student-marks bookdetails

student-marks 1
student-marks 2
student-marks 3
student-marks 4
student-marks 5
bookdetails 1
bookdetails 2
bookdetails 3
bookdetails 4
bookdetails 5

3 倒序输出文本 

awk '{line[NR]=$0} END{i=NR;while(i>0) {print line[i];i=i-1}} ' readme.tx

4 获取给定月份的最后一个星期六的日期

cal 04 2012 | awk '{ print $7 }' | grep -v "^$" | tail -1

5 重要的变量

NR:Nubmer of records

FNR:Number of records(different with files)

NF:Number of fields,separate by FS

FS:Field separator, space or Tab by default

OFS: Output field separator, space by default

ORS: Output record separator,same as RS

6 丰富的内置函数

gsub(r,s) substitute s for r globally in $0 return number of substitution made


更多内容:

1http://coolshell.cn/articles/9070.html

2http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

3http://web.cs.du.edu/~dconnors/courses/comp2400/notes/AWK.pdf

awk技巧,布布扣,bubuko.com

awk技巧

标签:style   blog   http   io   for   2014   art   ar   

原文地址:http://blog.csdn.net/ithiker/article/details/38467323

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