Delete second-to-last line of file using sed
<p>I have a file.txt and I have to delete the second-to-last line of the file.
The below three sed commands are able to print the second-to-last line. But I need to delete that line. Any help??</p>

<pre><code>sed -e '$!{h;d;}' -e x file.txt

sed -n 'x;$p' file.txt

sed 'x;$!d' file.txt
</code></pre>

<p>$ cat file.txt<br>
apple<br>
pears<br>
banana<br>
carrot<br>
berry  </p>

<p>Can we delete the second-to-last line of a file:<br>
a. not based on any string/pattern.<br>
b. based on a pattern - if second-to-last line contains "carrot"</p>