Skip to content

Use the sed or grep command in Linux to remove all lines from a file that start with a specific character

You can use the sed or grep command in Linux to remove all lines from a file that start with a specific character.

If you want to edit the file in place, and your version of sed supports the -i option, you can use sed as follows:

sed -i '/^;/d' filename

The ^; is a regular expression that matches lines that start with ;, and d tells sed to delete those lines. -i tells sed to edit the file in place.

If your version of sed does not support -i or you do not want to edit the file in place, you can redirect the output to a new file:

sed '/^;/d' filename > newfile

If you prefer grep, you can use the -v the option which inverts the matching, to exclude lines that start with ;:

grep -v '^;' filename > newfile

This will print all lines that do not start with ; to newfile.

Remember to replace filename with the name of the file, you want to modify.

Published inBash

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *