|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. Code:
>>{
>>hi
>>GoodMorning
>>how are you?
>>}
>>is it good,
>>tell me yes, if it is goodIn 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 Last edited by zaxxon; 12-09-2009 at 05:03 AM.. Reason: code tags please |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Code:
awk '/{/,/}/ {if (tolower($0) ~ /good/) {z++}} END{print z}' z=0 infile |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Quote:
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 |
|
#4
|
||||
|
||||
|
how about: Code:
sed -n -e '/{/,/}/ {/good/I p}' file.txt |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Code:
#!/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
doneI think awk could do it easier |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Code:
#!/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" |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Quote:
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 |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to search unique occurence in a file? | akash028 | UNIX for Dummies Questions & Answers | 6 | 10-05-2009 09:39 AM |
| How to count the occurence of a character in a line | sumit207 | UNIX for Advanced & Expert Users | 1 | 11-26-2008 08:00 AM |
| Sed and replacing one occurence of pattern | ss9u | Shell Programming and Scripting | 6 | 07-16-2008 02:06 PM |
| First occurence of character in a file | dhanamurthy | Shell Programming and Scripting | 6 | 05-13-2008 12:56 AM |
| Pattern occurence in a file | videsh77 | UNIX for Dummies Questions & Answers | 5 | 12-10-2004 06:41 AM |
|
|