- 浏览: 2868668 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (1173)
- 名言警句 (5)
- 心情随笔 (50)
- 数据库 (57)
- Java基础 (241)
- J2EE框架 (91)
- 数据结构 (12)
- 程序设计 (21)
- WEB技术 (128)
- 网络日志 (12)
- IT资讯 (247)
- linux (64)
- solaris (2)
- 其它 (143)
- WebService (4)
- 日语学习 (2)
- 机器人 (5)
- Android (5)
- cgywin (3)
- Game (1)
- DWR (1)
- spring (8)
- canvas (1)
- Guava (3)
- Modbus (5)
- 测试 (6)
- mongodb (9)
- Quartz (2)
- Cron (1)
- windows (2)
- 持续集成 (1)
- bootstrap (3)
- 结对编程 (1)
- nodejs (1)
- Netty (1)
- 安全 (3)
- webstorm (2)
- sparkline (1)
- Job (1)
- git (3)
- Maven (3)
- knockout (5)
- jquery (1)
- bower (1)
- docker (1)
- confluence (4)
- wiki (1)
- GoogleMap (1)
- jekyll (10)
- ruby (2)
- npm (3)
- browserify (1)
- gulp (3)
- openwrt (1)
- discuz (3)
- 输入法 (1)
- JPA (1)
- eclipse (2)
- IntelliJ (1)
- css (1)
- 虚拟机 (1)
- 操作系统 (1)
- azkaban (2)
- scrum (1)
最新评论
-
pangxiea_:
你好, 想请问一下 Linux下 这么使用rxtxcomm 在 ...
使用Java进行串口通信 -
abababudei:
请教一下,这个您是怎么解决的:/dev/ttyS2enteri ...
Java应用程序的MODBUS通讯 -
xuniverse:
hannibal005 写道楼主,我问下 request.se ...
用javascript与java进行RSA加密与解密 -
atxkm:
找了一下午,终于找到了
gulp 拷贝文件时如何移除文件目录结构 -
kalogen:
gtczr 写道非常感谢,经过我自己的修改,已经完美实现。发出 ...
用javascript与java进行RSA加密与解密
$ cat file Item1,200 Item2,500 Item3,900 Item2,800 Item1,600
$ awk -F"," '{x+=$2}END{print x}' file 3000The delimiter(-F) used is comma since its a comma separated file. x+=$2 stands for x=x+$2. When a line is parsed, the second column($2) which is the price, is added to the variable x. At the end, the variable x contains the sum. This example is same as discussed in the awk example of finding the sum of all numbers in a file.
2. To find the total sum of particular group entry alone. i.e, in this case, of "Item1":
$ awk -F, '$1=="Item1"{x+=$2;}END{print x}' file 800This gives us the total sum of all the items pertaining to "Item1". In the earlier example, no condition was specified since we wanted awk to work on every line or record. In this case, we want awk to work on only the records whose first column($1) is equal to Item1.
3. If the data to be worked upon is present in a shell variable:
$ VAR="Item1" $ awk -F, -v inp=$VAR '$1==inp{x+=$2;}END{print x}' file 800-v is used to pass the shell variable to awk, and the rest is same as the last one.
4. To find unique values of first column
$ awk -F, '{a[$1];}END{for (i in a)print i;}' file Item1 Item2 Item3Arrays in awk are associative and is a very powerful feature. Associate arrays have an index and a corresponding value. Example: a["Jan"]=30 meaning in the array a, "Jan" is an index with value 30. In our case here, we use only the index without values. So, the command a[$1] works like this: When the first record is processed, in the array named a, an index value "Item1" is stored. During the second record, a new index "Item2", during third "Item3" and so on. During the 4th record, since the "Item1" index is already there, no new index is added and the same continues.
To understand the for loop better, look at this:
for (i in a) { print i; }
Note: The order of the output in the above command may vary from system to system. Associative arrays do not store the indexes in sequence and hence the order of the output need not be the same in which it is entered.
5. To find the sum of individual group records. i.e, to sum all records pertaining to Item1 alone, Item2 alone, and so on.
$ awk -F, '{a[$1]+=$2;}END{for(i in a)print i", "a[i];}' file Item1, 800 Item2, 1300 Item3, 900a[$1]+=$2 . This can be written as a[$1]=a[$1]+$2. This works like this: When the first record is processed, a["Item1"] is assigned 200(a["Item1"]=200). During second "Item1" record, a["Item1"]=800 (200+600) and so on. In this way, every index item in the array is stored with the appropriate value associated to it which is the sum of the group.
6. To find the sum of all entries in second column and add it as the last record.
$ awk -F"," '{x+=$2;print}END{print "Total,"x}' file Item1,200 Item2,500 Item3,900 Item2,800 Item1,600 Total,3000This is same as the first example except that along with adding the value every time, every record is also printed, and at the end, the "Total" record is also printed.
7. To print the maximum or the biggest record of every group:
$ awk -F, '{if (a[$1] < $2)a[$1]=$2;}END{for(i in a){print i,a[i];}}' OFS=, file Item1,600 Item2,800 Item3,900Before storing the value($2) in the array, the current second column value is compared with the existing value and stored only if the value in the current record is bigger. And finally, the array will contain only the maximum values against every group. In the same way, just by changing the "lesser than(<)" symbol to greater than(>), we can find the smallest element in the group.
if (condition)
{
<code for true condition >
}else{
<code for false condition>
}
8. To find the count of entries against every group:
$ awk -F, '{a[$1]++;}END{for (i in a)print i, a[i];}' file Item1 2 Item2 2 Item3 1
9. To print only the first record of every group:
$ awk -F, '!a[$1]++' file Item1,200 Item2,500 Item3,900A little tricky this one. In this awk command, there is only condition, no action statement. As a result, if the condition is true, the current record gets printed by default.
10. To join or concatenate the values of all group items. Join the values of the second column with a colon separator:
$ awk -F, '{if(a[$1])a[$1]=a[$1]":"$2; else a[$1]=$2;}END{for (i in a)print i, a[i];}' OFS=, file Item1,200:600 Item2,500:800 Item3,900This if condition is pretty simple: If there is some value in a[$1], then append or concatenate the current value using a colon delimiter, else just assign it to a[$1] since this is the first value.
if(a[$1]) a[$1]=a[$1]":"$2; else a[$1]=$2The same can be achieved using the awk ternary operator as well which is same as in the C language.
$ awk -F, '{a[$1]=a[$1]?a[$1]":"$2:$2;}END{for (i in a)print i, a[i];}' OFS=, file Item1,200:600 Item2,500:800 Item3,900Ternary operator is a short form of if-else condition. An example of ternary operator is: x=x>10?"Yes":"No" means if x is greater than 10, assign "Yes" to x, else assign "No".
Concatenate variables in awk:
One more thing to notice is the way string concatenation is done in awk. To concatenate 2 variables in awk, use a space in-between.
Examples:
z=x y #to concatenate x and y z=x":"y #to concatenate x and y with a colon separator.
发表评论
-
linux中if -z -d什么意思?
2015-04-27 13:53 1162[ -a FILE ] 如果 FILE 存在则为真。 ... -
linux bash shell 基本知识
2015-04-23 14:01 1218以下为转载内容: $ which bash O ... -
如何处理 bash shell 变量中的斜杠(/)?
2015-02-10 12:19 3631#!/bin/bash ## Our path ... -
通过dos 命令调用git bash
2015-01-14 12:57 18921. 先安装 git windows 客户端 2. ... -
vim常用快捷键
2014-11-24 10:55 730一. 移动: h,j,k,l: ... -
win7 + cygwin + nodejs很详细的安装步骤
2014-04-12 09:23 4423win7下安装node.js详细步骤 1.下载cygwin ... -
open windows explorer in cygwin
2014-01-08 14:29 724There's a very good implementa ... -
Operator usage in bash shell
2014-01-07 15:19 967Common Bash comparisons Op ... -
call bash shell script by dos command
2014-01-07 13:50 18041. at first we have a scrip ... -
10 个很有用的高级 Git 命令
2013-08-26 19:56 900迄今,我已经使用Git很长一段时间了,考虑分享一些不管你是团 ... -
cygwin很多命令显示command not found的解决办法
2013-08-14 13:53 2744cygwin很多命令显示command not found的 ... -
linux 服务器初始基本设置
2013-07-18 17:16 962初始安全设置 登录以后,应该马上安装安全更新,这很重要,安 ... -
10个VI的常用技巧
2013-07-17 12:42 9241.打开和关闭行号 :s ... -
some useful notes for cygwin
2013-05-22 10:48 1101useful alias list: ========== ... -
Change cygwin Home location
2013-05-22 10:20 1029Directly edited the Cygwin.bat ... -
vim语法高亮
2013-05-21 18:51 1052vi配置:1.在用户目录建立.vimrc[flying_dog ... -
bash shell notes:
2013-01-29 12:04 1106#!/bin/bash config_file ... -
how to call groovy application in linux
2013-01-29 10:21 939#!/bin/bash function launc ... -
Userful shell commoand in linux
2013-01-04 16:08 1035Userful shell commoand in li ... -
Go Lang books
2012-05-24 16:17 0this book is from http://vmg.pp ...
相关推荐
MOXA AWK-3121 配置方法及要点说明 MOXA AWK-3121 是一种工业级无线设备,具有 web 功能配置、IP 网络参数设置、工作模式选择、功率增强等特点。本文将详细介绍 MOXA AWK-3121 的配置方法及要点说明。 一、Web ...
Moxa AWK-3131A 三合一工业级无线AP/Bridge/Client 支 持IEEE 802.11n 技术,数据传输率高达300Mbps,满足了 不断增长的快速数据传输和信号覆盖范围更广泛的要求。 AWK-3131A 符合各种工业标准,包括工作温度、输入...
### AWK-4121系列室外无线AP/网桥/客户端关键技术知识点 #### 一、产品概述 AWK-4121是一款专为工业级应用设计的室外无线接入点/AP、网桥及客户端三合一设备。它适用于不易布线、布线成本较高或者需要在移动TCP/IP...
首先,配置AWK-3121-EU的IP网络参数是实现设备接入网络的基础。通过Web界面可以设置IP地址、子网掩码以及默认网关,为设备提供唯一的网络身份标识。在网络配置中,详细步骤通常包括IP地址的静态分配或通过DHCP自动...
awk-思维导图
这个名为"AWK-file.rar_awk_awk tcl_delay awk_jitter awk _jitter ns-2"的压缩包文件显然是针对网络性能分析的,特别是使用AWK进行分析。下面将详细介绍涉及的知识点。 1. **AWK**: AWK是一种编程语言,由Aho、...
摩沙AP用戶手冊
- 示例:`awk 'BEGIN { FS=":" } /pat/ { print }' file1 file2`。 4. **脚本文件**: - awk程序可以保存到脚本文件中,并通过 `-f` 参数调用。 - 示例脚本文件 `script.awk` 内容为 `{ print $1, $2 }`,运行...
- **通用格式**:`awk [options] '{awk-commands}' file` - `options`:可以是`awk`支持的各种选项。 - `awk-commands`:具体的`awk`命令。 - `file`:待处理的文件。 #### 六、awk程序文件 - **执行格式**:`...
The book begins with an overview and a tutorial that demonstrate a progression in functionality from grep to sed to awk. sed and awk share a similar command-line syntax, accepting user instructions in...
在Linux系统中,awk是一种强大的文本分析工具,尤其在处理数据和日志文件时非常有用。awk其实有多个版本,包括awk、nawk和gawk,而在CentOS系统中默认使用的是gawk。awk的工作原理是对文本文件的每一行进行处理,...
"Sed-and-Awk-101-Hacks"可能包含如何使用基本的sed命令,如替换、删除、插入行,以及使用地址范围进行特定操作。此外,还可能涉及正则表达式在sed中的应用,如查找和替换模式,以及使用sed进行批量文本编辑。 3. ...
标题"awk-script.zip_NS2 awk_awk_ns2_awk_trace"暗示我们关注的是一个`awk`脚本,它设计用来处理与`ns2`追踪相关的任务。`ns2`的追踪文件通常包含大量的网络活动信息,如节点位置、数据包传输、延迟等,而`awk`脚本...
### awk & sed 高级练习题解析 #### sed 命令详解 1. **删除一个文件的每行中的第1个字符** ```shell sed 's/^.//g' /etc/passwd ``` - **解析**:`s/^.//g` 表示将每行开头(`^`)的第一个字符(`.`)替换为...
《GNU Awk - 中文版1》是一本详细介绍GNU Awk的参考手册,适用于广泛的读者群体,无论是初学者还是经验丰富的程序员。Awk是一种强大的文本分析工具,它基于Alfred V. Aho、Brian W. Kernighan和Peter J. Weinberger...
基本形式为`awk 'program' file1 file2 ...`,其中`program`是AWK脚本。 4. **AWK的语法**: - `pattern {action}`是AWK的基本结构,表示当行匹配`pattern`时执行`action`。 - 如果没有指定`pattern`,则`action`...
《全面解析NS2仿真与AWK脚本:深入理解all-awk.rar》 在计算机科学领域,网络模拟和分析是至关重要的环节,特别是在设计和优化网络协议时。NS2(Network Simulator 2)是一个广泛使用的开源网络模拟工具,它允许...
AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。 之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首 字符。
examples in this page, plus a smidgen. The examples given below have the extensions of the executing script as part of the filename. Once you download it, and make it executable, you can rename it ...