LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2013, 03:06 AM   #1
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Rep: Reputation: 15
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.
 
Old 03-12-2013, 05:59 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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.
 
Old 03-13-2013, 06:19 AM   #3
ksri07091983
Member
 
Registered: Nov 2007
Location: Chennai,TamilNadu,India
Distribution: RedHat,SuSE
Posts: 65

Original Poster
Rep: Reputation: 15
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

Thank you once again!
 
Old 03-13-2013, 10:53 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 10:21 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
[SOLVED] Pattern Matching kdate Linux - Newbie 4 05-23-2011 05:27 PM
Pattern Matching Aveltium Linux - Newbie 5 04-12-2009 11:14 PM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration