LinuxQuestions.org
Visit Jeremy's Blog.
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, 04: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, 04:12 PM   #2
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 307

Rep: Reputation: 38
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, 04: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, 04:24 PM   #4
arckane
Member
 
Registered: Sep 2005
Location: UK
Distribution: Gentoo/Debian/Ubuntu
Posts: 307

Rep: Reputation: 38
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, 04: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, 04:58 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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 05:07 PM.
 
  


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


All times are GMT -5. The time now is 09:16 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration