例如,如果我的 Linux 系统上的文件中有以下文本:
10-02-2020
given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
In addition, two variant programs egrep and fgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F
16-02-2020
The top program provides a dynamic real-time view of a running
system. It can display system summary information as well as a list
of processes or threads currently being managed by the Linux kernel.
The types of system summary information shown and the types, order
and size of information displayed for processes are all user
configurable and that configuration can be made persistent across
restarts.
如何删除之前的所有文本16-02-2020
并将文件转换为:
16-02-2020
The top program provides a dynamic real-time view of a running
system. It can display system summary information as well as a list
of processes or threads currently being managed by the Linux kernel.
The types of system summary information shown and the types, order
and size of information displayed for processes are all user
configurable and that configuration can be made persistent across
restarts.
您可以使用
sed
. 一般格式为:除非明确告知 with ,否则不打印的
-n
原因。Som 该命令将打印介于行匹配和一个匹配(包括)之间的所有行。如果有多个匹配项,将打印多行。sed
p
pattern1
pattern2
在您的情况下,您希望打印所有内容,直到文件结束,因此
pattern2
将是$
. 因此,您正在寻找这个:在不相关的注释上,
fgrep
并且egrep
已弃用,您应该使用grep -F
andgrep -E
。见man grep
:Perl 版本:
我认为,对于从文件中修剪块,
ed
将文件视为一个整体,而不是像这样一次处理一行sed
是更好的选择:删除从第一行到前一行的所有内容,
16-02-2020
并保存修改后的文件。如果在脚本中使用,您可以使用 heredoc 将命令发送到ed
: