Lets says I have a file called test.log
more rows being added all the time:
one|two|apple|four|foo1
one|two|pear|four|foo2
one|two|apple|four|foo3
one|two|peach|four|foo4
I want to tail it but am only interested in lines that say apple, and I only want to see the string after the last pipe.
To filter by apple I can use:
tail -f test.log | grep apple
This outputs:
one|two|apple|four|foo1
one|two|apple|four|foo3
Now to get the string after the last pipe I thought I could use:
tail -f test.log | grep apple | awk -F '|' '{print $(NF)}'
But this outputs nothing.
If I remove the -f
option from tail it works but then I can't watch my log file.
The problem is awk hangs as described in this answer. I could not make the solution outlined there work for me.
So maybe using cut would be easier than awk. There are a set number of pipes so I could just do:
cat test.log | grep apple | cut -d\| -f 5
Outputs:
foo1
foo3
But when I watch with:
tail -f test.log | grep apple | cut -d\| -f5
Again I get no output.
Is there an one liner I can use to tail this file, filter it, and extract the last bit?
tail -f test.log | awk -F '|' '/apple/{print $NF}'
. – terdon Sep 10 '21 at 10:39