How to extract log data based on date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract log data based on date
# 1  
Old 08-12-2010
Question How to extract log data based on date

Hi Gurus,

I've been having some problem in extracting the log data based on the current date and month.

As shown in the sample data below, how to extract the log info for Aug 11?

Sample data:
Code:
root     pts/ta       userpc Wed Aug 11 09:46 - 20:21  (10:35)
root     pts/ta       userpc Wed Aug 11 09:44 - 20:10  (10:34)
root     pts/ta       userpc Wed Aug 11 09:40 - 20:05  (10:32)
root     pts/tb       userpc Tue Aug   9 11:23 - 11:23  (00:00)
root     pts/tb       userpc Tue Aug   9 11:22 - 11:20  (00:00)
root     pts/ta       userpc Tue Aug   2 10:46 - 19:19  (08:33)
root     pts/ta       userpc Tue Aug   2 10:40 - 19:15  (08:33)
root     pts/ta       userpc Tue Jul    1 10:41 - 10:45  (00:04)
root     pts/ta       userpc Tue Jul    1 10:40 - 10:43  (00:04)

Sample output:
Code:
root     pts/ta       userpc Wed Aug 11 09:46 - 20:21  (10:35)
root     pts/ta       userpc Wed Aug 11 09:44 - 20:10  (10:34)
root     pts/ta       userpc Wed Aug 11 09:40 - 20:05  (10:32)

Would aprpeciate for any of your help and advice.

Thank you.
# 2  
Old 08-12-2010
Code:
grep "Aug 11"  inputfile

# 3  
Old 08-12-2010
If wanted to extract the data according to current date like "Aug 11" and test1 is the data file, the below command works.

Code:
bash-3.00$ cat test1
root     pts/ta       userpc Wed Aug 11 09:46 - 20:21  (10:35)
root     pts/ta       userpc Wed Aug 11 09:44 - 20:10  (10:34)
root     pts/ta       userpc Wed Aug 11 09:40 - 20:05  (10:32)
root     pts/tb       userpc Tue Aug   9 11:23 - 11:23  (00:00)
root     pts/tb       userpc Tue Aug   9 11:22 - 11:20  (00:00)
root     pts/ta       userpc Tue Aug   2 10:46 - 19:19  (08:33)
root     pts/ta       userpc Tue Aug   2 10:40 - 19:15  (08:33)
root     pts/ta       userpc Tue Jul    1 10:41 - 10:45  (00:04)
root     pts/ta       userpc Tue Jul    1 10:40 - 10:43  (00:04)


Code:
 
cat test1 | grep "`date +'%h %d'`"

This User Gave Thanks to gaithrit For This Post:
# 4  
Old 08-12-2010
Hi gaithrit,

Thanks for your response.

I've tried your method and it seems to be working fine.

However, when there is a space in between the date and month, it's not working.

Sorry, I forgot to mention that I'm using HPUX B.11.23 ia64, the sample log data was extracted from "last -R".

Please kindly advice.

Thanks.

Last edited by superHonda123; 08-12-2010 at 08:38 AM.. Reason: typo error
# 5  
Old 08-12-2010
I am not clear, can you give the command you are running? Thanks.
# 6  
Old 08-12-2010
Something like this,

Code:
 awk -v v1=`date +'%h'` -v v2=`date +'%d' | sed 's/^0//g'`  '{if($5==v1 && $6==v2) { print }}' infile

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 08-12-2010
Quote:
Originally Posted by pravin27
Something like this,

Code:
 awk -v v1=`date +'%h'` -v v2=`date +'%d' | sed 's/^0//g'`  '{if($5==v1 && $6==v2) { print }}' infile

Based on your code:
Code:
 last -R|awk -v v1=`date +'%h'` -v v2=`date +'%d'`  '$5==v1 && $6==v2'

These 2 Users Gave Thanks to Klashxx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract date from files based on file pattern

I want to extract dates from the files and i have different types of files with pattern. I have list file with the patterns and want to date extract based on it in a sh script Files in the directory : file1_20160101.txt file2_20160101_abc.txt filexyz20160101.txt list file with... (2 Replies)
Discussion started by: lijjumathew
2 Replies

2. UNIX for Dummies Questions & Answers

Extract date ranged data from log file

Hi, I am trying to extract lines of data within a log file on a Redhat 5 Linux system. eg I need all the lines with a particular username over the last 3 minutes. the log file may read like this, and I want a way to search all the lines extracting all the relevant lines over the last 3... (2 Replies)
Discussion started by: mantis100
2 Replies

3. Shell Programming and Scripting

Extract data based on 2nd colume having double quotes

i want extract where the 2nd column having "3" or "7". Based on the forums tried like this but it is not working awk -F"," '$2=3;$2=7 {print}' filename Source "1","2","3","4" "1","3","3","4" "1","7","3","4" "1","8","3","4" "1","2","3","4" "1","2","3","4" Output : ... (5 Replies)
Discussion started by: onesuri
5 Replies

4. Shell Programming and Scripting

extract data in a csv file based on a certain field.

I have a csv file that I need to extract some data from depending on another field after reading info from another text file. The text file would say have 592560 in it. The csv file may have some data like so Field 1 Field2 Field3 Field4 Field5 Field6 20009756 1 ... (9 Replies)
Discussion started by: GroveTuckey
9 Replies

5. Shell Programming and Scripting

Extract data based on position

The file has record length 200. And i have 100 search strings which are ten digits of character from 1 to 10 characters all of them are unique, they need to searched in a file. Please help me to pull the records based on position (say from 1-10). test data 1FAHP2DW0BG115206RASHEED ... (6 Replies)
Discussion started by: zooby
6 Replies

6. Shell Programming and Scripting

Extract data based on specific search criteria

I have a huge file (about 2 millions records) contains data separated by “,” (comma). As part of the requirement, I can't change the format. The objective is to remove some of the records with the following condition. If the 23rd field on each line start with 302 , I need to remove that from the... (4 Replies)
Discussion started by: jaygamini
4 Replies

7. Shell Programming and Scripting

How to extract log data based on current date and month ?

Hi Gurus, I'm using HP-UX B.11.23 operating system. I've been trying to extract this log info based on the current date and month, but was having some issues as the date column which on the 4th column has a comma and the 5th column has a dot tied to it. Here is the output from my shut... (5 Replies)
Discussion started by: superHonda123
5 Replies

8. UNIX for Dummies Questions & Answers

Extract Data and arrange it based on timelines

Hi Im trying to extract a specific pattern of data from a log file and store it in a other file, Im executing the below command in various files and storing it in a single file. I data that Im storing needs to be arranged based on a date and timestamp. Please assist, how to achieve this. Thanks... (1 Reply)
Discussion started by: vr3w3c9
1 Replies

9. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

10. Shell Programming and Scripting

Extract data from log file from or after the specific date

Hi , I am having a script which will start a process and appends the process related logs to a log file. The log file writes logs with every line starting with date in the format of: date +"%Y %b %d %H:%M:%S". So, in the script, before I start the process, I am storing the date as DATE=`date +"%Y... (5 Replies)
Discussion started by: chiru_h
5 Replies
Login or Register to Ask a Question