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 ...
随机推荐
- 使用.NET向webService传double、int、DateTime 服务器得到的数据时null的问题(转http://blog.csdn.net/slimboy123/article/details/4366701)
用C#.NET调用Java开发的WebService时,先在客户端封装的带有int属性的对象,当将该对象传到服务器端时,服务器端可以得到string类型的属性值,却不能得到int类型.double和D ...
- hdu3374 最大最小表示法 +kmp
#include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...
- 【转】Requests 官方中文文档 - 快速上手
迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Requests ...
- 谈谈Groovy闭包
A closure is a function with variables bound to a context or environment in which it executes. 概述 闭包 ...
- 编程中的链式调用:Scala示例
编程中的链式调用与Linux Shell 中的管道类似.Linux Shell 中的管道 ,会将管道连接的上一个程序的结果, 传递给管道连接的下一个程序作为参数进行处理,依次串联起N个实用程序形成流水 ...
- SQL Server 配置管理器
- Java Socket入门
Java Socket底层采用TCP/IP协议通信,通信细节被封装,我们仅仅需要指定IP.端口,便能轻易地创建TCP或UDP连接,进行网络通信.数据的读写,可以使用我们熟悉的stream进行操作. T ...
- c# 控件的基类——Control
控件的基类用于Windows窗体应用的控件都派生自Control类,并继承了许多通用成员,这些成员都是平时使用控件的过程中最常用的. Name:控件实例的名字,通常通过“属性”窗口设置,控件实例名称变 ...
- Linux基础命令---添加用户useradd
useradd 创建新的系统用户,useradd指令只能以管理员的身份运行,创建的用户都在“/etc/passwd”文件中.当不加-D参数,useradd指令使用命令列来指定新帐号的设定值and使用系 ...
- java实现 HTTP/HTTPS请求绕过证书检测代码实现
java实现 HTTP/HTTPS请求绕过证书检测代码实现 1.开发需求 需要实现在服务端发起HTTP/HTTPS请求,访问其他程序资源. 2.URLConnection和HTTPClient的比较 ...