awk Help -- If match found return the count

Hi All,

I need to get the count of records in the file, if the passing parameter matches with the list of records in the file. Below is my example

source file: Test1.dat

20120913
20120913
20120912
20120912
20120912
20120912
20120912
20120913
20120913
20120912

In my script I am deriving the processing date. I need to check how many files are there with the current processing date. based on the return count I need to send a mail.

For the above file, If I pass 20120912 as parameter

Result

count_files=6

Please help me how to write a Awk script for this requirement.

Thanks,
Chandu.

DATE="20120913"
grep -c "$DATE" filename
1 Like

Got the answer with Awk

awk '/20120912/ { print $0 }' test.dat | wc -l

Try also

$ awk '/20120912/{cnt++} END{print cnt}' test.dat
6
1 Like

Hi! You can use also...

cat Test1.dat | nawk -v var="20120912" 'BEGIN{count=0}\
{if ($0 ~ var) ++count;}
END{print count}'
1 Like

You get a useless use of cat award. awk, and any other shell utility, does not need cat's help to read a file.

Whenever you have cat file | awk '{...}' you can always do awk '{...}' file to save yourself some bother and CPU time.