Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count the number of times a string appears in a text file with only AWK?

Tags:

linux

awk

I am writing an AWK script which takes a list of dates and formats it into an easier to read list. I was able to do that no problem.

The real problem is this. After displaying the list, I want to display how many times "2010" appears in the text file.

How do I do this? I've been searching for days and I've come up with absolutely nothing.

like image 200
user1349126 Avatar asked Nov 29 '25 16:11

user1349126


1 Answers

awk -F"2010" 'NF>0 { count += NF-1 } END { print 0+count }
like image 133
Michael J. Barber Avatar answered Dec 02 '25 05:12

Michael J. Barber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!