getting string after date in log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting string after date in log
# 1  
Old 02-28-2012
getting string after date in log

I need a script to search for *.LOG in a directory, output the line after the current date and yesterday's date and also output the next 5 lines after the current date and yesterday's date

I have tried following commands, still no result
Code:
Currentdate=`date -u +"%m/%d/%y"`
awk '/$Currentdate/{getline; print}' *.LOG

The log has data for the whole month for which I need only the strings attached to the current date and yesterdays date

Following is the log for the current day
Code:
02/28/12 06:33:34 - Process(3682) User(mq) Program(amioqc)
AMQ9209: Connection to host  closed.

EXPLANATION:
An error occurred receiving data  over TCP/IP.
The connection to the remote host has unexpectedly terminated.
ACTION:
Tell the systems administrator.
----- amq 3094 -------------------------------------------------------
02/28/12 06:39:05 - Process(110.1) User(mq) Program(amioqc)
AMQ: Connection to host 'loopback ' closed.

EXPLANATION:
An error occurred receiving data from 'loopback ' over TCP/IP.  The
connection to the remote host has unexpectedly terminated.
ACTION:
Tell the systems administrator.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-28-2012 at 07:12 AM.. Reason: Please use code tags for code and indent your code, thank you
# 2  
Old 02-28-2012
Quote:
Originally Posted by PhAnT0M
Code:
Currentdate=`date -u +"%m/%d/%y"`
awk '/$Currentdate/{getline; print}' *.LOG

Code:
$ today=`date -u +"%m/%d/%y"`
$ yest=`date -u -d"yesterday" +"%m/%d/%y"`
$ awk '/'$today'/ || /'$yest'/ {print; getline; print}' inputfile

# 3  
Old 02-28-2012
Still not working

Quote:
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]

The error context is
/02/28/12/ >>> || <<<
I am currently using AIX
# 4  
Old 02-28-2012
also
Code:
TODAY=$(perl -e '@now=localtime; printf "%02d/%02d/%02d\n", $now[4]+1,$now[3],($now[5]+1900)%100')

Code:
YESTERDAY=$(perl -e '@now=localtime(time - 86400); printf "%02d/%02d/%02d\n", $now[4]+1,$now[3],($now[5]+1900)%100')

... but I would suggest you convert everything in a YYYYMMDD format instead of the non-friendly MM/DD/YY format

Last edited by ctsgnb; 02-28-2012 at 10:22 AM..
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 02-29-2012
Code:
TODAY=$(perl -e '@now=localtime; printf "%02d/%02d/%02d\n", $now[4]+1,$now[3],($now[5]+1900)%100')

YESTERDAY=$(perl -e '@now=localtime(time - 86400); printf "%02d/%02d/%02d\n", $now[4]+1,$now[3],($now[5]+1900)%100')

These codes work perfectly in displaying date in MM/DD/YY format
But after I get this date sting I need to display the lines attached to the this date from the log

I tried using

Code:
awk '/$TODAY/' *.LOG
awk '/'$YESTERDAY/' *.LOG

But this displays nothing

Thanks
# 6  
Old 02-29-2012
Code:
 
bash-3.2$ cat test.txt
01/02/12 : MY LOG
01/02/12 : MY LOG 
01/02/12 : MY LOG 
01/02/12 : MY LOG 
dafdasfasdf
asd
fasd
fas
f
asdf
01/02/12 : MY LOG 
 
$ echo $date
01/02/12

$ nawk -v d="$date" '$0~d' test.txt
01/02/12 : MY LOG
01/02/12 : MY LOG 
01/02/12 : MY LOG 
01/02/12 : MY LOG 
01/02/12 : MY LOG

# 7  
Old 02-29-2012
Try this by escape the forward slash and use double quote in awk:
Code:
TODAY=$(perl -e '@now=localtime; printf "%02d\\/%02d\\/%02d", $now[4]+1,$now[3],($now[5]+1900)%100')

YESTERDAY=$(perl -e '@now=localtime(time - 86400); printf "%02d\\/%02d\\/%02d", $now[4]+1,$now[3],($now[5]+1900)%100')

awk "/$TODAY/ || /$YESTERDAY/" file

This User Gave Thanks to chihung For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting String Date into UNIX Date

Hi, I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below: MyTest.sh -s 2018-05-09 suppdt=$1 # string date passed via arguement as 2018-04-09 curryr=`date '+%Y'` nextyr=`expr... (2 Replies)
Discussion started by: Saanvi1
2 Replies

2. UNIX for Dummies Questions & Answers

Copy log based on from-date and to-date

Hi all, i go a customer support requirement where i need to scan several files based on from/to date like 1-oct to 2-oct please help... (3 Replies)
Discussion started by: AbhiJ
3 Replies

3. UNIX for Dummies Questions & Answers

I need a Script to read Log string and check date at the same time

I need to check 1 log file, which is logging: 2014-08-18T09:10:39+02:00 user: XXXXX START FEATURE 2014-08-18T09:10:39+02:00 user: XXXXX FINISH FEATURE I first need to check that the START FEATURE starts and finish on the same time/date for the same user, which is different each time START... (2 Replies)
Discussion started by: TheBest43
2 Replies

4. UNIX for Dummies Questions & Answers

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. HttpGenRequest - -<!--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"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

5. Shell Programming and Scripting

Compare Date with String in date format

Hi, I am new to this forum, searched, but for some reason did not find much help, so a new thread. I am trying to compare date with a string which is in date format , but for some reason, system does not give me the right result. Date is coming in a file and i am comparing the same with... (2 Replies)
Discussion started by: mkstool
2 Replies

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

7. Homework & Coursework Questions

Get Julian date from date string

Hi, im new for UNIX. i have a problem in date function. please help me to find a solution. batchdate="29/10/2010" nextdate="01/11/2010" i want compare this two date. if my batch date greater than nextdate should prompt error message. how can i do that? as i know its better and safer if i... (2 Replies)
Discussion started by: ananth4mu
2 Replies

8. Shell Programming and Scripting

Need to get next valid date from date string

Hi, I am passing date string of format 'YYYYMMDD' to a ksh script. Will I be able to get next valid date from the passed in string. Example I pass '20100228' to the shell script, Is there a reverse date command to get '20100301' .i.e to convert '20100228' to date and get next date.... (5 Replies)
Discussion started by: bittoo
5 Replies

9. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies
Login or Register to Ask a Question