Delete a line from text file with line number using sed

 

Sed is a stream editor for filtering and transforming text.

With sed we can delete a line using its line number without opening the text file.

Lets test with a example:

$ cat input.txt 

1

2

3

4

input.txt contains 4 lines with content as shown above.

Now I would like to delete line number 3 from input.txt

$ sed -i "3d" input.txt

$ cat input.txt

1

2

4

After running sed command as shown above just check the input.txt file where we can see that line 3 will be deleted.

-Sany

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s