Find and print specific date with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and print specific date with awk
# 1  
Old 06-21-2012
Find and print specific date with awk

hi all
I would like to help me find the problem with this script to find and print to the screen a specific date of a log file that I have on my server, the date it is received as the first argument in the script $ 1
Here I show you a few lines that made ​​the idea of ​​my log file:

Code:
**** run_sql: state_cagendas.sql - 18/Jun/2012 22:59 ****
PL/SQL procedure successfully completed.
**** Completed script /bin/run_sql  - 18/Jun/2012 22:59 ****


**** run_sql: /bin/sql/unid.sql - 19/Jun/2012 22:58 ****
5 rows updated.
****  Completed script /bin/run_sql  - 19/Jun/2012 22:58 ****


**** run_sql: state_cagendas.sql- 19/Jun/2012 22:59 ****
PL/SQL procedure successfully completed.
**** Completed script /bin/run_sql  - 19/Jun/2012 22:59 ****


**** run_sql: c /tarif.sql - 20/Jun/2012 05:15 ****
PL/SQL procedure successfully completed.
**** Termino script /hfa/bin/run_sql  - 20/Jun/2012 05:15 ****


**** run_suspende: opensic- PL_SUSPENDE - 20/Jun/2012 05:30 ***
PL/SQL procedure successfully completed.
**** Termino  /hfa/bin/run_suspende  - 20/Jun/2012 05:30 ***

that is my script called "search_log"
Code:
echo "Enter date to Search (e.g. 19Jun2012): "
read search
day=`echo $search | cut -c1-2`
month=`echo $search | cut -c3-5`
year=`echo $search | cut -c6-9`

echo "BEGIN { count=0 }; /"$day"\/"$month"\/"$year"/ {print;active=\"y\";count=count+1;next}; { if (active == \"y\" && count == 2) print;active=\"n\";next}; { if (active == \"n\") exit}"  > /tmp/prog$$

awk -f /tmp/prog$$  $1
rm /tmp/prog$$

when I run

search_log / logfile

I get the following error in the command line:

awk: syntax error near line 1
awk: bailing out near line 1

please if anyone can give a help to get out of this problem would appreciate it.

thank you very much
# 2  
Old 06-21-2012
You need to see this.
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 06-21-2012
Code:
echo "Enter date to Search (e.g. 19-Jun-2012): "
IFS=" /-" read DD MON YYYY

nawk -v D="$DD/$MON/$YYYY" '/\*\*\*\*/ && ($0 ~ D) { P=1 } P ; /\*\*\*\*/ && !($0 ~ D) { P=0 }' inputfile

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 06-21-2012
thank you very much for the speed of responses both solutions were effective.

I changed awk to nawk and it worked.

I tried the second solution of Corona and I also was very useful, the only detail on the last line of the log shows the date of the day "20/Jun/2012" when I look for the log of the day "19/Jun2012" but very good solution anyway. Smilie
# 5  
Old 06-21-2012
I can fix that! Smilie

Code:
nawk -v D=" $DD/$MON/$YYYY " '/\*\*\*\*/ && ($0 ~ D) { P=1 }; /\*\*\*\*/ && !($0 ~ D) { P=0 }; P' inputfile

Also note the spaces around the date. It will match 9/xxx/yyyy without matching 19/xxx/yyyy now.
This User Gave Thanks to Corona688 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

To find files having filename containing specific date format

Hi, I have a requirement to create a shell script(tcsh) that finds all the files in a directory having the file name containing date format "YYYYMMDDHHMM" and extract the date time part ""YYYYMMDDHHMM" for further processing. Could you please have any idea on this. trades_201604040000.out... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

2. Shell Programming and Scripting

Print a specific length using awk

Hello, I have the following input data: A C AA GG A G CG AG the code I tried: awk -F" " '{ if ((length($1==1) && (length($2==1))) input_file is not working, I need the output to be (such that only the columns containing one letter are printed) A C A G Any help would be appreciated! (5 Replies)
Discussion started by: Rabu
5 Replies

3. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

4. UNIX for Dummies Questions & Answers

Find Commands Run on Specific Date

Hello All, I was wondering if anyone knew of a way to find out what commands were run on a specific date. I'm looking to see if I can find certain commands that were run on 3/4, today is 3/10...? Any thoughts or ideas would be much appreciated! Thanks in Advance, Matt ----------... (3 Replies)
Discussion started by: mrm5102
3 Replies

5. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

6. Shell Programming and Scripting

Find files for a specific date

Hi, I am looking to find files of a specific date. I am on Sun Solaris so newermt doesnot work.. I thought of using mtime but not getting how to use it. Please help me with this.. Regards Abhinav (3 Replies)
Discussion started by: abhi1988sri
3 Replies

7. Shell Programming and Scripting

Find files with specific date

Dear all, kindly i have some files with different dates i need to grep word from these files but i need to search in files with date 2012-12-02 not all files in this directory do u have any command (4 Replies)
Discussion started by: maxim42
4 Replies

8. Shell Programming and Scripting

awk statement to find events from a specific date

I have a script which archives log file events which are 90-days old. Script works fine but I wanted some input on one aspect of this script. My nawk statement, bolded below, that removes events 90-days prior from today, I need it to find anything 90-days or older. The log file date pattern looks... (0 Replies)
Discussion started by: numele
0 Replies

9. AIX

find files with specific date and time

Hi, I wanna to find files with specific date and time. I know this command: ls -ltr /system1/*.505 | grep 'Jan 18 09:00'. I want the 'Jan 18 09:00' not hard coded, I want it coming from the system date and time. And I want the time varies from 09:00-09:05. Is this possible without heavy... (3 Replies)
Discussion started by: itik
3 Replies

10. Shell Programming and Scripting

How to print specific lines with awk

Hi! How can I print out a specific range of rows, like "cat file | awk NR==5,NR==9", but in the END-statement? I have a small awk-script that finds specific rows in a file and saves the line number in an array, like this: awk ' BEGIN { count=0} /ZZZZ/ { list=NR ... (10 Replies)
Discussion started by: Bugenhagen
10 Replies
Login or Register to Ask a Question