We may also want to count the number of rows returned by the filter. For example, using the internal NR variable will show all rows and not just matched rows; for us to be able to report the number of users that have logged in, we must use our own variable. The following code will maintain the count within the variable that we name cnt. We increment this using the C style ++ for each iteration of the main code block.
We can use the END code block to display the closing value of this variable:
!(/Never logged in/ || /^Username/ || /^root/) { cnt++ print $0; } END { print "========================" print "Total Number of Users Processed: ", cnt }
We can see from the following code and output how this appears on my system:
From the display output, we can now see that we show only users that have logged in and, in this case, it is just the single user. However, we may also decide that we want to abstract the data further and display only certain fields from the matched rows. This should be a simple task but it is complicated, as the number of fields will vary depending on how the login was executed.