linuxshellunixsedwc

Count total number of pattern between two pattern (using sed if possible) in Linux


I have to count all '=' between two pattern i.e '{' and '}' Sample:

{
100="1";
101="2";
102="3";
}; 
{
104="1,2,3";
};
{
105="1,2,3";
};

Expected Output:

3
1
1

Solution

  • A very cryptic perl answer:

    perl -nE 's/\{(.*?)\}/ say ($1 =~ tr{=}{=}) /ge'
    

    The tr function returns the number of characters transliterated.


    With the new requirements, we can make a couple of small changes:

    perl -0777 -nE 's/\{(.*?)\}/ say ($1 =~ tr{=}{=}) /ges'