I have quite a few txt files. They contain the word 'Minimum' (it only shows once in each file), and I want to get the line that contains this word, and show the line in a new file.
To do this, I use this command:
grep 'Minimum' file1.txt > new1.txt
I did this to each file (file1, file2, etc.), and got lots of newn.txt
. But it turns out that some files don't have the word 'Minimum', so their newn.txt
are empty. Well, if grep can't get the word, why doesn't Linux give me an error message? Why does it continue and make the newn.txt? Giving me some empty files is really annoying.
So is there a way that I can stop it from creating the newn.txt
if it can't find the word?
Also, I'm thinking about putting it into a bash script - the grep command is the first step, and then in the second step I'll do something to the newn.txt
. So if grep can't find the word, or if there's an empty file created, I want the script to stop and not to do the second step. Is there a way to do this?
grep
isn't going to give an error just because the pattern isn't found. It doesn't work in that way. If you have a script and you want it to stop after a certain condition then you need to add its contents to your question.>
redirection operator, creating (or opening, if it already exists) the file is the first step, done by the shell before executing thegrep
command