Visit Our UNIX and Linux User Community


Search for a specific String in a log file for a specific date range


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for a specific String in a log file for a specific date range
# 1  
Old 03-26-2014
Search for a specific String in a log file for a specific date range

Hi,

I have log file which rolls out every second which is as this.
Code:
[2014-03-24 18:45:35,704 ERROR [http-8080-15] HttpGenRequest - [ALERT]-<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/> </oxip> )]

I want a script file to print out all the occurrences of the string 'ERROR' for a particular date range example on a specific day for example 2014-03-24 or a date range or a month given as input.

And print out different types of errors as "Request found no matching data" into another file

Last edited by Franklin52; 03-27-2014 at 04:13 AM.. Reason: Please use code tags
# 2  
Old 03-27-2014
If sample provided will be same for all the records , then you can try as follows

Code:
awk -v d=2014-03-24 -F'"' 'substr($1,2,10)==d && substr($2,1,10)==d && $1~/ERROR/ {print substr($1,2,19)",ERROR,"$2","$12}'  sample.txt

output:-
Code:
2014-03-24 18:45:35,ERROR,2014-03-24 23:45:37,Request found no matching data


or for date ranges

Code:
awk -v d1=2014-03-24 -v d2=2014-03-25 -F'"' 'substr($1,2,10)==d1 && substr($2,1,10)==d2 && $1~/ERROR/ {print substr($1,2,19)",ERROR,"$2","$12}'  grep_error_in_range


Last edited by Franklin52; 03-27-2014 at 04:13 AM.. Reason: Please use code tags
# 3  
Old 03-27-2014
2014-03-27 14:24:11,380 INFO [pool-1-thread-539] HttpGenRequest - [OK]-GET: (TimeTaken: 184 milliseconds)-(Request: HttpGetGetEventDetails - 1f9b62c3-924b-49c2-b05a-41d1c424765b)-(TimeInOpenBet: 0.1298 seconds)-(Diff: 55 milliseconds)-(Reply Len: 42225)


log file will be exactly as this, the command which you gave was not working...

couldn't we use grep for searching a string and sorting according date..???
# 4  
Old 03-27-2014
Code:
#!/bin/bash
# Written on Windows without shell avilable
#
#	Variables
#
	search="ERROR"
	logfile=/var/log/logname
	str_output=""
	start_year=2014
	start_month=3
	start_day=20
	end_year=2014
	end_month=3
	end_day=30
#
#	Action
#
	while [ $start_year -lt $end_year ];do
		while [ $start_month -lt $end_month ];do
			while [ $start_day -lt $end_day ];do
				str_output+="$(grep $start_year-$start_month-$start_day $logfile|grep $search && printf '\n' )"
				((start_day++))
				[ $start_day -gt 31 ] && start_day=1 && ((start_month++))
			done
			((start_month++))
			[ $start_month -gt 12 ] && start_month=1 && ((start_year++))
		done
		((start_year++))
		[ $start_year -gt $(date +'%Y') ] && break
	done
	printf "${str_output}\n"|sort

Save this (and change vars if required) as a script and execute it.
To save the results, simply use redirection: ./script > result.txt

Hope this helps

Last edited by sea; 03-27-2014 at 09:33 PM..
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #809
Difficulty: Easy
The CSS3 RGB Hex Code for DarkOrange is #FF4500.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

2. UNIX for Dummies Questions & Answers

Need to search specific string in a log

Hi, I have a log file containing below data 12/3/14 12:43:59 AM WIT |Performance threshold: elapsed time = 3152... (5 Replies)
Discussion started by: csm231
5 Replies

3. Shell Programming and Scripting

Search for logs traced between specific date and time from log file

HI, I want to search for a logs which are trace between specific date and time from logs file. My logs are generated like this :- Tue Jun 18 05:00:02 EEST 2013 | file_check.sh| Message:script has files to process. Thu Jun 20 05:00:02 EEST 2013 | file_check.sh| Message:script has files to... (5 Replies)
Discussion started by: ketanraut
5 Replies

4. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

5. Shell Programming and Scripting

Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as (7 Replies)
Discussion started by: manigrover
7 Replies

6. Shell Programming and Scripting

Urgent request to consider:Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as # Drug_Target_X_Gene_Name:(Where X can be any number (1-1000) (1 Reply)
Discussion started by: manigrover
1 Replies

7. UNIX for Dummies Questions & Answers

How can I search and change an specific string in a file

Dear All, New to Linux/Unix OS, my Linux version is 2010 x86_64 x86_64 x86_64 GNU/Linux As titled, I wonder if you can help to provide a solution to find and change an specific string in a file The file include a lots of data in following configuration but might be various in... (3 Replies)
Discussion started by: axel
3 Replies

8. Shell Programming and Scripting

Search for a specific data in a file based on a date range

Hi, Currently I am working on a script to automate the process of converting the log file from binary into text format. To achieve this, partly I am depending on my application’s utility for this conversion and the rest I am relying on shell commands to search for directory, locate the file and... (5 Replies)
Discussion started by: svajhala
5 Replies

9. UNIX for Dummies Questions & Answers

Coping Files for a specific date range

Hi, we have file name appended by date in yymmdd format .. ex: abc090101.dat I need to copy all the files between abc090101 to abc090331.. could you plz help me.. Thanks. (1 Reply)
Discussion started by: kolariya4u
1 Replies

10. Shell Programming and Scripting

Get Data Between a specific Date Range from logs

I need to extract data from logs for a mentioned date range..Its quite urgent can anyone help me out with it..its to be written in unix..just thought its better to specify.. (4 Replies)
Discussion started by: sankasu
4 Replies

Featured Tech Videos