gpt4 book ai didi

Linux 脚本 : substituting a multiple lines with single line of text

转载 作者:可可西里 更新时间:2023-11-01 11:49:20 26 4
gpt4 key购买 nike

我应该在 Linux 中使用什么 shell 脚本来将具有 n 行文本的组替换为一行?

我有一个这样的文件:

a
b
c
*
d
e
f
*
g
h
i
*

我想获取一个文件:

abc
def
ghi

最佳答案

sed 方式:

sed ':a;N;$!ba;s/\n//g' < t | sed 's:*:\n:g'

t 是您要更改的文件。

引用资料: How can I replace a newline (\n) using sed? Why does sed not replace all occurrences?

第一个命令将\n 替换为空。第二个将 * 替换为\n。

顺便说一下,sed 是一个非常强大的流编辑器工具,知道它可以以比您想象的更多的方式为您提供帮助。

关于Linux 脚本 : substituting a multiple lines with single line of text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com