【问题标题】:How to find first empty line and delete with in the file using sed command regular expression [duplicate]如何使用 sed 命令正则表达式在文件中查找第一个空行并删除
【发布时间】:2019-08-13 09:36:11
【问题描述】:

我需要使用sed 命令正则表达式在文本文件中找到第一个空行。

以下是我拥有的内容


this is defile
to be cleaned
skip rest consl

在上面的内容中,我只需要找到第一行是空行 并删除第一行。

我使用了下面的正则表达式,但它对我不起作用,它在文件中到处都是空行。

^\s*$

【问题讨论】:

  • 你是如何以及在哪里用什么语言编码的?你能分享你的代码/演示/努力吗?
  • 你需要第一个空行的内容,或者它的位置,还是什么?
  • 试试(?m)\A\s*$,如果为空,它只会匹配第一行。
  • 如果您想在文本编辑器中查找第一个空行并替换它,请使用 ^\s*$([\s\S]*) 并替换为 New line$1
  • @Pushpesh Kumar Rajwanshi - 我需要在 linux 中使用 sed 命令的代码

标签: regex file find line is-empty


【解决方案1】:

在 Java 中你可以这样做:

Pattern pattern = Pattern.compile ("^\\s*$", Pattern.MULTILINE);
Matcher matcher = pattern.matcher ("foo\n  \nbar");
if (matcher.find()) {
    String firstEmptyLine = matcher.group();
    ...
}

【讨论】:

  • 米哈伊尔 - 我需要 sed 命令
  • 你需要sed 来处理第一个空行吗?
  • 我要删除第一个空行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2012-06-01
  • 2011-04-23
  • 2015-04-01
  • 2017-03-06
  • 2019-04-02
  • 2011-10-02
相关资源
最近更新 更多