0

I want to write a bash script that monitors a file for new lines and then potentially writes that new line to a different file if it passes a certain condition. This script needs to run continuously to monitor the file and constantly pipe these lines to the condition. I have been using tail -f filename.txt to monitor the file for growth, but I am unsuccessful in the latter two parts. Is this possible to do?

  • You should add more detail. What are the conditions? – guillermo chamorro Sep 19 at 20:57
  • 1
    Have you considered, directly processing the output of the process that created the file. – ctrl-alt-delor Sep 19 at 21:04
  • 1
    What have you tried in the latter two parts (you say that you were unsuccessful)? – ctrl-alt-delor Sep 19 at 21:05
0

Depending on how complicated your input to output is going to be you could use a script that handles each line like this:

tail -f 'filename' | xargs -L1 'script'

'script' would then be executed for each line with the line as arguments to it(accessed by "$@" in the script).

Rich in the comments suggested using grep or sed in the following way:

tail -f 'filename' | grep -e 'expr1' -e 'expr2' >> 'newfile'

or

tail -f 'filename' | sed 's/Conditions for line./Line that goes to new file./g' >>'newfile'

  • Could be simpler: tail -F filename|grep -e expr1 -e expr2>>output – Rich Sep 19 at 23:26
  • True, depends on what the poster needs it to do. I understood it to need more processing, but I added grep and sed options. – Pedro Sep 19 at 23:47
  • Thanks for incorporating my suggestion! I didn't draw attention to it, but I'm actually saying -F, which follows a file path which may be absent when tail is run, or become absent, or be replaced. Seems a better strategy for arbitrary stuff like this. I like your sed option - and sure, we could imagine all sorts of others like a perl or awk or ¯\_(ツ)_/¯ – Rich Sep 20 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.