linux awk高级应用实例_linux shell

今天看到unix shell 范例精解上有道awk的题目 做了以后拿来和大家分享下

处理前的文档:

 Mike Harrington:(510) 548-1278:250:100:175
 Christian Dobbins:(408) 538-2358:155:90:201
 Susan Dalsass:(206) 654-6279:250:60:50
 Archie McNichol:(206) 548-1348:250:100:175
 Jody Savage:(206) 548-1278:15:188:150
 Guy Quigley:(916) 343-6410:250:100:175
 Dan Savage:(406) 298-7744:450:300:275
 Nancy McNeil:(206) 548-1278:250:80:75
 John Goldenrod:(916) 348-4278:250:100:175
 Chet Main:(510) 548-5258:50:95:135
 Tom Savage:(408) 926-3456:250:168:200
 Elizabeth Stachelin:(916) 440-1763:175:75:300

需要的结果如下:
                ***CAMPAIGN 1998 CONTRIBUTIONS***

Name                   PHone                   Jan |   Feb |   MAR |   Total Donated
---------------------------------------------------------------------------------
 
Mike Harrington         (510) 548-1278          250     100     175     525
Christian Dobbs         (408) 538-2358          155     90      201     446
Susan Dalsass           (206) 654-6279          250     60      50      360
Archie McNichol         (206) 548-1348          250     100     175     525
Jody Savage             (206) 548-1278          15      188     150     353
Guy Quigley             (916) 343-6410          250     100     175     525
Dan Savage              (406) 298-7744          450     300     275     1025
Nancy McNeil            (206) 548-1278          250     80      75      405
John Goldenrod          (916) 348-4278          250     100     175     525
Chet Main               (510) 548-5258          50      95      135     280
Tom Savage              (408) 926-3456          250     168     200     618
Elibeth Stachel         (916) 440-1763          175     75      300     550

 SUMMARY
-----------------------------------------------------------------------------------
The campan received atotal of $6137 for this quarter
average donation for the 12 contributors was $511.417.
The highest contribution was $450.
The lowest contribution was $15.

代码如下:

BEGIN{FS=":";low1=300;low2=400;low3=500
    OFS="\t"
    print "\t\t***CAMPAIGN 1998 CONTRIBUTIONS***\n"
    print "---------------------------------------------------------------------------------\n"
    print " Name\t\t\tPHone\t\t\tJan |\tFeb |\tMAR |\tTotal Donated"
    print "---------------------------------------------------------------------------------\n"
    }

{tot=$3+$4+$5}
{Ttot+=tot}
{print $1,"\t"$2"\t\t"$3" \t"$4" \t"$5" \t"tot}
{avg=Ttot/12}
{high1=(high1>$3)?high1:$3}
{high2=(high1>$4)?high1:$4}
{high3=(high1>$5)?high1:$5}
{max12=(high1>high2)?high1:high2}
{max23=(high2>high3)?high2:high3}
{Max=(max12>max23)?max12:max23}
{low1=(low1<$3)?low1:$3}
{low2=(low1<$4)?low1:$4}
{low3=(low1<$5)?low1:$5}
{min12=(low1<low2)?low1:low2}
{min23=(low2<low3)?low2:low3}
{Min=(min12<min23)?min12:min23}

END{
    print "-----------------------------------------------------------------------------------"
    print"\t\tSUMMARY"
    print "-----------------------------------------------------------------------------------"
    printf "The campan received atotal of $";printf Ttot; print " for this quarter"
    printf "average donation for the 12 contributors was $"; printf avg ;print"."
    printf "The highest contribution was $";printf Max;print "."
    printf "The lowest contribution was $";printf Min;print "."
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索awk高级应用
linux shell awk、linux shell awk if、linux shell awk 变量、linux shell awk f、linux shell awk 分割,以便于您获取更多的相关知识。

时间: 2024-08-01 07:06:52

linux awk高级应用实例_linux shell的相关文章

linux awk时间计算脚本及awk命令详解_linux shell

在linux如果计划时间是个麻烦事, 用awk脚本如下 BEGIN {FS=":";OFS=":"} {total_seconds=total_seconds+$3} total_seconds>=60 {total_seconds=total_sconds-60 $2=$2+1 } {total_minutes=total_minutes+$2 $2=$2+1 } {total_minutes=total_minutes+$2} total_minutes&

Shell脚本检查IP格式及mysql操作实例_linux shell

还是cronjob的一部分,就是在Rails的定时任务里,后台交互运行 CheckIPAddress() { echo $1 |grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" > /dev/null if [ $? = 1 ]; then return 1 else a=`echo $1 | awk -F. '{print $1}'` b=`echo $1 | awk -F. '{print $2}'

用shell脚本监控进程是否存在 不存在则启动的实例_linux shell

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/sh ps -fe|grep processString |grep -v grep if [ $? -ne 0 ] then echo "start process....." else echo "runing....." fi ##### processString 表示进程特征字符串,能够查询到唯一进程的特征字符串 0表示存在的 $? -ne 0 不存在,$? -eq 0 存

一天一个shell命令 linux文本内容操作系列-awk命令详解_linux shell

简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理. awk有3个不同版本: awk.nawk和gawk,未作特别说明,一般指gawk,gawk 是 AWK 的 GNU 版本. awk其名称得自于它的创始人 Alfred Aho .Peter Weinberger 和 Brian Kernighan 姓氏的首个字母.实际上 AWK

linux Shell脚本里面把一个数组传递到awk内部进行处理_linux shell

前段时间和几位同事讨论过一个问题:Shell脚本里面怎样把一个数组传递到awk内部进行处理? 当时没有找到方法.前两天在QQ群里讨论awk的时候,无意间又聊起这个话题.机缘巧合之下找到一个思路,特此分享. 测试环境: [root]# head -1 /etc/redhat-release Red Hat Enterprise Linux Server release 6.5 (Santiago) [root]# awk --version | head -1 GNU Awk 3.1.7 众所周知

awk命令、awk编程语言详细介绍和实例_linux shell

一,什么是awk awk是linux下的一个命令,他对其他命令的输出,对文件的处理都十分强大,其实他更像一门编程语言,他可以自定义变量,有条件语句,有循环,有数组,有正则,有函数等.他读取输出,或者文件的方式是一行,一行的读,根据你给出的条件进行查找,并在找出来的行中进行操作,感觉他的设计思想,真的很简单,但是结合实际情况,具体操作起来就没有那么简单了.他有三种形势,awk,gawk,nawk,平时所说的awk其实就是gawk. 二,awk中的记录,域,分割符 当我们读取输出时,或者读取文件时,

Shell脚本注册到Linux系统服务实例_linux shell

注册一个系统服务,开机自启动. 1 脚本编写 #vim test.sh 复制代码 代码如下: #!/bin/bash    #description: hello.sh  #chkconfig: 2345 20 81    EXEC_PATH=/usr/local/  EXEC=hello.sh  DAEMON=/usr/local/hello.sh  PID_FILE=/var/run/hello.sh.pid    . /etc/rc.d/init.d/functions    if [ !

linux bash shell中case语句的实例_linux shell

bash case语句的例子. 分享一段bash shell代码,对于学习bash的同学理解case语句的用法,会有帮助. 例子: 复制代码 代码如下: #!/bin/bash### Program:# File operation# 1.) Open file 2.) Display file 3.) Edit file 4.) Delete file# site: WWW.JB51.NETPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/u

linux whatis与whatis database 使用及查询方法(man使用实例)_linux shell

通过man查找帮助过程: [chengmo@centos5 ~]$ man -h ...  f:same as whatis(1) ... #觉得比较奇怪,whatis是什么呢? [chengmo@centos5 ~]$ man whatis #查询得到是: #whatis - search the whatis database for complete words. #它是查询whatis数据库的工具 #The whatis database is created using the com