Extract date from log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract date from log file
# 1  
Old 03-17-2009
Extract date from log file

Hello All,

I just need to extract the date portion [ * ] from a apache log file
I am able to do it using the chain of command -

Logfile contents - First record -
==========================
197.130.211.240 - - [11/Mar/2009:01:00:00 -0700] "GET /jp/index.shtml HTTP/1.1" 200 24255 "http://www.google.co.jp/search?q=cco&lr=lang_ja&ie=utf-8&oe=
utf-8&aq=t&rls=org.mozilla:jaSmiliefficial&client=firefox-a" "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7" "C
TC=202.226.241.234.1211271614048643; SMIDENTITY=8dvPlQzpqd5+avTXCpx7AWCRc3IbGqY7Mm6ECeAu/lwojS/RC4qmMwC+zQCHmNJekImecfJ9e8qJQVgI8Nb7hBe4rNRzPVXPRqUxTh69a
Bp4UA5oRd5SceTwPkA+RLB/ZvSST0Bvxk1dVnmo8R50VJ+IUDqB+N2854r7x/pihcXYObu7HLRFLdAhwVNYaamwWZ8qsTwgUEfUTZLlEGTlqsJuqo+DiO1nZnUmJ1PIgfIpEHeMpzxTiSpFI7FtTWYivH
GE9WeHfJBPV6EcuWONc9pvTfB4hprbm+sW5HxlzwP6wa2RdhpdO5rrLVX8DEP/I4fKYKTu0LYm0MIrVY1/xpNsHa3EkCgQL9F6r1Ns40qry9RRfCywEGhnDTk4jCsPCGF4s7bnwypVmpraunUq/jQLaBm
zLjB/Jr/uPcNR4zpodSuGbbc/8X3INPMx/LeXxAwau79aeBC40u7yGVZXYcyhdLJrsKOqwOcMcXJbOw9aW0SmQJrzCEtnEvc0nysGttJ5PwYdDO0Mrt5CIFs/IfTSKhDUB/Oa123IVoGO15TPHjE4XoFI
Jxk6FmNojzR1I2iSLgGKF4wwR44i9sPqXWwFfTUOUuRknOu614p368dorLg6gjhOdPvyX0apnP4RZA1iGR8f8FWuyDFQfvO3Mg2f QBoTQYIYSXBu372t8HNbvspnFElLuGqtHOyuC13a; USER_PROFIL
E=eNpLTE5OLS72SS1LzbE1tgYAKM8FFg==; __utma=152635051.1371569464997210600.1234262319.1234262319.1234865834.2; __utmz=152635051.1234262319.1.1.utmcsr=(dire
ct)|utmccn=(direct)|utmcmd=(none); s_pers=%20s_nr%3D1236048421913%7C1238640421913%3B%20gpv_p20%3Dtools.cisco.com/search/jsp/search-results.get%7C12367597
50363%3B; s_sess=%20s_ria%3Dflash%252010%257C%3B%20s_cc%3Dtrue%3B%20s_sq%3D%3B" "202.227.241.239"

============================
How i am trying

dt=`head -1 log-date-checker.txt | awk -F "]" '{print $1}' | awk -F "[" '{print $2}' | cut -f1 -d" "`

echo $dt
========
Output

11/Mar/2009:01:00:00


What i need ?-

===============
Any alternate and optimal solution to pick the date portion

Any help appreciate

Thanks

Last edited by jambesh; 03-17-2009 at 02:43 PM..
# 2  
Old 03-17-2009
Code:
echo '198.133.219.248 - - [11/Mar/2009:01:00:00 -0700] "GET /jp/index.shtml HTTP/1.1" 200 24255' | sed 's#[^[]*[[]\([^]][^]]*\).*#\1#'

# 3  
Old 03-17-2009
Thanks Vgersh
It works !

Last edited by jambesh; 03-17-2009 at 02:49 PM..
# 4  
Old 03-17-2009
'[^[]*[' - any character BUT '[' repeated 0 or more times followed by '['.
'[^]][^]]*' - any character BUT ']' repeated 1 or more times
# 5  
Old 03-17-2009
thank you for your help
I appreciate your valuable time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

AIX UNIX - script on how to extract from log file with previous date

Hello, I am new to this forum so any assistance would help. I am currently trying to develop a script that extract all data from a log file with has the previous day's date. $ <root@aixtest3> /var/log > more sudo.log May 13 10:52:10 aixtest3 local2:notice sudo: tbrath : TTY=unknown ; ... (14 Replies)
Discussion started by: Kslew82
14 Replies

2. UNIX for Dummies Questions & Answers

How to extract date from a file?

Hello Gurus, I have one file from which I want to extract only dates. The below is the file format : ========================= Process ID 16842770 Log Read Checkpoint Oracle Integrated Redo Logs 2014-07-04 17:06:11 <=== SCN 1779.852353022 (7641599172606) Process ID ... (6 Replies)
Discussion started by: pokhraj_d
6 Replies

3. 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

4. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

5. 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

6. Shell Programming and Scripting

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: root pts/ta userpc Wed Aug 11 09:46 - 20:21 (10:35) root pts/ta userpc... (13 Replies)
Discussion started by: superHonda123
13 Replies

7. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

8. 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

9. Shell Programming and Scripting

How to extract date with time from file

Hi, I have a file where there is a date and time field, the format for it is yyyy-mm-dd hours:mins:sec the position of date field may vary anywhere in the line and it might be different and it is specified along with the variable AppTimeStamp how do i extract date and time both from the... (5 Replies)
Discussion started by: prash_b
5 Replies

10. Shell Programming and Scripting

Extract Date from file

I am new to unix scripting. we are using bash. My task is I have dir which contains 30 files. The first column in each file is Date field. For all the files I need to extract the date part Ex(2007-09-05) from the file and add that at the end of that file. for example: The file names are... (2 Replies)
Discussion started by: magi
2 Replies
Login or Register to Ask a Question