LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Query about conditional pattern matching (https://www.linuxquestions.org/questions/programming-9/query-about-conditional-pattern-matching-4175453732/)

ksri07091983 03-12-2013 03:06 AM

Query about conditional pattern matching
 
Hello All,

I am trying to count the number of users logged in a particular day using the last and filtering with 'awk, like below:

Code:

last | awk '/Mar 12/{count n++}END{print n}'
It worked fine, until I encountered a server whose wtmp contained login information more than a year. so obviously my filtering method above lists current year as well as previous year's info.

Is there any sub-programming in awk or sed to
a) print the lines that contain the pattern
b) stop parsing the file, once the line that does not contain pattern had been reached.

grail 03-12-2013 05:59 AM

Maybe use the -F option for last and then limit the regex to be between the dates you are looking for.

As for stop parsing the file, you can use the exit command when you reach the point at which you wish to stop.

ksri07091983 03-13-2013 06:19 AM

Hi Grail,

Thanks for the suggestions!.

I added the 'day of the week' in the regex and excluded the previous year's information from the output

Code:

last | awk '/Mon Mar 11/{count n++}END{print n}'
However, for non Linux platform, like AIX, last output didnt had a 'day of the week' field. so i used

Code:

last | sed '1,/<PREVIOUS DATE>/!d' | sed -n '/<REQUIRED DATE>/p' | wc -l
and it seems to be working :scratch:

Thank you once again!

grail 03-13-2013 10:53 AM

hmmm ... my suggestion was the option for last not awk, just to be clear.


All times are GMT -5. The time now is 05:04 PM.