Details:
I am reading a file in an incoming stream using tail -f
I am able to find and replace characters/strings in the stream using sed, something like:
tail -f a.log | sed 's/'`echo -e "\xnn"`'/'`echo "$(tput setaf 1)|sep|$(tput sgr0)"`'/g'
What the above achieves is give me a clear visual marker for the separator string (which is non-printable hex character nn in this case, nn might be 05 as an example, replacing it with a red (in this case) colored (via tput) string |sep|.
So I get something like
field **|sep|** field **|sep|** field **|sep|**
What I want is
field **|sep#1|** field **|sep#2|** field **|sep#3|**
So, the requirement is to have the nth match marked as such in the substitution string (|sepn| where n is the no. of match encountered)
Use Perl’s ability to evaluate replacement on the fly to keep track of number of replacements made:
Result:
As you see, you will have to manually reset separator counter on every line.