Count Pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count Pattern
# 8  
Old 06-16-2014
In a substitution such as gsub in awk or s in sed, & means the matched text itself.
# 9  
Old 06-16-2014
Quote:
Originally Posted by techedipro
What is & used for ?

Code:
 
awk '{s += gsub(/SIN\*[YN]/,"&")}END{print s}' fileawk '{s += gsub(/SIN\*[YN]/,"&")}END{print s}' file

Code:
 gsub(r, s [, t])        For each substring matching the regular expres-
                               sion r in the string t, substitute  the  string
                               s,  and return the number of substitutions.  If
                               t is  not  supplied,  use  $0.   An  &  in  the
                           replacement text is replaced with the text that
                           was actually matched.  Use \& to get a  literal
                               &.   (This  must  be  typed as "\\&"; see GAWK:
                               Effective AWK Programming for a fuller  discus-
                               sion  of  the  rules for &'s and backslashes in
                               the replacement text of sub(), gsub(), and gen-
                               sub().)

# 10  
Old 06-16-2014
Thanks for the explanation!

How can I put the value of the count in the below xml structure


Code:
 
<count>
<value>$totalcount</value>
</count>


I tried this statement but the xml structure is looping for each input record

Code:
count.txt |awk 'BEGIN{print"<Totalcount>"}{print "<value>"$1"</value>"}END { print "</Totalcount>"}'> reccount.txt

# 11  
Old 06-16-2014
why don't you combine all in one like this
Code:
awk '{s += gsub(/SIN\*[YN]/,"&")}END{if(s)print "<Totalcount><value>"s"</value></Totalcount>"}' file

This User Gave Thanks to Akshay Hegde For This Post:
# 12  
Old 06-16-2014
Thank you Akshay and Neutronscott!
# 13  
Old 06-18-2014
For 90MB file the above command runs for several minutes , is there a way we can reduce the processing time ?

Thanks
# 14  
Old 06-23-2014
Can anyone optimize the above command , I am ruuning a 90 MB file and it is taking good amount of time to get the count of the pattern

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count Pattern using awk

I need to get a count of all the records that start with 4 and then print the value. I have the below statement but it is missing something, can someone help me to fix this awk 'BEGIN{~/^4/{C++}};END {print"<Total>"} {print"<Reg>"}{print "<value>"C"</value></Reg>"}' {print"</Total>"} temp >... (2 Replies)
Discussion started by: rosebud123
2 Replies

2. 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

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. UNIX for Dummies Questions & Answers

Count pattern occurences

hi, I have a text..and i need to find a pattern in the text and count to the no of times the pattern occured. i have used grep command ..but the problem is , it shows the occurrences of the pattern but doesn't count no of times the pattern occuries. (5 Replies)
Discussion started by: nvnni
5 Replies

5. Shell Programming and Scripting

Pattern search and count

Hi all, I need to search the database log find out the most frequently used tables for a certain period of time. The search pattern is : the database.table so, i need to look for ABCD.* in the entire log and then need the top ten tables. I thought of using awk, search for the pattern ... (7 Replies)
Discussion started by: ysvsr1
7 Replies

6. Shell Programming and Scripting

count lines in a pattern

Hi, I had posted few days back and got replies on how to extract patterns from a file. I had another question. I want to count the number of lines a particular pattern. I thought of somethings like using NF variable, etc, but they didnt work. Here is sample input. ... (9 Replies)
Discussion started by: sandeepk1611
9 Replies

7. Shell Programming and Scripting

Filename pattern count

Hi I have the following files in a directory - ds_pl_W_Test ds_pl_list_Test ds_pl_list_Test ds_pl_listl_Test ds_pl_file_Test ds_pl_xx_Test I would like to count the number files in the directory that contain the word "list" I tried the following but it returns zero - ... (3 Replies)
Discussion started by: Prega
3 Replies

8. 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

9. Shell Programming and Scripting

pattern search and count

i want to search a word in a file and find the count of occurences even if pattern occures twice in a same line. for example file has the following content. yes no no nooo yees no yes if I search for "no" it should give count as 4 Pls help. Thanks (9 Replies)
Discussion started by: RahulJoshi
9 Replies

10. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 Replies
Login or Register to Ask a Question