Exclude multiple lines using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exclude multiple lines using grep
# 1  
Old 09-28-2016
Exclude multiple lines using grep

Hi,
I'm working on a shell script that reports service status on a database server.
There are some services that are in disabled status that the script should ignore and only check the services that are in Enabled status.

I output the service configuration to a file and use that information to loop through the services. Unfortunately im not able to exclude the service that is in Disabled status.

Here's the content of the file -

Code:
Service name: Bot_Serv1
Service is disabled
Preferred instances: botqa2,botqa3

Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3

Each service configuration is separated by a blank line. As you can see the first service is in Disabled status and the 2nd service is Enabled.

How do i get the content of the file, excluding the configuration details for the Disabled service. Im looking to grep the file and output should be the following -

Code:
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3


Last edited by Don Cragun; 09-29-2016 at 12:54 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 09-28-2016
grep doesn't do that, you need a language which can remember things, like awk.

RS="" tells awk to process text in blocks separated by two newlines.

!/Service is disabled/ prints every block which doesn't contain "Service is disabled".

Code:
$ awk -v RS="" -v ORS="\n\n" '!/Service is disabled/' data

Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-28-2016
Thank you very much, that works perfectly. Interesting to know how powerful is awk. shall experiment with it Smilie

Thanks again.
# 4  
Old 09-29-2016
Code:
awk '/Service is enabled/{print s;print;getline;print $0}{s=$0}'  input.txt

# 5  
Old 09-29-2016
Hi.

A version of grep, context grep, cgrep approach:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate extract paragraph, cgrep.
# Source for cgrep:
# https://sourceforge.net/projects/cgrep/ (verified 2016.09.28)

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
cgrep -D -1 +w='' "enabled" $FILE |
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
bash GNU bash 4.3.30

-----
 Input data file data1:
Service name: Bot_Serv1
Service is disabled
Preferred instances: botqa2,botqa3

Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3


-----
 Expected output:
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3


-----
 Results:
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3


-----
 Verify results if possible:

-----
 Comparison of 4 created lines with 4 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output.txt have same content.

The heart of this solution is:
Code:
cgrep -D -1 +w='' "enabled"

search for enabled, copy one line before, and all lines after until an empty line occurs.

Best wishes ... cheers, drl
# 6  
Old 09-29-2016
Gnu grep has -C option.

Code:
bash-3.2$ cat t.txt
Service name: Bot_Serv1
Service is disabled
Preferred instances: botqa2,botqa3

Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3


bash-3.2$ grep -C1 "Service is enabled" t.txt
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude lines in a file with matches with multiple Strings using egrep

Hi I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings. Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like CAT MAT DAT The command should fetch me... (4 Replies)
Discussion started by: Sathwik
4 Replies

2. Shell Programming and Scripting

grep from multiple lines in several gz files

Hello all, I have been struggling to get grep work to my requirements. Basically I have to filter out patterns spread across multiple lines over hundreds of .gz files in a folder. And the output needs to be piped to a file. Here is the example: folder name: logs files in this folder:... (4 Replies)
Discussion started by: mandhan
4 Replies

3. UNIX for Dummies Questions & Answers

Grep multiple lines

I want to grep multiple lines from a text file. I want to grep all lines containing X,Y and NA in a single command. How do I go about doing that? This is what my text files look like: rs1983866 0.0983 10 100016313 rs1983865 0.5994 X 100016339 rs1983864 0.3272 11 100017453 rs7077266... (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Advanced & Expert Users

grep across multiple lines

How do you grep 'select * from table_name' string from a script if the select * and from table_name are on 2 different lines ? like select * from table_name Any help would be greatly appreciated !!! Thanks RDR (4 Replies)
Discussion started by: RDR
4 Replies

5. UNIX for Dummies Questions & Answers

grep in multiple lines

hi i have kind of below text in a file. I want to get a complete paragraph starting with START and ending with before another START) which has a particular string say XYZ or ABC START XYZ hshjghkjh 45 ljkfd fldjlj d jldf START 3493u ABC 454 4545454 4545454 45454 4545454 START ...... (3 Replies)
Discussion started by: reldb
3 Replies

6. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

7. Shell Programming and Scripting

grep multiple lines

Hi. I have this format on a textfile: VG Name /dev/vg00 PV Name /dev/dsk/c16t0d0 PV Name /dev/dsk/c18t0d0 PV Name /dev/dsk/c16t4d0 VG Name /dev/vg01 PV Name ... (6 Replies)
Discussion started by: jOOc
6 Replies

8. Shell Programming and Scripting

grep - to exclude lines beginning with pattern

11132 13069 11137 11142 13070 Can I use grep command to exclude all lines beginning with 13? I dont want to use grep -v 13 as potentially there will be a number with something like 11013 that I would exclude in error.. (2 Replies)
Discussion started by: frustrated1
2 Replies

9. Shell Programming and Scripting

grep multiple lines

Hey guys: I've been meaning to post this question for awhile...it is regarding grep. Let's say for example that the following entry is in logxx: Wed Feb 2 07:44:11 <vsm> 91030 Line 5 Severity 1 Vps 6 Call Answered - DN:8753101 CLID:5164665761 PI:83 If I do a grep 91030... (27 Replies)
Discussion started by: cdunavent
27 Replies

10. Shell Programming and Scripting

Grep on multiple lines

I 'm trying to grep 2 fieldds on 2 differnt lines. Like this: psit > file egrep -e '(NS|ES)' $file. Not working. If this succeeds then run next cmd else exit. Pls Help Gundu (13 Replies)
Discussion started by: gundu
13 Replies
Login or Register to Ask a Question

Featured Tech Videos