Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-09-2009
divak divak is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 10 September 2014, 5:18 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Hammer & Screwdriver 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.


Code:
>>{
>>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

Last edited by zaxxon; 12-09-2009 at 05:03 AM.. Reason: code tags please
Sponsored Links
    #2  
Old Unix and Linux 12-09-2009
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 4 September 2015, 8:47 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,318
Thanks: 137
Thanked 469 Times in 423 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 12-09-2009
divak divak is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 10 September 2014, 5:18 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by zaxxon View Post
Code:
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
    #4  
Old Unix and Linux 12-09-2009
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 31 August 2015, 7:10 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 18 Times in 17 Posts
how about:
Code:
sed -n -e '/{/,/}/ {/good/I p}' file.txt

Sponsored Links
    #5  
Old Unix and Linux 12-09-2009
frans's Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 4 December 2013, 9:39 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts

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
done

I think awk could do it easier
Sponsored Links
    #6  
Old Unix and Linux 12-09-2009
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts

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  
Old Unix and Linux 12-09-2009
divak divak is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 10 September 2014, 5:18 PM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ghostdog74 View Post
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"


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
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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



All times are GMT -4. The time now is 10:31 AM.