linux - 在不同组的行之间添加空行

标签 linux shell scripting sed awk

我有一个格式为(频率、文件名、代码行)的文件 A:

1 file_name1 code_line1
2 file_name2 code_line2
2 file_name2 code_line3
2 file_name3 code_line4
2 file_name3 code_line5
3 file_name4 code_line6
3 file_name4 code_line7
3 file_name4 code_line8

我希望输出 B 为:

1 file_name1 code_line1

2 file_name2 code_line2
2 file_name2 code_line3

2 file_name3 code_line4
2 file_name3 code_line5

3 file_name4 code_line6
3 file_name4 code_line7
3 file_name4 code_line8

基本上文件 A 包含文件名和文件中的代码行,第一个字段是频率,即文件中的代码行数。

我应该明智地通过这些代码行文件。我发现它很乏味,如果不同文件的条目之间存在行间隙,那么对我来说会更容易,因此需要输出。

最佳答案

Awk 可以做到:

awk '{if(NR > 1 && $2 != prev_two){printf "\n";} prev_two=$2; print $0}' A

A 是文件名。

关于linux - 在不同组的行之间添加空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4380447/

相关文章:

linux - 如何在 Linux 中创建只读 root 类型用户?

c++ - 在没有 Inotify 的情况下从 Linux 内核读取文件系统事件

scripting - 如何从字符串执行任意 native 命令?

bash - 如何将 grep 的值分配给变量并与字符串连接?

linux - 在 Linux 中,如何编写脚本来抓取所有 .js 文件?

batch-file - 如何搜索用户文件夹,同时避免使用 DIR 挖掘得太深?

python - 让 python 3 linux 创建的脚本在 Windows python 3 上运行的问题

linux - 如何让程序可以由具有所有者权限的特定用户在linux上运行

python - 使用 OpenSSL 静态编译 Python 3.6

windows - 使Windows批处理文件在程序退出时不关闭