循环遍历文件并删除某些行

Bil*_*ill 4 bash shell

我想循环一个文件并删除某些行.示例文件:

的test.txt

a
b
c
d
Run Code Online (Sandbox Code Playgroud)

是)我有的:

FILE=/tmp/test.txt
while read FILE
do
  # if the line starts with b delete it otherwise leave it there
  ?
done
Run Code Online (Sandbox Code Playgroud)

hek*_*mgl 8

这是一个工作sed- UNIX流编辑器:

(sed '/^b/d' yourfile > yourfile~ && mv yourfile~ yourfile) || rm yourfile~
Run Code Online (Sandbox Code Playgroud)

该命令将删除以a开头的所有行,b并将剩余行写入备份文件.如果成功,备份文件将重命名为原始文件,如果失败,备份文件将被删除.