How do you delete all lines in a file that begin with "string" in sh? I was thinking about using the sed command.
To Remove the lines from the source file itself, use the -i option with sed command. If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.
Deleting Multiple LinesPress the Esc key to go to normal mode. Place the cursor on the first line you want to delete. Type 5dd and hit Enter to delete the next five lines.
The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.
grep -v '^string' yourfile.txt > stripped.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With