How to remove a text from file/files using grep
However in practice we need to remove some text from a file or from multiple files. Removing text from files manually is a tough task. Here is a sample example to do this:
Create a script
- Create a file like
- Write following lines of code in that file
You can use any extension in place of txt according to your requirement.
for thefile in *.txt ; do grep -v "YOUR_TEXT_TO_REMOVE" $thefile > $thefile.$$.tmp mv $thefile.$$.tmp $thefile done
grep -v "YOUR_TEXT_TO_REMOVE" $thefile > $thefile.$$.tmp returns the content of all
.txt files in that directory
YOUR_TEXT_TO_REMOVE and write that output in a tempfile then
replace new file with that original one.
- To run script just run