linux - 仅当使用 sed 或 awk 为空时才删除模式后的一行

标签 linux bash awk sed

我只想使用 sed 或 awk 删除一个空行,前提是这一行在我的模式行之后 例如,如果我有

G

O TO P999-ERREUR

END-IF.

本例中的模式是G 我想要这个输出

 G
 O TO P999-ERREUR

 END-IF.

最佳答案

这样就可以了:

$ awk -v n=-2 'NR==n+1 && !NF{next} /G/ {n=NR}1' file
G
O TO P999-ERREUR

END-IF.

解释:

-v n=-2    # Set n=-2 before the script is run to avoid not printing the first line
NR == n+1  # If the current line number is equal to the matching line + 1
&& !NF     # And the line is empty 
{next}     # Skip the line (don't print it)
/G/        # The regular expression to match
{n = NR}   # Save the current line number in the variable n
1          # Truthy value used a shorthand to print every (non skipped) line

关于linux - 仅当使用 sed 或 awk 为空时才删除模式后的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31961990/

相关文章:

linux - Redhat 中的锁定和解锁屏幕

linux - 我遇到错误 "Clearsigned file isn' t 有效,得到 'NOSPLIT'(网络是否需要身份验证?)”

linux - 如何在linux下安装adobe air sdk?

file - 用 bash 读取文本文件的前 8 个字符

linux - 引号取消 bash 中的命令传递给变量

bash - sed 命令在 mac 中不起作用

python - 从行中搜索数字并放在特定句子中

awk - 使用 awk 删除过滤组

unix - 如何合并以特定子字符串开头的行?

c - RS 232 DB9 引脚切换代码的​​问题