Reputation: 473
Suppose I use
find
(some stuff) | wc -l
Then I have output like
14 file1.txt
29 file2.txt
32 file3.txt
1 tile4.txt
I'm completely new to Bash scripts but what I want to happen is that I can use this output right away after the command to write to another file. For example, if the count is greater than 10, I want to write "ALERT! Count for file#.txt is greater than 10!"
in myotherfile.txt
.
Thanks for any help
Upvotes: 0
Views: 173
Reputation: 1495
awk
can do the trick.
find [some stuff] | wc -l | awk '$1 > 10 {print "ALERT! Count for " $2 " is greater than 10!"}' > myotherfile.txt
Note that this will overwrite myotherfile.txt
everytime you run the command. If you want the lines to be added to the end of the file instead every time you run it, use >>
instead of >
.
Upvotes: 0
Reputation: 753725
find ... -exec wc -l {} + |
while read count file
do
if [ $count -gt 10 ]
then echo "ALERT! Count for $file is $count" >>myotherfile.txt
fi
done
The residual problem is buffering in the pipeline; there isn't an easy way to stop that. The use of >> myotherfile.txt
is partially to address that. It would be simpler in some respects to use redirection on the loop as a whole (done > myotherfile.txt
, with no >>
redirection), but there would be more buffering like that.
Note that your proposed pipeline:
find ... | wc -l
will not count the lines in each file; it will only count the number of lines generated by the find
command.
Upvotes: 2