0

I have a script which I want to delete every line after a specific line.

I have a command, but it just wipes the entire file?

sed -n '/twm &/q;p' ~/.xinitrc > ~/.xinitrc

Convert something like:

some line
another line

some other line twm & more lines to be deleted and more

To

some line
another line

some other line

AAA
  • 151

1 Answers1

0

Since the parent process (your shell) sets up redirection before your sed command runs, the > ~/.xinitrc clears the file before you can read it.

Read man sed, and do something like UNTESTED:

sed -i.bak  -e "/twm \&/,\$d" ~/.xinintc

This leaves a copy of the original file in ~/.xinitrc.bak.

waltinator
  • 4,865
  • Other common ways to love this is by writing to a temporary file that is later renamed or by piping to sponge (a moreutils tool). In the end, these three variation (counting the one in this answer too) does the same thing, either explicitly or behind the scenes. – Kusalananda Sep 26 '23 at 18:06