How to Delete a Line From a Text File using SED


Linux tips

Linux tips

Suppose you’re using GNU/Linux or FreeBSD.

Suppose you have a text file.

Suppose you want to delete all the text lines that contains a specif text.

Suppose you don’t know on which line it is, or suppose you have a lot of occurrences of these words.

The solution is simple. Use: SED.

Type:

sed -i "/text-i-want-to-remove/d" /path/to/file

Where text-i-want-to-remove is the text contained on the lines you’re going to remove and  /path/to/file is the absolute path to the file you want to clean.

text-i-want-to-remove can contains also spaces.

This is the way that’s going to modify the original file.

Suppose you want to keep the original file and you just need a new file with that lines deleted?

Then type:

sed "/text-i-want-to-remove/d" /path/to/file > new-file

Where new-file is the new text file (in the current directory) without the lines that contained text-i-want-to-remove.

That’s all.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...