Delete lines based on pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines based on pattern match
# 1  
Old 10-16-2014
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.

Code:
$ cat someLogfile.txt
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'CMPA' does not exist
Failing sql is:
GRANT SELECT ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "CMPA"
Some Important text1
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT UPDATE ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text2
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT SELECT ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text3
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT INSERT ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text4
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT DELETE ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text5

Expected output after the deletes:
Code:
Some Important text1
Some Important text2
Some Important text3
Some Important text4
Some Important text5

How can I do this ?
# 2  
Old 10-16-2014
Try

Code:
nawk '/ORA-39083:/{s=FNR+4}FNR==s' file

On Solaris/SunOS system, use /usr/xpg4/bin/awk or /usr/xpg6/bin/awk , or nawk
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-16-2014
I think that should be sth like
Code:
awk '/ORA-39083:/{L=NR+4}NR>=L' file

to list more than just that line.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-16-2014
Thank You very much Akshay , Rudic.

Akshay, I forgot to mention that I want to view all the text other than those lines I wanted to skip/delete. I didn't include the lines in red below in my earlier example. I want to see these lines starting with AnotherImportant ... too. ie. All the remaining lines in the file. RudiC's solution caters to this requirement too.


Code:
$ cat someLogfile.txt
AnotherImportan text
Another important test2
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'CMPA' does not exist
Failing sql is:
GRANT SELECT ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "CMPA"
Some Important text1
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT UPDATE ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text2
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT SELECT ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text3
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT INSERT ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text4
ORA-39083: Object type OBJECT_GRANT failed to create with error:
ORA-01917: user or role 'TESTER' does not exist
Failing sql is:
GRANT DELETE ON "CMPA_USER"."CMPA_CLIENT_VISIT_TX_1909" TO "TESTER"
Some Important text5
$
$
$
$ ### Akshay's solution ####
$ nawk '/ORA-39083:/{s=FNR+4}FNR==s' someLogfile.txt
Some Important text1
Some Important text2
Some Important text3
Some Important text4
Some Important text5
$
$

$ ### Rudic's solution ### This shows all the remaining lines too
$ awk '/ORA-39083:/{L=NR+4}NR>=L' someLogfile.txt
AnotherImportan text
Another important test2
Some Important text1
Some Important text2
Some Important text3
Some Important text4
Some Important text5

# 5  
Old 10-16-2014
grep -v????
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

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

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

5. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

6. Shell Programming and Scripting

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: 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... (1 Reply)
Discussion started by: udelalv
1 Replies

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

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

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