linux - 如何在从文件读取的每个匹配项上分割一行

标签 linux bash shell cygwin

我是一位 Linux 新手,需要您的帮助。 这段代码

sed 's/regexp/&\n/g' file.txt

仅在第一个匹配上工作仅在第一个匹配上给出输出,然后停止读取行的其余部分。

如果我想继续阅读该行直到结束并在每次匹配时将该行移至新行,该怎么办?

例如,我有一个如下所示的日志文件

some text here Critical and some text here Critical and again some text here Critical.

我想像下面这样打破它

some text here
Critical and some text here
Critical and again some text here

最佳答案

您必须使用 g 进行全局更改。

echo "some text here Critical and some text here Critical and again some text here Critical." | sed -e 's:Critical:\nCritical:g'

尽管这可行,但我建议您使用它:

echo "some text here Critical and some text here Critical and again some text here Critical." | sed -e 's:Critical:\
> Critical:g'

其中 > 是提示字符。你不必使用它。你会得到的。这是使用 sed 更改新行的可移植方法。所有其他方式都不可移植。

从文件中读取:

sed -e 's:Critical:\
Critical:g' file

关于linux - 如何在从文件读取的每个匹配项上分割一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718870/

相关文章:

mysql - 如何找到Mysql线程

c - Linux-C : reading from pipe returns first buffer written to it

c++ - MsgPack c++ 打包长度超过 32 个字符的字符串[Ubuntu]

c - 如何使用 C 获取 Linux 中的 CPU 数量?

linux - start-stop-daemon 启动多个进程

c - 如何在另一个 shell 的上下文中访问一个上下文的 shell 变量

java - 使用 Java 在 Mac 上的终端中运行使用管道的命令

linux - 如何在同一目录中的另一个脚本中使用 bash 脚本中的函数和变量?

git - 我如何让 git 使用 Textmate 作为我的默认编辑器?

shell - 我们可以使用 gsutil 命令执行 shell 脚本吗