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

标签 string bash

假设我有一个脚本,其中有许多行以 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/

相关文章:

bash - 如何在CloudFormation中传入UserData作为参数

linux - 可以使用与管道命令的输出相同的输入文件吗?

c - 使用 fscanf 从文件中读取字符串、整数等

c# - 如何设置 DateTime 小时、分钟和秒?

linux - 使用 bash 将文件提取到一个 tar 文件中,但内部由许多 tar 文件组成

bash - 如何使用 sed 在给定行范围/模式内的匹配之前添加行?

java - 将一串数字拆分为单独的数字

java - 需要提取java中多行中存在的特定字符串

c++ - 将具有字符串成员的结构对象插入 std::vector 时出错

linux - 将命令保存到变量中而不是运行它