delete block of lines when pattern does not match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete block of lines when pattern does not match
# 1  
Old 05-06-2010
Question delete block of lines when pattern does not match

I have this input file that I need to remove lines which represents more than 30 days of processing.

Input file:
Code:
On 11/17/2009 at 12:30:00, Program started processing...argc=7
Total number of bytes in file being processed is 390
Message buffer of length=390 was allocated successfully
Number of bytes read from input file is 121756 = 390
Number of bytes put  = 390
Total Number messages  = 1
On 11/17/2009 at 12:30:00, Program has completed processing. RC = 0
****************************************************************************
On 05/05/2010 at 12:30:00, Program started processing...argc=7
Total number of bytes in file being processed is 390
Message buffer of length=390 was allocated successfully
Number of bytes read from input file is 121756 = 390
Number of bytes put  = 390
Total Number messages  = 1
On 05/05/2010 at 12:30:00, Program has completed processing. RC = 0
****************************************************************************

Output file should look like this:
Code:
On 05/05/2010 at 12:30:00, Program started processing...argc=7
 Total number of bytes in file being processed is 390
 Message buffer of length=390 was allocated successfully
 Number of bytes read from input file is 121756 = 390
 Number of bytes put  = 390
 Total Number messages  = 1
 On 05/05/2010 at 12:30:00, Program has completed processing. RC = 0
 ****************************************************************************

On a previous thread, I have used the below lines of code to check on the date on the line if it is greater than or equal to 30 and to print a number of lines when the date condition was met.

Code:
Days=30
Before=$(printf "%4d%02d%02d" $(datecalc -a $(date +'%Y %m %d') - ${Days}))
awk -F '[/ ]*' -v Before=${Before} '
{
   date = $4 $2 $3;
   if (date >= Before) {
      print;
      printSepLine = 7;
   }
} 
/^\*+/ && printSepLine {
   print;
   printSepLine = 0;
}
' log

However I'm getting this result:

Code:
Total number of bytes in file being processed is 390
Message buffer of length=390 was allocated successfully
Number of bytes read from input file is 121756 = 390
Number of bytes put  = 390
Total Number messages  = 1
**************************************************************************** 
On 05/05/2010 at 12:30:00, Program started processing...argc=7
Total number of bytes in file being processed is 390
 Message buffer of length=390 was allocated successfully
Number of bytes read from input file is 121756 = 390
Number of bytes put  = 390
Total Number messages  = 1
On 05/05/2010 at 12:30:00, Program has completed processing. RC = 0
****************************************************************************

The block of lines in between dates that were older than 30 days were retained from the output (blue lines above). How do I delete those block of lines?
# 2  
Old 05-06-2010
Try this:
Code:
Days=30
Before=$(printf "%4d%02d%02d" $(datecalc -a $(date +'%Y %m %d') - ${Days}))

awk -F"[/ ]" -v Before=${Before} '
/^On/{d=$4 $2 $3; if(d < Before){f=1}}
/^\*/ && f {f=0; next}
!f
' log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Delete lines based on pattern match

BASH in Solaris 10 I have a log file like below. Whenever the pattern ORA-39083 is encountered, I want to delete the line which has this pattern and 3 lines below it. $ cat someLogfile.txt ORA-39083: Object type OBJECT_GRANT failed to create with error: ORA-01917: user or role 'CMPA' does... (4 Replies)
Discussion started by: kraljic
4 Replies

4. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

5. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

6. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

7. Shell Programming and Scripting

delete a line that does not match the pattern

hi, i am parsing a file, in that searching for lines those contains "$threadNo.Received message:" , if that line contains the required fields write them into a separate file other wise ignore them. i am using the following code,but it is printing all the lines , i dont want to rpint , please help... (3 Replies)
Discussion started by: Satyak
3 Replies

8. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies

9. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies

10. UNIX for Dummies Questions & Answers

How to delete lines do NOT match a pattern

On Unix, it is easy to get those lines that match a pattern, by grep pattern file or those lines that do not, by grep -v pattern file but I am editing a file on Windows with Ultraedit. Ultraedit support regular expression based search and replace. I can delete all the lines that match a... (1 Reply)
Discussion started by: JumboGeng
1 Replies
Login or Register to Ask a Question