I use grep to pull lines that match 0.in multiple files. For files that do not contain 0., I want it to output "None" to the file. If it finds matches, I want it to output matches to a file. I have two sample files that look like this:
$ cat sea.txt
shrimp 0.352
clam 0.632
$ cat land.txt
dog 0
cat 0
In the example files, I got both lines from sea.txt, but from the file land.txtI would just get "None" using the following code:
$ grep "0." sea.txt || echo "None"
The double channel ( ||) can be read as "make foo or otherwise make bar", or "if not foo then bar". It works fine, but the problem I encountered cannot force it to output either matches (as it would be in the file sea.txt), or "None" (as it would be in the file land.txt) to the file. It always prints on the terminal. I tried the following, as well as others, without the knowledge of the output of the file saved to the file:
grep "0." sea.txt || echo "None" > output.txt
grep "0." sea.txt || echo "None" 2> output.txt
Is there any way to make it save the file? If not, can I use the lines printed on the terminal?
source
share