Count Pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count Pattern
# 1  
Old 06-16-2014
Wrench Count Pattern

I need to count the total no of SIN*Y and SIN*N patterns from the below file , The file does not have a record terminators , data in the file is streamed i.e. one line

I want to put this count in xml structure

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

Tried the following awk statements but since the file does not have any record terminators the output of count is always coming out as 0

Code:
awk -F "" '/SIN\*Y\*/{x++;}END{print x >>"count.txt"}' Test.txt

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

Input file Test.txt

Code:
 
ISA*xx*xyzzzz    *00*          *0x*xxxxxxxxx      *14*xxxxxxxxxxxxx  *140417*1005*^*00501*xxxxxxxxx*0*P*>~GS*xx*xxxxxxxxx*xxxxxxxxxxxx1*20140417*xxxxxx*xxx*X*005010X220A1~ST*xxx*xxxxxx*005010X220A1~BGN*00*xxxxxxxxxxxxxx*20140417*1005*Ex***4~N1*P5*xxxxxxxxxxxxx*FI*xxxxxxxxx~N1*IN*xxxxxxxx*FI*xxxxxxxxx~SIN*Y*18*xx*XN*A~REF*0F*xxxxxxxxx~REF*1L*xxxxxxxx~REF*ZZ*xxxxxxxxxxxxx~NM1*IL*1*xxxxx*xxxxxxxx*A***ZZ*00~PER*IP**HP*xxxxxxxxxx~N3*xxxxxxxxxxxxxxxxx~N4*xxxxxxxxxxxxxxx*208771193~DMG*xx*xxxxxxxx*x~HD*xxx**xxx*xxx~DTP*xxx*xx*xxxxxxxx~LX*1~NM1*xxxxxxxxx*XX*xxxxxxxxxx*72~PER*xxx*TE*xxxxxxxxxx*FX*xxxxxxxxxx~SIN*N*xx*xxx*XN*A~REF*xx*3xxxxxxxx~REF*xx*xxxxxxx2~REF*xxx00xxxxxxxxxxx~NM1*xxxx*Rxxxxxxx*xxxxxxxx*R***xx*0x~PER*xx**xx*xxxxxxxxxx~N3*xxxxxxxxxxxxxxxxxxD~N4*xxxxxxxxxx*xx*2xxxxxxx8~DMG*xx*xxxxxxx0*x~HD*xx1**xxO*xxx~DTP*xxx*xx*xxxxxxx2~LX*1~NM1*xxx2*xxxxxxxxx Mxxxxxxxxxxxxxxx-xx*******xx~PER*xx**xxxxxxxxxxxx7*xxx*xxxxxxxx6~SE*xx*xxxxxx~GE*x*xxx~IEA*x*0000xxxxx~

Thanks!
# 2  
Old 06-16-2014
If your grep has -o, you can try
Code:
$ grep -o 'SIN\*[YN]' input | wc -l
2

otherwise you need to be more resourceful. perhaps:
Code:
$ sed 's/SIN\*[YN]/\n&\n/g' input | grep -cx 'SIN\*[YN]'
2
$ perl -lne 'END {print $c} $c += s/SIN\*[YN]//g' input
2

# 3  
Old 06-16-2014
awk
Code:
$ awk '{s += gsub(/SIN\*[YN]/,"&")}END{print s}' file
2


Code:
$ awk '{s += gsub(/SIN\*[YN]/,"&")}END{f="count.txt";print s >f; close(f)}' test.txt

# 4  
Old 06-16-2014
oh and keeping awk, perhaps:
Code:
Removed. Akshay beat me to it

# 5  
Old 06-16-2014
Thanks Akshay!

Can you please explain the code
# 6  
Old 06-16-2014
Code:
awk '{s += gsub(/SIN\*[YN]/,"&")}END{print s}' file

function gsub returns the number of substitutions made, variable s holds sum of this count and printed in END block
# 7  
Old 06-16-2014
What is & used for ?

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

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