LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-26-2015, 09:43 AM   #1
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Filter a log to only the lines between two patterns


Both awk and sed can do this, but everything tried outputs nothing.
So, I have a huge log file from which I need to extract all events for a specific day.
Start pattern: "Tue Feb 24"
End pattern: "Wed Feb 25"

I used 'grep' to get the line numbers for start/end pattern like so:
Code:
grep -n "Tue Feb 24" /var/log/logfile.log |less 
grep -n "Wed Feb 26" /var/log/logfile.log |less
This now works, I can actually get the content:
sed '29694592,29897297!d' /var/log/logfile.log |wc -l
202706

Of course I could be satisfied by this, but I know it can be done with a simple one-liner - I just can't get it right.
Here is what I've tried, none of them outputs anything:
Code:
awk '/"Tue Feb 24"/ { show=1 } show; /"Wed Feb 25"/ { show=0 }' /var/log/logfile.log
awk '/"Tue Feb 24"/,/"Wed Feb 25"/' /var/log/logfile.log

sed -n '/"Tue Feb 24"/,/"Wed Feb 25"/p' /var/log/logfile.log
sed -n -e '/"Tue Feb 24"/,/"Wed Feb 25"/p' /var/log/logfile.log
sed '/"Tue Feb 24"/,/"Wed Feb 25"/!d' /var/log/logfile.log
sed -e '/"Tue Feb 24"/,/"Wed Feb 25"/!d' /var/log/logfile.log
Extract from logfile, the line with date:
<log realm="RemoteServer" at="Tue Dec 02 01:32:19 CET 2014.599">
 
Old 02-27-2015, 09:11 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
I came up with this after adapting from an earlier post http://www.linuxquestions.org/questi...0/#post5133189
Code:
awk '{split($6,t,":");split($8,y,".");e=mktime(y[1]" 12 "$5" "t[1]" "t[2]" "t[3]); if (e>mktime("2014 12 01 23 59 59") && e<=mktime("2014 12 03 0 0 0")) print $0}' logfile.txt
I have cheated by hardcoding the month as a numeral (as shown in bold) rather than converting the month string.
With this input
Quote:
<log realm="RemoteServer" at="Tue Dec 01 01:32:19 CET 2014.599">
<log realm="RemoteServer" at="Tue Dec 02 01:32:19 CET 2014.599">
<log realm="RemoteServer" at="Tue Dec 02 02:32:19 CET 2014.599">
<log realm="RemoteServer" at="Tue Dec 03 01:32:19 CET 2014.599">
I get this output
Quote:
<log realm="RemoteServer" at="Tue Dec 02 01:32:19 CET 2014.599">
<log realm="RemoteServer" at="Tue Dec 02 02:32:19 CET 2014.599">

Last edited by allend; 02-27-2015 at 09:20 AM.
 
1 members found this post helpful.
Old 03-02-2015, 11:16 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
The double quotes get in the way if they are not present in the log file itself.

Code:
sed -e '/^Tue Feb 24/,/^Wed Feb 25/!d' /var/log/logfile.log
There sed looks for the string literals when they occur at the start of the line. If you are looking for somewhere else on the line, then remove the carets.
 
1 members found this post helpful.
Old 03-10-2015, 06:59 AM   #4
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Original Poster
Rep: Reputation: 127Reputation: 127
Yes, of course, the single quotes shouldn't be there! How could I miss that!
Thanks a lot!
 
  


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
[SOLVED] Grep varying no. of lines between two patterns Tauro Linux - Newbie 21 04-14-2011 03:57 AM
Extract lines between two patterns containing certain string aliencenet Linux - Software 1 06-16-2010 05:38 PM
grepping all the lines between 2 patterns raghu123 Programming 2 11-04-2008 01:24 AM
bash script to use sed for filter mutiples patterns from apache access logs matyu Programming 5 02-06-2008 10:28 PM
Help, help! How to find lines in between searched patterns? wujee Linux - Software 1 01-06-2005 02:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

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