Home > OS >  Grep a text file for a specific file extension
Grep a text file for a specific file extension

Time:12-30

I have a diff output in output.txt. Out of everything possibly listed in that diff, we do not want to have in that txt file a series of specific file types.

Goal: take output.txt, grep -v into a new text file everything NOT containing specific file extensions.

So I was trying to get grep -v to work but all the variations I'm trying are not working in some capacity.

I'm trying to remove the following as an example. Note all * symbols for this question mean wildcard anything

*.log
*.a
rz_build_*

while the 3rd one is slightly different and possibly easier, its the first 2 I am having an issue with. I tried a few things ending with these

grep -v "\.log" output.txt
grep -v "\.a" output.txt

reading that "\." will treat the . as an actual character and not a variable this nearly works, except for ".a" its removing anything with an ".a" and $ will not work with this to tell it to STOP at .a so its grabbing other files like ".asc".

Log is working due to it being a bit more unique but if i wanted to not remove ".logs" if they were in there it would grab that too.

Is there a way to make this work or a more direct other method to remove all of a specific file type from a text file? All searches seem to lead to how to remove a specific file type from a directory, none about a text file.

CodePudding user response:

You can use \b around the file extention egrep -v '\.\b(a|log)\b' output.txt. This will match .a and .log, but not .ab or .logs

CodePudding user response:

You can use extended grep for this, something like:

egrep -v ".log|.a" output.txt

Extended grep gives the possibility to use a separator for filtering multiple results.

  • Related