Count Pattern using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Count Pattern using awk
# 1  
Old 09-01-2018
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


Code:
awk 'BEGIN{~/^4/{C++}};END {print"<Total>"} {print"<Reg>"}{print "<value>"C"</value></Reg>"}' {print"</Total>"} temp > test.txt

# 2  
Old 09-01-2018
I'm afraid your sample code is entirely screwed up. Try (untested) (sample data would have been nice!):

Code:
awk '
/^4/   {C++}
       {print "<Reg><value>" C "</value></Reg>"}
END    {print "<Total>" C "</Total>"}
' temp

# 3  
Old 09-01-2018
Other guesses:

Code:
awk 'BEGIN{ print "<Total>" }/^4/ {C++; print "<Reg><value>"C"</value></Reg>"}; END {print "</Total>"}' temp

Output
Code:
<Total>
<Reg><value>1</value></Reg>
<Reg><value>2</value></Reg>
<Reg><value>3</value></Reg>
<Reg><value>4</value></Reg>
<Reg><value>5</value></Reg>
</Total>


Code:
awk 'BEGIN{ print "<Total>" } /^4/{C++} END{ print "  <Reg>\n    <value>"C"</value>\n  </Reg>\n</Total>"}' temp

Output:
Code:
<Total>
  <Reg>
    <value>5</value>
  </Reg>
</Total>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Combine 4 awk pattern count statements into 1

Hello fellow awkers, I am trying to combine the following awk statements into 1 so that the results are more accurate: awk '/\=\+/ { count++ } END { print count}' filename awk '/\=\?/ { count++ } END { print count}' filename awk '/\=\-/ { count++ } END { print count}' filename awk... (8 Replies)
Discussion started by: ux4me
8 Replies

3. Shell Programming and Scripting

awk pattern match and count unique in column

Hi all I have a need of searching some pattern in file by month and then count unique records D11 G11 R11 -------> Pattern available in file S11 Jan$1 to $5 column contains some records in which I want to find unique for this purpose I have written script like below awk '/Jan/ ||... (4 Replies)
Discussion started by: nex_asp
4 Replies

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

5. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

6. Shell Programming and Scripting

Awk Count Pattern problem.

I want to keep a count of a all the records processed in a input file. The input file would have a lot of data containing various information. Lets say I make a pattern that only prints out data with the amount $37.57. How would I go about keeping track of how many $37.57 appears? I have... (2 Replies)
Discussion started by: Boltftw
2 Replies

7. Shell Programming and Scripting

How to count the pattern in a file by awk

hello everybody, I have 3 files eg- sample1 sample2 sample3 each file contain word babu many times eg- cat sample1 babu amit msdfmdfkl babu abhi babu ruby amit babu I want to count only the count of babu ,how many times it appeared . (5 Replies)
Discussion started by: abhigrkist
5 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

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 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