Vim的正则表达式重复的行分组 [英] Vim Regex Duplicate Lines Grouping

查看:98
本文介绍了Vim的正则表达式重复的行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个日志文件:

I have a log file like this:

12 adsflljl
12 hgfahld
12 ash;al
13 a;jfda
13 asldfj
15 ;aljdf
16 a;dlfj
19 adads
19 adfasf
20 aaaadsf

和我想群他们像这两个中的一个:

And I would like to "group" them like one of these two:

12 adsfllj, 12 hgfahld, 12 ash;al
13 a;jfda, 13 asldfj
15 ;aljdf
16 a;dlfj
19 adads, 19 adfasf
20 aaaadsf

或者

12 adsfllj, hgfahld, ash;al
13 a;jfda, asldfj
15 ;aljdf
16 a;dlfj
19 adads, adfasf
20 aaaadsf

和我完全卡住。如果VIM没有做到这一点,我已经sed和awk和bash了。我真的不想写一个bash脚本,我想提高我的正则表达式福

And I am totally stuck. And if vim doesn't do it, I have sed and awk and bash too. I just don't really want to write a bash script, I want to increase my regex-fu

推荐答案

我只是用awk:

awk '
  {
    sep = val[$1] ? ", " : ""
    val[$1] = val[$1] sep $2
  }
  END {for (v in val) print v, val[v]}
' log.file | sort > new.file

这篇关于Vim的正则表达式重复的行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆