Reputation: 41
I'm trying to use AWK to determine how many lines in a file contain a string.
BEGIN {RS=FS}
{if ($0 ~ /ed/) n++}
END {print n}
Works fine as long as ed is preceded by white space no matter what follows it, such as ed or education, but if the line contains muted or fed, it doesn't register them. I thought /.*ed/ would correct this but no such luck.
Upvotes: 1
Views: 146
Reputation: 207455
Or just use grep
with the -c
option to count occurrences:
grep -c ed file
4
Upvotes: 3
Reputation: 41456
Just remove BEGIN {RS=FS}
and you should be fine.
cat file
ed
education
not here
covered
fed
not here
awk '/ed/ {n++} END {print n}' file
4
$0~/ed/
is the same /ed/
. It will search the line and if any ed
is found increment the counter.
So in my example, 4
ed
are found.
Upvotes: 2