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 12-05-2008, 03:00 PM   #1
jpostma
Member
 
Registered: Jun 2003
Location: Alphen ad Rijn -- The Netherlands
Distribution: Fedora Core 2 - Slackware 12
Posts: 119

Rep: Reputation: 15
Parsing a log file


Hi,

I have a big log file (access.log). I'm only interested in the lines that contains the words '404' and 'class' (both in the same line). How can I create a new file with only these log lines?

Johannes
 
Old 12-05-2008, 03:12 PM   #2
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 308

Rep: Reputation: 39
Easy way if you're not worried about the sorting of the file:

grep 404 access.log > /tmp/parsed.txt && grep class access.log >> /tmp/parsed.txt
 
Old 12-05-2008, 03:15 PM   #3
jpostma
Member
 
Registered: Jun 2003
Location: Alphen ad Rijn -- The Netherlands
Distribution: Fedora Core 2 - Slackware 12
Posts: 119

Original Poster
Rep: Reputation: 15
Thank you!
 
Old 12-05-2008, 03:24 PM   #4
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 308

Rep: Reputation: 39
Actually, that was terrible advice. Pattern match will be quicker:

Code:
grep "404\|cron" access.log > /tmp/newfile.txt
Sorry, not wearing my admin hat this evening.
 
Old 12-05-2008, 03:31 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
If I'm reading correctly, he only wants a match where both '404' and 'class' are on the same line.

In that case I would use something like:
%cat access.log | grep '404' | grep 'class' > output.txt
 
Old 12-05-2008, 03:58 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Or to save oneself two invocations of grep and the cat:
Code:
awk '/404/ && /class/' access.log > extract
Oh, and if the two patterns are always in the
same order a simpler one-grep approach would
also work, it they're not a slightly lengthier
(and uglier) solution using one grep could be found.
Code:
grep "404.*class" access.log > extract
egrep "(404.*class|class.*404)" access.log > extract
Personally I find the awk solution cleanest ... ;)


Cheers,
Tink

Last edited by Tinkster; 12-05-2008 at 04:07 PM.
 
  


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
parsing who logs in from the log files. snowman81 Programming 3 03-11-2008 11:55 AM
Help on parsing a log file in BASH globemast Programming 5 01-11-2007 01:56 AM
Log Parsing Program For Breezy seattlegeek Ubuntu 1 10-28-2005 10:41 AM
Help Parsing Log files blacky777 Linux - Security 6 04-21-2004 01:06 PM
parsing log files jstu Linux - General 4 06-10-2003 07:26 PM

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

All times are GMT -5. The time now is 09:26 PM.

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