0

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

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 >.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.