我只想使用 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/