`

awk应用

F# 
阅读更多

a.txt
andy    05/99   48311   Green   8       40      44
congfeng        06/99   4317    green   9       24      26
ljb     02/99   48      Yellow  12      35      28
carl    07/99   4842    Brown-3 12      16      26
rich    05/99   4712    Brown-2 12      30      28


[root@localhost shell]# awk '{ print $0 }' a.txt
[root@localhost shell]# awk '{ print $1,$4}' a.txt

#单独使用begin/BEGIN都可以,但是要使用begin-end的方式。必须全部大写:BEGIN-END
[root@localhost shell]# awk 'BEGIN {print "hello, andylin!\nName\tBelt\n"}
[root@localhost shell]# awk 'begin {print "hello,andylin!\nName\tBelt"} {print $1, $4} 

END {print"end of awk"} ' a.txt

[root@localhost shell]# awk '{if ($4 ~ /Brown/ ) print $0}' a.txt

[root@localhost shell]# awk '$3=="48" {print $0}' a.txt
[root@localhost shell]# awk '$0 !~ /Brown/' a.txt
[root@localhost shell]# awk '{if($0 !~ /Brown/) print$0}' a.txt
[root@localhost shell]# awk '{if ($4 !="Brown-2") print$0}' a.txt
[root@localhost shell]# awk '{if($6<$7) print$0} ' a.txt

[root@localhost shell]# awk '{if($6 <= $7) print $0}' a.txt



[root@localhost shell]# awk '/[Gg]reen/' a.txt

[root@localhost shell]# awk '{if($0~/[Gg]reen/) print $0}' a.txt

[root@localhost shell]# awk '{if ($1=="andy" && $4=="Green") print $0}' a.txt
[root@localhost shell]# awk '{if ($1=="andy" || $4=="Green") print $0}' a.txt

awk 'BEGIN {print ARGC,"_" FS, "_"} {print NF,NR} ' a.txt


awk 'END{print NR,$NF,FILENAME}' a.txt


[root@localhost shell]# echo $PWD | awk -F/ '{print $NF}'
[root@localhost shell]# pwd | awk -F/ '{print $NF}'

[root@localhost shell]# awk '{name=$1;belts=$4; if (belts ~/Yellow/) print name " is 

belt " belts}' a.txt

[root@localhost shell]# awk 'BEGIN {BASELINE="27  "} {if($6 < BASELINE) print $0}' a.txt
[root@localhost shell]# awk 'BEGIN {BASELINE=27} {if($6 < BASELINE) print $0}' a.txt

[root@localhost shell]# awk '{if($1=="andy"){$1="aaa"}; print $1}' a.txt

[root@localhost shell]# awk '{if($1 > "cc")  print$1,$2}' a.txt
[root@localhost shell]# awk '{if($1 > "cc") {}; print$1,$2}' a.txt

[root@localhost shell]# awk 'BEGIN{print"Name\tDifference"} {if($6<$7){$8=$7-

$6;print$1,$8}}' a.txt

[root@localhost shell]# awk '{total += $6} END{print "total points:"total}' a.txt
[root@localhost shell]# awk '{total += $6}{print$0} END{print "total points:"total}' 

a.txt

[root@localhost shell]# ls -l | awk '/^[^d]/ {print $8"\t"$5} {total += $5} END{print 

"total KB:"total}'


[root@localhost shell]# awk 'gsub(/4842/,1111){print$0}' a.txt

[root@localhost shell]# awk 'gsub(/4842/,1111){print$0}' a.txt


[root@localhost shell]# awk 'BEGIN {print "length($1)",length($1)} {print "length

($1):"length("a"$1)}' a.txt

[root@localhost shell]# awk 'BEGIN {print split("123#456#678", arr, "#"),arr[0], arr[1], 

arr[2]}'

[root@localhost shell]# echo "65" | awk '{printf"%c\n",$0}';

[root@localhost shell]# awk '{printf "%-15s %s\n", $1,$3}' a.txt

[root@localhost shell]# who | awk '{print $1" you are connect "$2}'


awk文件
#!/bin/awk -f

BEGIN{
        print("=========== Begin =================");
        strRecord="123#456#789";
        split(strRecord, arr, "#");

        FS="\t";
};

{
        print $0;
        total += $6;
}

END{
        for (i in arr)
        {
                print(arr[i]"\t");
        }
        print("total val:", total);
        print("******* end of awk ***********");
};


 
分享到:
评论

相关推荐

    AWK应用和讲解步骤

    man 中文awk版内容包括:语法、AWK变量、操作符、语句的编写,数组和函数的书写内容。

    AWK 应用程序入门与实例

    Awk 是一种名称奇怪但功能强大的语言。本文是一个包含三部分的系列的第一...个真实的高级 awk 应用程序。 -------------------------------------------- 这是从 ibm 网站上看到的。觉得不错后制作成为 PDF,在此共享。

    071204awk应用1

    071204awk应用1

    awk-prototype:awk 应用程序原型

    awk 应用程序原型安装说明:heroku 实例开始使用 mongodb 安装使用

    awk入门到精通.pdf

    这是awk最常被应用之处. 若能常常 如此处理问题, 读者可以以更高的角度来思考抽象的问题, 而不会被拘泥于细节的部份. 本手册为awk入门的学习指引, 其内容将先强调如何撰写awk程序,未列入进一步解题方式的应 用实例,...

    Linux四剑客之awk高级应用.doc

    创造者:Aho Wwinberger Kernighan 基于模式匹配检查输入文本,逐行处理并输出,通常用在shell中,获取指定的数据,单独使用时,可对文本数据做统计。

    学习和理解AWK的最佳书籍Effective AWK Programming

    这本书对awk的编程模型、基本语法有简单明了的介绍,在进行数据处理、文本处理、报表、试验算法方面的应用也有很多好的实例。  由于是88年的老书,其中对gawk最新版本对awk的扩展没有提及,但这并不妨碍其称为一本...

    AWK 使用教程

    在Linux中,AWK应用比较广泛,本文档介绍awk使用方法,内容详细,适合初学者使用!

    Sed与Awk (中文版)

    sed和awk是用户、程序员和管理员应用的工具。之所以称为sed是因为它是一个流编辑器(stream editor),用于对许多文件执行一系列的编辑操作。awk是根据它的开发者Aho、Weinberger和Kernighan命名的。awk是一种编程...

    Sed与awk 中文第二版

    sed和awk是用户、程序员和管理员应用的工具。之所以称为sed是因为它是一个流编辑器(stream editor),用于对许多文件执行一系列的编辑操作。awk是根据它的开发者aho、weinberger和kernighan命名的。awk是一种编程语言...

    生产环境awk最佳实践

    生产环境awk最佳实践

    Linux awk高级应用和cut的使用

    shell中色彩处理,awk高级应用, cut的基本使用,很多案例,可以实践

    AWK-4121介绍

    AWK-4121系列室外无线AP/网桥/客户端为工业应用提供了理想的 3合1无线解决方案,适用于不便接线、布线成本昂贵或使用移动 TCP/IP网络连接设备的场合

    The_AWK_Programming_Language_zh_CN.pdf

    Awk 是一种使用方便且表现力很强的编程语言, 它可以应用在多种不同的计算与数据处理任务中. 这一章 是一个简短的教程, 目的是为了能让读者尽可能快地写出自己的awk 程序. 第二章对整个awk 语言进行描 述, 剩下的章节...

    AWK命令集解释与应用

    全面描述AWK命令的各种解释与应用,协助系统维护人员开发小程序,提升维护效率

    linuxsed与awk第三版

    详细介绍了linuxsed与awk的应用,值得学习 第三版

    awk 基本的一些常用用法

    把实际应用中一些常用的awk的用法进行了总结,包括多文件操作,与shell之间的相互调用等

    5.6: awk高级应用 、 综合案例 、 总结和答疑.docx

    5.6: awk高级应用 、 综合案例 、 总结和答疑.docx

Global site tag (gtag.js) - Google Analytics