Por exemplo, se tiver o seguinte texto em um arquivo no meu sistema 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.
Como posso excluir todo o texto antes 16-02-2020
e transformar o arquivo neste:
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.
Você pode fazer isso com
sed
. O formato geral é:As
-n
causassed
não são impressas, a menos que sejam explicitamente informadas comp
. Som esse comando imprimirá todas as linhas que caem entre uma correspondência de linhapattern1
e uma correspondênciapattern2
(inclusive). Se houver várias correspondências, várias linhas serão impressas.No seu caso, você quer imprimir tudo até o final do arquivo, então
pattern2
será$
. Portanto, você está procurando por isso:Em uma nota não relacionada
fgrep
eegrep
estão obsoletos, você deve usargrep -F
egrep -E
. Vejaman grep
:Versão Perl:
Para remover um bloco de um arquivo, acho
ed
que , que trata o arquivo como um todo, em vez de agir em uma linha de cada vezsed
, é uma escolha melhor:Exclui tudo, desde a primeira linha até a linha anterior à que começa
16-02-2020
e salva o arquivo modificado. Se usado em um script, você pode usar um heredoc para enviar comandos paraed
: