gpt4 book ai didi

string - 如何将所有从 'foobar' 开始的行移动到文件末尾?

转载 作者:行者123 更新时间:2023-11-29 09:17:48 27 4
gpt4 key购买 nike

假设我有一个脚本,其中有许多行以 foobar 开头

我想将所有行移动到文档的末尾,同时保持它们的顺序

例如从:

# There's a Polar Bear
# In our Frigidaire--
foobar['brangelina'] <- 2
# He likes it 'cause it's cold in there.
# With his seat in the meat
foobar['billybob'] <- 1
# And his face in the fish

# There's a Polar Bear
# In our Frigidaire--
# He likes it 'cause it's cold in there.
# With his seat in the meat
# And his face in the fish
foobar['brangelina'] <- 2
foobar['billybob'] <- 1

据我所知:

grep foobar file.txt > newfile.txt
sed -i 's/foobar//g' foo.txt
cat newfile.txt > foo.txt

最佳答案

这可能有效:

sed '/^foobar/{H;$!d;s/.*//};$G;s/\n*//' input_file

编辑:针对 foobar 位于最后一行时的极端情况进行了修改

关于string - 如何将所有从 'foobar' 开始的行移动到文件末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8001762/

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