首页 > 解决方案 > 在 Shell 中的每个空行之后打印所有行

问题描述

我正在尝试在文件中的每个空白行之后打印所有行。任何人都可以在shell中建议一个命令吗?

示例文件:

line1                                                                        
line2

line 3                                                                        
line 4

line 5
line 7

line 6

预期输出:

第 3
行 第 4
行 第 5
行 第 7
行 第 6 行

标签: shellawksed

解决方案


您能否尝试在 GNUawk中仅在显示的示例中进行跟踪、编写和测试。

awk '/^$/{found=1;next} found && !/^$/' Input_file

输出如下。

line 3
line 4
line 5
line 7
line 6

说明:为上述解决方案添加详细说明。

awk '             ##Starting awk program from here.
/^$/{             ##Checking if a line is an empty line then do following.
  found=1         ##Setting found to 1 here.
  next            ##next will skip all further statements from here.
}
found && !/^$/    ##Checking condition if found is SET and line is NOT empty then print that line.
' Input_file      ##Mentioning Input_file name here.

推荐阅读