How to identify the occurence of a pattern between a unique character? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 12-09-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 24 October 2014, 1:54 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,237
Thanks: 121
Thanked 453 Times in 412 Posts

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

Sponsored Links
    #3  
Old 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 12-09-2009
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 4 April 2014, 3:58 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 17 Times in 16 Posts
how about:
Code:
sed -n -e '/{/,/}/ {/good/I p}' file.txt

Sponsored Links
    #5  
Old 12-09-2009
frans's Avatar
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 12-09-2009
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 24 October 2014, 9:49 AM EDT
Posts: 2,662
Thanks: 0
Thanked 17 Times in 17 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 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 02:22 AM.