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