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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to identify the occurence of a pattern between a unique character?
# 8  
Old 12-09-2009
Quote:
awk: There is a regular expression error.
?, *, or + not preceded by valid regular expression
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.
# 9  
Old 12-09-2009
Code:
#!/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
Code:
$ 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


Last edited by ghostdog74; 12-09-2009 at 10:11 AM..
# 10  
Old 12-09-2009
Code:
nawk ' /{/{p++} /}/{p--;next} (! p){print} ' infile

# 11  
Old 12-09-2009
hello buddy
simple solution from a geek.
Code:
awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}'

regards.
# 12  
Old 12-09-2009
Quote:
Originally Posted by steadyonabix
Code:
nawk ' /{/{p++} /}/{p--;next} (! p){print} ' infile

you sure its what OP wants?

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

Quote:
Originally Posted by gaurav1086
hello buddy
simple solution from a geek.
Code:
awk 'BEGIN{count=0;}/(^\{#+$|^#+\}$)/{count++;if(count%2==0) print occ;}/bad/{if(count%2==1) occ++;}'

regards.
what are your results? please post.
# 13  
Old 12-09-2009
Quote:
Originally Posted by ghostdog74
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.
# 14  
Old 12-09-2009
Code:
$ 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

How to identify varying unique fields values from a text file in UNIX?

Hi, I have a huge unsorted text file. We wanted to identify the unique field values in a line and consider those fields as a primary key for a table in upstream system. Basically, the process or script should fetch the values from each line that are unique compared to the rest of the lines in... (13 Replies)
Discussion started by: manikandan23
13 Replies

4. Shell Programming and Scripting

Regex to identify unique words in a dictionary database

Hello, I have a dictionary which I am building for the Open Source Community. The data structure is as under HEADWORD=PARTOFSPEECH=ENGLISH MEANING as shown in the example below अ=m=Prefix signifying negation. अँहँ=ind=Interjection expressing disapprobation. अं=int=An interjection... (2 Replies)
Discussion started by: gimley
2 Replies

5. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

6. UNIX for Dummies Questions & Answers

How to search unique occurence in a file?

Hi, I have to search and count unique occurence of DE numbers in bold below in a file which has content like below. Proc Tran F-BUY Item Tkey Q5JV Item Tsid JTIZ9 Item Tdat 20091001 Item Tset 20091001 Item Tbkr 5 Item Tshs 2 Item Tprc 897.0 Item Tcom 2000.0 Item Tcm1 20091001... (6 Replies)
Discussion started by: akash028
6 Replies

7. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

8. UNIX for Advanced & Expert Users

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution.:) (1 Reply)
Discussion started by: sumit207
1 Replies

9. UNIX for Dummies Questions & Answers

Counting occurence of a particular character on each line

Hi, I have the following data in a flat file: abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 abcd_efgh_ijkl_20080522.dat|20080602222508|1357 I need to check the no. of occurence of "|" (pipe) on each line and the output should look like below:... (4 Replies)
Discussion started by: hey_mak
4 Replies

10. Shell Programming and Scripting

First occurence of character in a file

Hi All I have the following contents in a file say in a file name called 'FILE1' *********** Start of the file ************** SANDIO000456GROJ8900 SANDIO2338923GRJH900 *********** End of the file ******************* I want to cut the first line which has the characters.... (6 Replies)
Discussion started by: dhanamurthy
6 Replies
Login or Register to Ask a Question