Prakash Somasundaram
Prakash Somasundaram

Reputation: 83

Print lines between two patterns and ignore the lines if certain text found

I have a file with 500+ lines. This file consists output of 3 server details separated by "---------" symbol with 5 categories.

For ex: I have a file named Test.txt in my Linux server

Contents of the file as follows.

########  
Server 1  
########  
--------  
Category 1  
No issue  
--------  
Category 2  
XYZ File system is 90%  
--------  
Category 3  
ABC codes are deployed  
--------  
Category 4  
No issue  
--------  
Category 5  
AB123 user is added  
--------  
########  
Server 2  
########  
--------  
Category 1  
Backup is completed  
--------  
Category 2  
XYZ File system is 90%  
--------  
Category 3  
ABC codes are deployed  
--------  
Category 4  
No issue  
--------  
Category 5  
AB123 user is added  
DD444 user is deleted
--------  
########  
Server 3  
########  
--------  
Category 1  
No issue
--------  
Category 2  
XYZ File system is 90%  
ABC File system is 95%  
--------  
Category 3  
ABC codes are deployed  
DEF codes are deployed  
--------  
Category 4  
No issue  
--------  
Category 5  
AB123 user is added  
CD456 user is added  
EF789 user is added  
AD111 user is added  
BB222 user is deleted
--------  

Here i expect the output to be copied into new file by ignoring the No issue categories.

Expected output:

########  
Server 1  
########  
--------  
Category 2  
XYZ File system is 90%  
--------  
Category 3  
ABC codes are deployed  
--------  
Category 5  
AB123 user is added  
--------  
########  
Server 2  
########  
--------  
Category 1  
Backup is completed  
--------  
Category 2  
XYZ File system is 90%  
--------  
Category 3  
ABC codes are deployed  
--------  
Category 5  
AB123 user is added  
DD444 user is deleted
--------  
########  
Server 3  
########  
--------  
Category 2  
XYZ File system is 90%  
ABC File system is 95%  
--------  
Category 3  
ABC codes are deployed  
DEF codes are deployed  
--------  
Category 5  
AB123 user is added  
CD456 user is added  
EF789 user is added  
AD111 user is added  
BB222 user is deleted
--------   

Since i am new to Unix scripting, i can barely achieve the output.

i tried below sed and awk commands but i did not succeed.
sed -n '/^------/,/^------/p, Test.txt -> But the output is missing some categories between "------" symbol

awk -v RS="------" '$0~/No/{​​​​​​​print $0 RS}​​​​​​​' Test.txt -> Getting symbol error

Can someone please help me achieve the desired output.

Thanks in advance.

Upvotes: 2

Views: 382

Answers (3)

potong
potong

Reputation: 58371

This might work for you (GNU sed):

sed '/^----/{N;N;/No issue/d}' file

If the line beginning with dashes is matched, append the 2 following lines and if those lines contain the words No issues delete them.

An alternative, for variable lines between dashes:

sed '/^---/!{H;1h;d};x;/No issue/d;$G;1d' file

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203254

This might be what you're looking for using GNU awk for multi-char RS:

$ awk 'BEGIN{RS=ORS="--------\n"} !/No issue/' file
########
Server 1
########
--------
Category 2
XYZ File system is 90%
--------
Category 3
ABC codes are deployed
--------
Category 5
AB123 user is added
--------
########
Server 2
########
--------
Category 1
Backup is completed
--------

The above was run against this input file (. and till Server 3 lines removed from the end of the file and blanks removed from the end of lines compared to the file you posted in your question):

$ cat file
########
Server 1
########
--------
Category 1
No issue
--------
Category 2
XYZ File system is 90%
--------
Category 3
ABC codes are deployed
--------
Category 4
No issue
--------
Category 5
AB123 user is added
--------
########
Server 2
########
--------
Category 1
Backup is completed
--------

Upvotes: 3

prig9951
prig9951

Reputation: 11

You can just use grep -v

cat file.txt | grep -v "Category 1 No issue"

This will print the lines you want

Upvotes: 0

Related Questions