You guys are really smart, so I'm hoping someone can help me out with this. I would like to match the third line occurrence of a pattern at the beginning of a line in a file and replace it. I'm ok at using sed but I have no clue about this one. Thanks in advance.
What have you tried so far?
sed "s/^pat1/pat2/" file
But I know it will do it for every occurrence. I've looked through some of the sed options but I can't figure out how to use them for what I'm trying to do.
Please provide data sample.
Maybe I should be more clear with the issue I'm having. I want to find the third occurrence at the beginning of a line. So if I have a file that reads:
blah blah
blah
pat1 blah
pat1
pat1 blah // <- find this one and replace with pat2
pat1
.
.
blah
I want to find the third pat1 and overlook the first two.
something like:
# awk '{CNT[$1]+=1; if ( $1=="pat1" && CNT[$1]==3 ){print "pat2"} else {print $0}}' blah
blah blah
blah
pat1 blah
pat1
pat2
pat1
should work