Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a line using SED from a file and redirect the output to same file

I use sed command to delete first two lines and last line from a file and i want to redirect the output to the SAME file

EXAMPLE :

sed '1,2d' '$d' pra >pra

this deletes first two lines & last line from the file .But when i try to redirect the output to Same file , an Problem Occurs .

when i open and see the the redirected file pra nothing is present inside the file .

IS there a better way of doing this ..deleting first two lines and last line from a file and and saving it with the same file Name .

like image 963
pravin_23 Avatar asked Oct 31 '25 15:10

pravin_23


1 Answers

To save output to same file, use --in-place or -i modifier.

E.g.

sed -i options_you_want name_of_file_to_change

It's much neater than redirecting to temp files, and then back to original file.

As pointed out below, --in-place may not be available on Solaris...

like image 110
pokero Avatar answered Nov 03 '25 11:11

pokero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!