#!/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
#!/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"
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
#!/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
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
$ 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
$
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
if I tweak the regex to have more metacharacters ;-).
regards.