Alim Hasanov
Alim Hasanov

Reputation: 307

bash: pipe continuously into a grep

Not sure how to explain this but, what I am trying to achieve is this: - tailing a file and grepping for a patter A - then I want to pipe into another customGrepFunction where it matches pattern B, and if B matches echo something out. Need the customGrepFunction in order to do some other custom stuff.

The sticky part here is how to make the grepCustomFunction work here.In other words when only patternA matches echo the whole line and when both patterA & patternB match printout something custom: when I only run:

tail -f file.log | grep patternA

I can see the pattenA rows are being printed/tailed however when I add the customGrepFunction nothing happens.

tail -f file.log | grep patternA | customGrepFunction

And the customGrepFunction should be available globally in my bin folder:

customGrepFunction(){

 if grep patternB 
  then
     echo "True"
  fi


 }  

I have this setup however it doesn't do what I need it to do, it only echos True whenever I do Ctrl+C and exit the tailing. What am I missing here?

Thanks

Upvotes: 0

Views: 240

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295954

What's Going Wrong

The code: if grep patternB; then echo "true"; fi

...waits for grep patternB to exit, which will happen only when the input from tail -f file.log | grep patternA hits EOF. Since tail -f waits for new content forever, there will never be an EOF, so your if statement will never complete.

How To Fix It

Don't use grep on the inside of your function. Instead, process content line-by-line and use bash's native regex support:

customGrepFunction() {
  while IFS= read -r line; do
    if [[ $line =~ patternB ]]; then
      echo "True"
    fi
  done
}

Next, make sure that grep isn't buffering content (if it were, then it would be written to your code only in big chunks, delaying until such a chunk is available). The means to do this varies by implementation, but with GNU grep, it would look like:

tail -f file.log | grep --line-buffered patternA | customGrepFunction

Upvotes: 3

Related Questions