awk

median of column with awk

风流意气都作罢 提交于 2020-04-08 02:03:33
问题 How can I use AWK to compute the median of a column of numerical data? I can think of a simple algorithm but I can't seem to program it: What I have so far is: sort | awk 'END{print NR}' And this gives me the number of elements in the column. I'd like to use this to print a certain row (NR/2) . If NR/2 is not an integer, then I round up to the nearest integer and that is the median, otherwise I take the average of (NR/2)+1 and (NR/2)-1 . 回答1: This awk program assumes one column of numerically

median of column with awk

[亡魂溺海] 提交于 2020-04-08 02:00:10
问题 How can I use AWK to compute the median of a column of numerical data? I can think of a simple algorithm but I can't seem to program it: What I have so far is: sort | awk 'END{print NR}' And this gives me the number of elements in the column. I'd like to use this to print a certain row (NR/2) . If NR/2 is not an integer, then I round up to the nearest integer and that is the median, otherwise I take the average of (NR/2)+1 and (NR/2)-1 . 回答1: This awk program assumes one column of numerically

median of column with awk

◇◆丶佛笑我妖孽 提交于 2020-04-08 02:00:04
问题 How can I use AWK to compute the median of a column of numerical data? I can think of a simple algorithm but I can't seem to program it: What I have so far is: sort | awk 'END{print NR}' And this gives me the number of elements in the column. I'd like to use this to print a certain row (NR/2) . If NR/2 is not an integer, then I round up to the nearest integer and that is the median, otherwise I take the average of (NR/2)+1 and (NR/2)-1 . 回答1: This awk program assumes one column of numerically

Number of fields returned by awk

半腔热情 提交于 2020-04-07 14:34:13
问题 Is there a way to get awk to return the number of fields that met a field-separator criteria? Say, for instance, my file contains a b c d so, awk --field-separator=" " | <something> should return 4 回答1: The NF variable is set to the total number of fields in the input record. So: echo "a b c d" | awk --field-separator=" " "{ print NF }" will display 4 Note, however, that: echo -e "a b c d\na b" | awk --field-separator=" " "{ print NF }" will display: 4 2 Hope this helps, and happy awking 回答2:

SHELL脚本之awk妙用

假装没事ソ 提交于 2020-04-07 08:18:43
对于一个sougou文本文件,解压后大概4G,要求在其基础上切出第一列时间年月日时分秒增加在列中,作为hive的一个索引。先将文件head一下展示格式: [root@Master date]# head -n 5 sogou.full.utf8 20111230000005 57375476989eea12893c0c3811607bcf 奇艺高清 1 1 http://www.qiyi.com/ 20111230000005 66c5bb7774e31d0a22278249b26bc83a 凡人修仙传 3 1 http://www.booksky.org/BookDetail.aspx?BookID=1050804&Level=1 20111230000007 b97920521c78de70ac38e3713f524b50 本本联盟 1 1 http://www.bblianmeng.com/ 20111230000008 6961d0c97fe93701fc9c0d861d096cd9 华南师范大学图书馆 1 1 http://lib.scnu.edu.cn/ 20111230000008 f2f5a21c764aebde1e8afcc2871e086f 在线代理 2 1 http://proxyie.cn/ 最开始不知道awk这个命令

Linux三剑客 grep、awk、seed

佐手、 提交于 2020-03-30 19:39:05
简介 grep:适合单纯的查找或匹配文本 sed:适合编辑文本 awk:适合格式化文本,对文本进行复杂的格式化处理 1.grep   grep -help   【options】   -c:只输出匹配行的计数   -i:不区分大小写   -h:查询多文件是不显示文件名   -l:查询多文件时只输出包含匹配字符的文件名   -m:显示匹配行及行号   -s:不显示不存在或无匹配文本的错误信息   -v:显示不包含匹配文本的所有行   -color=auto:可以将找到的关键词部分加上颜色的显示   【pattern】正则参数含义   \:忽略正则表达式中特殊字符的原有含义   ^:匹配正则表达式的开始   $:匹配正则表达式的结束   \<:从匹配正则表达式的行开始   \>:到正则表达式的行结束   []:单个字符ex:[a]即a符合   [-]:范围ex:[a-c],即a,b,c符合   .:所有单个字符   *:有字符,长度可以为0   【简单使用】匹配的是当前文件夹下   grep ‘test’ d* 显示所有以d开头的文件中包含test的行   grep ‘test’ aa bb cc 显示在aa,bb,cc文件中匹配test的行   grep ‘[a-z]\{5\}’ aa 显示在aa文件中所有包含每个字符串至少有连续5个小写字符的字符串的行   grep 'w\(es\

shell——awk

天大地大妈咪最大 提交于 2020-03-28 21:20:22
awk -F"分隔符" "command" filename awk -F":" '{print $1}' /etc/passwd 字段引用: $1代表第一列; $2代表第二列; $0代表所有列。 内置变量: FS  代表输入的分隔符,等同于-F OFS  代表输出的分隔符 NF  代表字段数,因为NF是列数,所以$NF代表最后一列 NR  代表当前处理第几行 关系操作符: ==  等于 !=  不等于 >  大于 <  小于 >=  大于等于 <=  小于等于 逻辑操作符 &&  逻辑与,类似于shell的[ 条件1 -a 条件2 ] ||  逻辑或,类似于shell的[ 条件1 -0 条件2 ] !  非 运算符 + - * / ^或**  幂 awk可以运算浮点数 echo $[1.2*3]  #错误 echo | awk '{print 1.2*3}'  #正确 练习 截取所有开放监听的TCP协议端口号 netstat -ntl | awk 'NR>2 {print $4}' |awk -F: '{print $NF}' /etc/passwd文件 截取前五行的倒数第二列 awk -F: 'NR<=5 {print $(NF-1)}' /etc/passwd 打印第五行 awk -F: 'NR==5 {print $0}' /etc/passwd 打印第五行第五列 awk

shell awk命令字符串拼接

随声附和 提交于 2020-03-28 07:49:37
本节内容: awk命令实现字符串的拼接 输入文件的内容: TMALL_INVENTORY_30_GROUP my163149.cm6 3506 5683506 mysql-bin.000013 327359057 TMALL_INVENTORY_31_GROUP my163149.cm6 3606 5683606 mysql-bin.000017 301259529 $1 $2 $3 $4 $5 $6 目标: TMALL_INVENTORY_30_GROUP 000013.327359057#5683506.0 awk awk '{position=$5":"$6"#"$4; print $1, position}' posi.txt | awk -F '[.]' '{print $1,$2"."0}' | awk '{print $1,$3}' 字符串拼接,使用双引号"",来连接两边的字符串 输出 TMALL_INVENTORY_30_GROUP mysql-bin 000013:327359057#5683506.0 TMALL_INVENTORY_31_GROUP mysql-bin 000017:301259529#5683606.0 如果输入: awk '{position=$5":"$6"#"$4; print $1, position}' posi.txt | awk

awk命令小结

戏子无情 提交于 2020-03-25 06:02:31
先在此至敬朱双印老师,博客写得很详细: http://www.zsythink.net/archives/tag/awk/ 这是朱双印老师关于awk博客的链接,强力推荐给大家 AWK一般在网上说是一种处理文本文件的语言,也是一个强大的文本分析工具。 虽然我也认同这种说法,但是在此我的认知是: awk是逐行处理文本文件的语言/分析工具 从上面朱老师的图中可以看到,$0表示整行,其它的就是以分隔符进行划分的了 变量名称 代表意义 NF 每一行 ($0) 拥有的栏位总数 NR 目前 awk 所处理的是『第几行』数据 FS 目前的分隔字节,默认是空白键 命令参数格式 #awk '条件类型1{动作1} 条件类型2{动作2} ...' filename -F 指定输入分隔符【此处其实是可以使用多个分隔符进行分隔的,例:< awk -F '[-|]' '{print $3}' FileName>】 #cat /usr/local/mail/app/log/authenticator.log|grep 'none'|awk -F '[<>{}]' '{print $2,$4}' -v 【options】的一种,用于设置变量的值 -v OFS="+++" 【使用变量要配合-v选项】OFS可以设定awk的输出分隔符 ARGC 命令行参数个数 ARGV 命令行参数排列 ENVIRON

awk

谁说胖子不能爱 提交于 2020-03-24 05:58:01
1.awk 介绍 默认情况下不编辑源文件 报告生成器 -- 通过模式匹配以及自己本身的语言格式,来获取并输出客户所需要的内容; eg: 获取系统上面用户 ID 大于等于 1 小于等于 500 的用户的用户名和用户 ID for i in $(cut -d:-f3/etc/passwd);do if [ $i -ge 1 -a $i -le 500 ];then echo grep $i | cut -d:-f1,3 fi done ------------------------------------------- #awk -F:’{if($3>=1&&$3<=500){print $3}}’ /etc/passwd 2.awk 工作原理 3.awk 的用法 awk [option]... ‘program’FILE... program 必须使用!单引号 ! 多条 program 语句使用大括号包含起来可以并列可以嵌套 awk’{print} /etc/passwd’ 4.awk 的常见 option -F 指定分隔符 awk -F[/:]’{print $1 $3}’a.txt 其中 [ ] 内表示多个字符中的任意一个 -v 因为 awk 是一种语言编译器,能够自己定义变量,同时也有内置变量(与环境变量类似)手动指定变量参数 awk-v a=”a/b” ‘{print a}