Insert, Update and Delete any Line without opening the file in Linux

Insert, Update and Delete any Line without opening the file in Linux

How comfortable would that be, if we ever have to Insert, Update, or Delete any line from a file without opening?

A few times, we are in a situation, where we had to get rid of a few lines or Insert a few lines in the file.

For the given purpose, we will be using Linux sed command line utility app.

For Inserting or Updating any Data in a specific line

Syntax:

sed -i 'Ni CONTENT_HERE' filename

here N represents line number, i represents insert and CONTENT_HERE would be your actual content, filename will be the filename where you want to insert data

Example

sed -i '10i WorldOfHacker back in 2010 was one of the largest hacking communities in India and across the world' worldofhacker.txt

The above command will insert the WorldOfHacker back in 2010 was one of the largest hacking communities in India and across the world text on the 10th line of the worldofhacker.txt file.

You can try your own homework for various other lines

For Deleting any Data in a specific line

Syntax:

sed `Nd` filename

here, N represents as usual line number d represents the delete action and filename is your filename where you want to delete the N line number.

Example:

sed '10d' worldofhacker.txt

The above command will delete the 10th line from the file worldofhacker.txt

For Deleting any Data in the Last line

Syntax:

sed `$d` filename

here, $ represents the last line number d represents the delete action and filename is your filename where you want to delete the N line number.

Example:

sed '$d' worldofhacker.txt

The above command will delete the last line from the file worldofhacker.txt

For Deleting any Data based on a range of lines

Syntax:

sed `S,Ed` filename

here, S represents the first starting line number and E represents the last or end line number d represents the delete action and filename is your filename where you want to delete the range of line contents.

Example:

sed '4,7d' worldofhacker.txt

The above command will delete the line from the 4th index up to the 7th index of the file worldofhacker.txt

For Deleting multiple lines from Data

Syntax:

sed `Nd;Pd;Od;` filename

here, N, P, and O represent various line numbers, d represents the delete action and filename is your filename where you want to delete the range of line contents.

Example:

sed '4d;7d;10d' worldofhacker.txt

The above command will delete the line from the 4th index, 7th index, and 10th index of the file worldofhacker.txt

For Deleting multiple ranges of Reverse conditional Data

Syntax:

sed `N,P!d` filename

here, N, and P represent various line numbers, !d represents the delete action except for these lines, and filename is your filename where you want to delete the range of line contents except N-P.

Example:

sed '4,10d' worldofhacker.txt

The above command will delete the other line except for the 4th index to the 10th index of the file worldofhacker.txt

Hope this crazy method helps you save time. You can do more about them just do man sed and you will find why Linux is Love!

Did you find this article valuable?

Support Hack, Build & Scale - Web & Server apps by becoming a sponsor. Any amount is appreciated!