search for a date and print the contents below the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for a date and print the contents below the line
# 1  
Old 09-18-2012
search for a date and print the contents below the line

Hi,

We have a script which takes the backup of some files and writes the output into a log file for each run on a daily basis. Following is the extract from the log file.

HTML Code:
Date:20120917
**********************************************************
*               BACKUP ACTIVITY STARTED                  *
**********************************************************
List of files to be backed up:
sample.txt
sample1.txt

File Name = sample.txt
Backup File Name: sample_bkp.txt
Begin File Backup at Tue Sep 17 12:04:54 EDT 2012
Compressing sample.txt file on Tue Sep 17 12:06:49 EDT 2012...
File Name = sample1.txt
Backup File Name: sample1_bkp.txt
Begin File Backup at Tue Sep 17 12:08:53 EDT 2012
Compressing sample1.txt file on Tue Sep 17 12:09:34 EDT 2012...
Files backed up Successfully on Tue Sep 17 12:10:07 EDT 2012
***********************************************************
*              BACKUP ACTIVITY ENDED                      *
***********************************************************
End

Date:20120918
**********************************************************
*               BACKUP ACTIVITY STARTED                  *
**********************************************************
List of files to be backed up:
sample.txt
sample1.txt

File Name = sample.txt
Backup File Name: sample_bkp.txt
Begin File Backup at Tue Sep 18 12:04:54 EDT 2012
Compressing sample.txt file on Tue Sep 18 12:06:49 EDT 2012...
Repository Name = sample1.txt
Backup File Name: sample1_bkp.txt
Begin File Backup at Tue Sep 18 12:08:53 EDT 2012
Compressing sample1.txt file on Tue Sep 18 12:09:34 EDT 2012...
Files backed up Successfully on Tue Sep 18 12:10:07 EDT 2012
***********************************************************
*              BACKUP ACTIVITY ENDED                      *
***********************************************************
End
I want to pass the date as an input and it should extract the information based on the given date. Say for example, if I pass 20120918 as an input to the log file, it should return the rows corresponding to that date.

I tried awk but I am unable to generate the desired output. I tried the following. Let me know if there is a better way to do it.

HTML Code:
awk '/End$/{exit}/Date:`date +%Y%m%d`$/{f=1}f' backup_files.log
# 2  
Old 09-18-2012
Code:
awk '/Date:'`date +%Y%m%d`'/{f=1}f==1{s=s $0 "\n";}/End$/&&f==1{print s; exit}' backup_files.log

# 3  
Old 09-18-2012
Code:
 printf '/End$/{exit} /Date:%s$/{f=1}f\n' `date +%Y%m%d` > ./t.awk
 awk -f ./t.awk backup_files.log

The problem was that it did not expand the ` ` inside ' '. This is one way to do it that makes it easy to see why it works.
# 4  
Old 09-18-2012
Thanks. It works like a charm.

What if I want to avoid displaying the date at the beginning and the last line 'End'?
# 5  
Old 09-18-2012
Code:
awk '/End$/&&f==1{print s; exit}f==1{s=s $0 "\n";}/Date:'`date +%Y%m%d`'/{f=1}' backup_files.log

# 6  
Old 09-18-2012
It worked. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for Multiple strings in a given date range and print the Group if they exists

Hi, I am Searching for Multiple strings in a given date range and print the Group if they exists. the below is the format: ------------------------------------------------------------------------------------------------------------------------- ID: FIRST ID MESSAGE: Event Message... (5 Replies)
Discussion started by: linuxuser999
5 Replies

2. UNIX for Beginners Questions & Answers

Search for word in huge logfile and need to continue to print few lines from that line til find date

Guys i need an idea for one logic..in shell scripting am struggling with a logic...So the thing is... i need to search for a word in a huge log file and i need to continue to print few more lines from that line and the consecutive line has to end when it finds the line with date..because i know... (1 Reply)
Discussion started by: Prathi
1 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

5. UNIX for Dummies Questions & Answers

How to search two strings in a file and print the contents in between to a file

I have a file called po.txt. Here is the content of the file: <!DOCTYPE PurchaseOrderMessage (View Source for full doctype...)> - <PurchaseOrder> - <Header> <MessageId>cdb3062b-685b-4cd5-9633-013186750e10</MessageId> <Timestamp>2011-08-01T13:47:23.536-04:00</Timestamp> </Header> -... (4 Replies)
Discussion started by: webbi
4 Replies

6. Shell Programming and Scripting

Search Duplicates, Print Line #

Masters, I have a text file in the following format. vrsonlviee RVEBAALSKE lyolzteglx UUOSIWMDLR pcybtapfee DKGFJBHBJO ozhrucfeau YQXATYMGJD cjwvjolrcv YDHALRYQTG mdukphspbc CQZRIOWEUB nbiqomzsgw DYSUBQSSPZ xovgvkneav HJFQQYBLAF boyyzdmzka BVTVUDHSCR vrsonlviee TGTKUCUYMA... (2 Replies)
Discussion started by: genehunter
2 Replies

7. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

8. Shell Programming and Scripting

Print the line within the search pattern

Hi Guys, I had file as typedef struct { char TrailerType1; char TrailerTxt1; }Trailer; typedef struct { char PfigMoneyType; char PfigMoneyvalue; }PfigMoney; i need to print the lines within the search pattern. if i give the search pattern as... (3 Replies)
Discussion started by: manosubsulo
3 Replies

9. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

10. Shell Programming and Scripting

Search for text and print the next line

Hi, I want to write a small script to search for a text in file and when its found I want to print the next line. I try to write that script but I could not manage it, I just write the following script the find the exact line but I want the next line. $ sed -n -e '/Form not/p' test.txt... (2 Replies)
Discussion started by: alijassim
2 Replies
Login or Register to Ask a Question