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的更多相关文章

  1. openpyxl使用sheet.rows或sheet.columns报TypeError: 'generator' object is not subscriptable解决方式

    解决方案: 因为新版本的openpyxl使用rows或者columns返回一个生成器所以可以使用List来解决报错问题 >>> sheet.columns[0] Traceback ...

  2. shell awk命令

    语法: awk '{command}' filename  多个命令以分号分隔. awk 'BEGIN {command1} {command2} END{command3}'  注意:BEGIN , ...

  3. MySQL如何优化GROUP BY :松散索引扫描 VS 紧凑索引扫描

    执行GROUP BY子句的最一般的方法:先扫描整个表,然后创建一个新的临时表,表中每个组的所有行应为连续的,最后使用该临时表来找到组 并应用聚集函数.在某些情况中,MySQL通过访问索引就可以得到结果 ...

  4. 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 ...

  5. 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 ...

  6. awk 输出前 N 列的最简单方法

    最近遇到一种场景,需要输出一个文本信息的前 N 列. 众所周知 cut 可以指定分隔符并指定列的范围,如 cut -d' ' -f-4 就是以空格为分隔符输出前 4 列.但是 cut 的分隔符只能是一 ...

  7. Oracle 10gR2分析函数

    Oracle 10gR2分析函数汇总 (Translated By caizhuoyi 2008‐9‐19) 说明:  1. 原文中底色为黄的部分翻译存在商榷之处,请大家踊跃提意见:  2. 原文中淡 ...

  8. [转帖]Introduction to text manipulation on UNIX-based systems

    Introduction to text manipulation on UNIX-based systems https://www.ibm.com/developerworks/aix/libra ...

  9. R2—《R in Nutshell》 读书笔记(连载)

    R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...

随机推荐

  1. SQLSetEnvAttr

    SQLSetEnvAttr 函数定义: 函数的大概意思就是将,第一个参数设置为第二个参数的属性 SQLRETURN SQLSetEnvAttr( SQLHENV     EnvironmentHand ...

  2. Yii2 Restful api自定义资源

  3. tomcat的JVM调优

    1.error场景 Tomcat 长期运行过程遇到Caused by: java.lang.OutOfMemoryError: PermGen space或java.lang.OutOfMemoryE ...

  4. STL容器之map

    [1]map容器 map 是关联容器.容器中的每一个元素都是由一个键值和一个数据值组成的. set 是一个集合它以其元素作为键值(同一个键值只能出现一次),且默认以升序排列. list 是一个顺序容器 ...

  5. flask 操作数据库(分类)

    数据库 数据库是大多数动态web程序的基础设施,只要你想把数据存下来,就离不开数据库. 这里所说的数据库指的是有存储数据的单个或多个文件组成的集合,它是一种容器,可以类比文文件柜.而人们通常使用数据库 ...

  6. activiti 报 next dbid

    记录一下吧. 今天将生产环境的几个服务节点改成集群模式,其中包含activiti审批服务节点,其中各个服务几点间数据通信采用MQ(与本文无关). 然后报出如题错误. 究其原因就是,在启动activit ...

  7. hashCode 一致性hash 算法

    1 如果两个对象相同,那么它们的hashCode值一定要相同.也告诉我们重写equals方法,一定要重写 hashCode方法,同一个对象那么hashcode就是同一个(同一个对象什么都是相同的).2 ...

  8. hihoCoder #1037 : 数字三角形 (动态规划)

    题目链接:https://hihocoder.com/problemset/problem/1037# 问题描述 小Hi和小Ho在经历了螃蟹先生的任务之后被奖励了一次出国旅游的机会,于是他们来到了大洋 ...

  9. input file accept类型

    Valid Accept Types: For CSV files (.csv), use: <input type="file" accept=".csv&quo ...

  10. 笔面试复习(spring常用.jar包/事务/控制反转/bean对象管理和创建/springMVC工作原理/sql查询)

    ###spring常用jar包1.spring.jar是包含有完整发布模块的单个jar包.2.org.springframework.aop包含在应用中使用Spring的AOP特性时所需要的类.3.o ...