How to identify the occurence of a pattern between a unique character?

hi,

is it possible to find the number of occurences of a pattern between two paranthesis.

for e.g

i have a file as below.

>>{
>>hi 
>>GoodMorning
>>how are you?
>>}
>>is it good, 
>>tell me yes, if it is good

In the above file, its clear the occurence of word "Good" is thrice.

But i need the output of only the count of occurence that exist between the { }, which is one here.

How could i do this??

Thanks

awk '/{/,/}/ {if (tolower($0) ~ /good/) {z++}} END{print z}' z=0 infile

Hi,

Im getting this error, when running the command given.
Error msg for rour reference.

awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression

Thanks

how about:

sed -n -e '/{/,/}/ {/good/I p}' file.txt
#!/bin/bash
read -p "Search pattern: " PAT
while true
L=""
do    
    until [ "$L" = "{" ]
    do 
        read L || exit
    done
    until [ "$L" = "}" ]
    do 
        read L || exit
        if $(echo $L | grep -q $PAT)
        then
        ...............
        fi
    done
done

I think awk could do it easier

#!/bin/bash
shopt -s nocasematch
while read -r line
do
    case "$line" in
       *{* ) flag=1;count=0;;
       *}* ) flag=0
        echo "count: $count"
       ;;
    esac
    if [ "$flag" -eq 1 ];then
            set -- $line
            for c in $@
            do
                case "$c" in
                    *good* )
                    count=$((count+1))
                esac
            done
    fi
done <"file"



Hi,

Thanks for the response,
what i need is
I have a file like this

------------------------------------
{##############
things are making me bad
bad things are good
thanks for your help
##############}
bad things make me worse
wore things are bad
bad is always good
good is not bad
-------------------------------

I need to count bad not in between {}
ie count = 4

It will be very helpful for me

If on Solaris, you might want to use gawk, nawk or /usr/xpg4/bin/awk.

You can also try to escape the curled brackets with a backslash to see if it makes a difference.

#!/bin/bash
shopt -s nocasematch
count=0
data=$(<"file")
IFS="}"
set -- $data
chunks=("$@")
c=${chunks[@]%%{*}
IFS=$'\n'
set -- ${c[@]}
w=("$@")
for i in ${w[@]}
do
    IFS=" "
    set -- $i
    for k in $@
    do
        #count "good".
        case "$k" in
            *good* ) count=$((count+1))
        esac
    done
done
echo "count: $count"

output

$ more file
------------------------------------
good GOOD{##############
things are making me bad
bad things are good or GOOD
thanks for your help
##############} blah
bad things make me worse
wore things are bad
bad is always good
good is not bad but is GOOD or gOOd
goodyear tires
-------------------------------

$ ./shell.sh
count: 7


nawk ' /{/{p++} /}/{p--;next} (! p){print} ' infile

hello buddy
simple solution from a geek.

awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}'

regards.

you sure its what OP wants?

---------- Post updated at 09:03 AM ---------- Previous update was at 09:01 AM ----------

what are your results? please post.

hello
good GOOD{##############
things are making me bad
bad things are good or GOOD
thanks for your help
##############} blah
bad things make me worse
wore things are bad | awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}'
2

give 2 as output which is intended, I suppose.

$ more file
good GOOD{##############
things are making me bad
bad things are good or GOOD
thanks for your help
##############} blah
bad things make me worse
wore things are bad

$ awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}' file
$

no output at my side.

hello ghostdog,
the input is like this as posted previously
{##############
things are making me bad
bad things are good
thanks for your help
##############}
bad things make me worse
wore things are bad

plz try on this and see.
Regards.

---------- Post updated at 07:51 PM ---------- Previous update was at 07:49 PM ----------

echo "{##############
> things are making me bad
> bad things are good
> thanks for your help
> ##############}
> bad things make me worse
> wore things are bad
> bad is always good
> good is not bad" | awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}'
2
localhost:/# paste

so it only works on that particular sample input. Not very flexible solution is it?

# more file
bad {##############
things are making me bad
bad things are good
thanks for your help
##############}
bad things make me worse
wore things are bad

$ awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}' file
$

gaurav@localhost:~$ echo "{##########
> things are bad enough
> to know bad things
> ##############}
> hi the bad boy has turned 
> up and he is " |  awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}'
2
gaurav@localhost:~$ 

---------- Post updated at 07:55 PM ---------- Previous update was at 07:54 PM ----------

hi ghostdog,
I did it a/c to the input. yes it can be very flexible indeed :slight_smile:
if I tweak the regex to have more metacharacters ;-).
regards.

please do so. thks