Sed - delete text from a position until the beginning of the file

Asked

Viewed 648 times

0

I have this command that erases from a position until the end of the file, but how to delete from this position to the beginning of the file?

sed -i '/Enviar mailBlogThis/,$d' *.txt

1 answer

1


Just use sed -i '1,/destino/d' *.txt

Explanation:

sed -i '1,/Enviar mailBlogThis/d' arquivo.txt
    └┬┘└┬┘ └────────┬────────┘└┬┘ └────┬────┘
     │  │           │          │       └─ Arquivo(s)
     │  │           │          └───────── Deleta as linhas encontradas via RegEx
     │  │           └──────────────────── Limite para remoção
     │  └──────────────────────────────── Começa deletar da primeira linhas
     └─────────────────────────────────── Altera o arquivo

txt file.

1
2
3
4
5
Enviar mailBlogThis
6
7
8
9
0

Exit:

6
7
8
9
0
  • A problem has occurred, if the file does not have the term (destination) it erases everything from the file

  • You can use the grep, for example: grep "Enviar mailBlogThis" file.txt && sed -i '1,/Enviar mailBlogThis/d' file.txt

  • Now it worked perfectly, thank you @Valdeir

Browser other questions tagged

You are not signed in. Login or sign up in order to post.