I want to search in a file for specific words and then show only the words that were not found. So far, with my limited skills in this area, I can find which words are found:
egrep -w "^bower_components|^npm-debug.log" .gitignore
If my .gitignore file contained bower_components but not npm-debug.log it would return bower_components. I want to find out how to ask it to return only the part of the pattern that was not found. That is, I want it to return only npm-debug.log. I don't want to see all of the text from the file that does not match the search, only the single word from the file that was not found. How do I do that?
I don't have a one liner for you but a short script would work for your use case.
file=$1
shift
for var in "$@"
do
found=`grep "^$var" $file`
if [[ -z $found ]]
then
echo $var
fi
done
Explanation, take the first argument as the file name, any subsequent arguments as strings to test for in the file. Loop through those arguments and test the result of the grep command to see if it returned empty. If it did then the string wasn't found and we print it to the console.
Usage: [script name] .gitignore bower_components npm-debug.log
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