LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-04-2013, 05:35 PM   #1
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 0
Grep challenge!


Hello all!

I'm trying to put together a grep command that will search a file (audit.log) for USERNAME=n where n is an unknown string and DATETIME=n n where both n's represent two 3-character values (day and month).

Here's what I have so far:
Code:
grep -ow  "USERNAME=\w\{1,13\}\|DATETIME=\w\{1,3\}" audit.log* > test
And the output looks like this:
Code:
audit.log:USERNAME=USTRELA
audit.log:DATETIME=Wed
Obviously I'm getting the day, but the 3-character month (eg Jan, Feb, etc.) should be included to look something like this:
Code:
audit.log:USERNAME=USTRELA
audit.log:DATETIME=Wed Aug
Can anyone be so kind as to help me? I've played with the above command quite a bit, but no dice


Bonus if you can help me with the command to show just the month (I'm at a loss there and suspect I'd need to get awk involved...) :
Code:
audit.log:USERNAME=USTRELA
audit.log:DATETIME=Aug

Last edited by Nickjpost; 09-04-2013 at 05:40 PM.
 
Old 09-04-2013, 05:40 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Can you post a sample of the input file?
 
Old 09-04-2013, 05:45 PM   #3
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sycamorex View Post
Can you post a sample of the input file?
Thank you so much for your reply!

Here's a sample:
Code:
EVENTID=USER_PROTECTED
DATETIME=Wed Aug 21 12:19:05 EDT 2013
PROTECTED_APP_USERNAME=USTRELA
RESULT=xxxxxxx
SESSION_ID=xxxxxxx
APPLICATION=xxxxxxxx
USERNAME=USTRELA
TYPE=xxxxx
As you can see, the only fields I'm interested in are USERNAME=n and DATETIME=n n
 
Old 09-04-2013, 05:56 PM   #4
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Rep: Reputation: 1
Quote:
Originally Posted by Nickjpost View Post
Hello all!

I'm trying to put together a grep command that will search a file (audit.log) for USERNAME=n where n is an unknown string and DATETIME=n n where both n's represent two 3-character values (day and month).

Here's what I have so far:
Code:
grep -ow  "USERNAME=\w\{1,13\}\|DATETIME=\w\{1,3\}" audit.log* > test
And the output looks like this:
Code:
audit.log:USERNAME=USTRELA
audit.log:DATETIME=Wed
Obviously I'm getting the day, but the 3-character month (eg Jan, Feb, etc.) should be included to look something like this:
Code:
audit.log:USERNAME=USTRELA
audit.log:DATETIME=Wed Aug
Can anyone be so kind as to help me? I've played with the above command quite a bit, but no dice


Bonus if you can help me with the command to show just the month (I'm at a loss there and suspect I'd need to get awk involved...) :
Code:
audit.log:USERNAME=USTRELA
audit.log:DATETIME=Aug

awk '/^USERNAME|DATETIME/ { print $1" "$2 }' file

Last edited by mmhs; 09-04-2013 at 06:08 PM.
 
1 members found this post helpful.
Old 09-04-2013, 06:03 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Would that work for you?
Code:
awk  '/DATETIME|^USERNAME/ {print $1, $2 }' file.txt
It's awk, not grep but it seems to do the job.
 
1 members found this post helpful.
Old 09-04-2013, 10:19 PM   #6
Nickjpost
LQ Newbie
 
Registered: Dec 2010
Location: Boulder, CO
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sycamorex View Post
Would that work for you?
Code:
awk  '/DATETIME|^USERNAME/ {print $1, $2 }' file.txt
It's awk, not grep but it seems to do the job.
That did the trick, sycamorex! Hats off to you and mmhs!! I'm terrible with awk, but that seemed so much easier....I guess it's time to pick up an O'Reilly Sed & Awk book, lol
 
  


Reply



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
grep to file outputs more than grep to screen? tcpman Linux - Server 4 06-07-2013 04:46 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:25 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