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

Some Important text1
Some Important text2
Some Important text3
Some Important text4
Some Important text5

How can I do this ?

Try

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

1 Like

I think that should be sth like

awk '/ORA-39083:/{L=NR+4}NR>=L' file

to list more than just that line.

1 Like

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.

$ 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

grep -v????