awk - group adjacent rows by identical columns
Liang always brings me interesting quiz questions. Here is one:
If i have a table like below:
chr1 113438 114495 1 chr1 114142 114143
chr1 113438 114495 2 chr1 114171 114172
chr1 170977 174817 1 chr1 171511 171512
chr1 170977 174817 2 chr1 171514 171515
chr1 170977 174817 2 chr1 173545 173546
and I would like to collapse the rows if the first 3 columns are identical to make the following output:
chr1 113438 114495 114142,114143,114171,114172
chr1 170977 174817 171511,171512,171514,171515,173545,173546
Is there any easy awk approach to do it?
Since I am so rusty at awk, I had to google around to find the solution:
awk -F '\t' '
$1FS$2FS$3==x{
printf ",%s,%s", $6, $7
next
}
{
x=$1FS$2FS$3
printf "\n%s\t%s,%s", x, $6, $7
}
END {
printf "\n"
}' test.txt
Assuming the input file is test.txt
. Note that the input and output are both tab-separated.
Explanation:
x=$1FS$2FS$3
: variable x stores the value of columns 1, 2, and 3 separated by field separator FS
.
Print the first part of an output line (columns 1, 2, 3, 6, 7).
For next line, if columns 1, 2, and 3 equal x, print columns 6 and 7.
Group and then count:
https://stackoverflow.com/questions/14916826/awk-unix-group-by
have this text file:
name, age
joe,42
jim,20
bob,15
mike,24
mike,15
mike,54
bob,21
Trying to get this (count):
joe 1
jim 1
bob 2
mike 3
awk -F, 'NR>1{arr[$1]++}END{for (a in arr) print a, arr[a]}' file.txt
References:
http://azaleasays.com/2014/10/06/awk-group-adjacent-rows-by-identical-columns/
Group rows in text file and aggregate corresponding rows to column
keeping last record among group of records with common fields (awk)
awk - group adjacent rows by identical columns的更多相关文章
- openpyxl使用sheet.rows或sheet.columns报TypeError: 'generator' object is not subscriptable解决方式
解决方案: 因为新版本的openpyxl使用rows或者columns返回一个生成器所以可以使用List来解决报错问题 >>> sheet.columns[0] Traceback ...
- shell awk命令
语法: awk '{command}' filename 多个命令以分号分隔. awk 'BEGIN {command1} {command2} END{command3}' 注意:BEGIN , ...
- MySQL如何优化GROUP BY :松散索引扫描 VS 紧凑索引扫描
执行GROUP BY子句的最一般的方法:先扫描整个表,然后创建一个新的临时表,表中每个组的所有行应为连续的,最后使用该临时表来找到组 并应用聚集函数.在某些情况中,MySQL通过访问索引就可以得到结果 ...
- Crazy Rows
Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...
- 2009 Round2 A Crazy Rows (模拟)
Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...
- awk 输出前 N 列的最简单方法
最近遇到一种场景,需要输出一个文本信息的前 N 列. 众所周知 cut 可以指定分隔符并指定列的范围,如 cut -d' ' -f-4 就是以空格为分隔符输出前 4 列.但是 cut 的分隔符只能是一 ...
- Oracle 10gR2分析函数
Oracle 10gR2分析函数汇总 (Translated By caizhuoyi 2008‐9‐19) 说明: 1. 原文中底色为黄的部分翻译存在商榷之处,请大家踊跃提意见: 2. 原文中淡 ...
- [转帖]Introduction to text manipulation on UNIX-based systems
Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ...
- R2—《R in Nutshell》 读书笔记(连载)
R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...
随机推荐
- kali linux dns劫持
1,確定局域網ip 2,修改/etc/ettercap/etter.dns 添加自己的ip和劫持域名 3,/var/www/index.html (修改html页面替换被劫持的 ...
- kali linux 基本命令(第一批)
pwd , rm ,locate ,cat ,head , clear ,ls ,cd ,mkdir ,touch ,ec ...
- 【转】win中IDLE选择virtualenv的启动方法
从dos命令行运行.(virtualenv dir)\Scripts\activate.bat脚本激活环境,然后执行: python -m idlelib.idle 摘录:https://blog.c ...
- ref 参数与out参数
变量作为参数传给方法,同时希望在方法执行完成后对参数,反应到变量上面.就需要用到ref和out这两个参数. ref参数:在 传入前必须先初始化 out参数:不需要做预先的处理
- SQL优化(转)
1. 负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好习惯 可以优化为in查询: ...
- 深度学习之循环神经网络RNN概述,双向LSTM实现字符识别
深度学习之循环神经网络RNN概述,双向LSTM实现字符识别 2. RNN概述 Recurrent Neural Network - 循环神经网络,最早出现在20世纪80年代,主要是用于时序数据的预测和 ...
- [转载]Oracle修改用户表所属表空间的步骤
1 .修改表的空间alter table TABLE_NAME move tablespace TABLESPACENAME 查询当前用户下的所有表选择'alter table'|| table_na ...
- CentOS7 重启网卡Failed to start LSB: Bring up/down networking.解决方法
环境:MAC PD虚拟机安装centos7 修改完网卡配置,重启网络服务报错 使用提示命令查看:systemctl status network.service 发现报错为Failed to star ...
- 前端框架VUE----学习vue前的准备工作
起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就直接 ...
- Talented Chef ZOJ - 3778
As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. ...