Script to Serach pattern and give number of occurrences


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Serach pattern and give number of occurrences
# 1  
Old 02-19-2014
Script to Serach pattern and give number of occurrences

Hi,

I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file.

lets say i have a file test.txt contents as below

Code:
good is good
but good is sometime bad and sometime good
you are very good and good is always good

So the script should search for pattern "good" and give me output as "7" as good appears seven time

Now my concern is

I tried in linux with code. It is giving me desired output.
But on SunOS "-o" not working.

Code:
grep -o good test.txt|wc -w

So please advise script by which I can achieve it with sed/awk or any other command.
Many thanks
# 2  
Old 02-19-2014
Code:
akshay@Aix:/tmp$ awk '{count+=gsub(/good/,x)}END{print "Total is "count}' file
Total is 7

Code:
akshay@Aix:/tmp$ cat file
good is good
but good is sometime bad and sometime good
you are very good and good is always good

Use nawk on Solaris/Sun OS
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 02-19-2014
Excellent mate it worked.Thanks
Could you please explain how its works and what is gsub
# 4  
Old 02-19-2014
Quote:
Originally Posted by sv0081493
Excellent mate it worked.Thanks
Could you please explain how its works and what is gsub
The gsub() ( global substitution ) function returns the number of substitutions made, it's just summed up and printed in END block.
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 02-19-2014
great thanks..one more question
Why gsub didnt work with awk.
# 6  
Old 02-19-2014
Quote:
Originally Posted by sv0081493
great thanks..one more question
Why gsub didnt work with awk.
Pardon me, I didn't get your question, could you please elaborate your problem.
# 7  
Old 02-19-2014
Hi.

There is a ggrep on some Solaris systems:
Code:
$ ggrep -o good data1|wc -w
       7

for:
Code:
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86
ggrep grep (GNU grep) 2.5

See if you have ggrep (and many other GNU utilities):
Code:
$ which ggrep 
/usr/sfw/bin/ggrep

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Bash script to give multiple choices and a varying number of answers

Hello everybody, I use `case' quite a lot but , excellent as it is , it only gives one final result ; can anyone suggest a way whereas , say long lists of choices are given and I , or a user could select either one two or any number of results to be echoed . many thanks in... (3 Replies)
Discussion started by: V686
3 Replies

3. Shell Programming and Scripting

Serach a string for multiple occurrences in a file and cut related texts

Hi, I want a help in this forum for my below issue. 1. I have a file where I'm searching for a text . 2. When I get the line where the above string is present I want to cut some texts from the line where the particular string was found. These above two steps will repeat in the... (2 Replies)
Discussion started by: bhaski2012
2 Replies

4. UNIX for Dummies Questions & Answers

Serach pattern in one field and replace in another

Hi all, I have a TAB separated file like this: sample.rpt: 54 67 common/bin/my/home {{bla bla bla}} {bla bla} Replace Me 89 75 bad/rainy/day/out {{ some bla} } {some bla} Dontreplace Me ...... ...... I wish to do a regexp match on the 3rd... (2 Replies)
Discussion started by: newboy
2 Replies

5. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

6. UNIX for Dummies Questions & Answers

Serach a pattern

Hi, I am trying to find a particular patter in multiple UNIX files (also contain system files,hidden files and normal files) i am now using CMD: egrep -ali 'pattern' * i am not getting the required result, i just need files path and finename Naveen (3 Replies)
Discussion started by: Naveen_5960
3 Replies

7. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

8. Shell Programming and Scripting

pattern serach using grep

Frds I have to search for a string which is starting with brighton which will be first in the line of a text file test1.txt. The contents of test1.txt are file names like ----- brighton brighton_gt hst_brighton gst_brighton -------so many files------ --------- i have retrieve only... (3 Replies)
Discussion started by: KiranKumarKarre
3 Replies

9. UNIX for Dummies Questions & Answers

Pattern Matching - serach and replace script

My requirement is to replace a a particular pattren in a script from A to B. I am not sure if this can be done through sed command or through awk . The file sv.inc is window DialogBox AddConnection tag "~ActiveApp/Add Connection - Provider Type?URL" I would wnat the file to be... (10 Replies)
Discussion started by: bsandeep_80
10 Replies

10. Shell Programming and Scripting

serach and replace a specific pattern or value in a xml file

can some one help me with a perl command i have to search and replace a version from a xml-file so i use in a ksh script a command like this ssh $GLB_ACC@$GLB_HOST "/usr/contrib/bin/perl -pi -e "s/$curVersion/$new_Version/g" $Dest_dir/epi.xml" this command worked so far, but the problem... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question