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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exclude lines in a file with matches with multiple Strings using egrep
# 1  
Old 09-21-2015
Lightbulb 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 the line which do have any of these Strings CAT MAT DAT

How can I write the command using egrep without "-v" option.
Thanks in advance.

Last edited by Sathwik; 09-21-2015 at 04:29 AM..
# 2  
Old 09-21-2015
How can you write the command using egrep without the -v option? With great difficulty. All you have to do is create an ERE that matches any line that does not match any line that contains something like CAT, MAT, or DAT. How you define "something like" may make a huge difference between this being difficult or impossible.

If this isn't a homework assignment (which would need to be filed in the Homework and Coursework Questions forum instead of the Shell Programming and Scripting forum), why would you need to avoid the egrep -v option?
# 3  
Old 09-21-2015
Thanks for response. Actually I have multiple conditions in my egrep to look out for pattern like

line starting with a certain string, spaces at some point, numbers at somepoint, ending with some characters. excluding few strings is one such condition
So I cannot use -v.
This requirement came in for my work Not for assignment.

For matching condition, I can use this
egrep (CAT|MAT|DAT) test.txt

but to negate this, I tried all options like using (^) but couldnt get it.

---------- Post updated at 01:54 PM ---------- Previous update was at 01:54 PM ----------

Thanks for response. Actually I have multiple conditions in my egrep to look out for pattern like

line starting with a certain string, spaces at some point, numbers at somepoint, ending with some characters. excluding few strings is one such condition
So I cannot use -v.
This requirement came in for my work Not for assignment.

For matching condition, I can use this
egrep (CAT|MAT|DAT) test.txt

but to negate this, I tried all options like using (^) but couldnt get it.
# 4  
Old 09-21-2015
How about presenting the entire scenario so a fit solution could be worked out?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-21-2015
If really don't want to use -v option with egpre or grep to exclude the lines containing CAT|MAT|DAT strings, then as per my knowledge the best option to use is sed

Code:
sed '/string/d' filename

here we can exclude one matching string at time

More details you can find here how to delete one or more lines from given file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: senthil3d
5 Replies

3. Solaris

How to find multiple strings on different lines in file?

Hello, I have spent considerable amount of time breaking my head on this and reached out here. here is the back ground. OS - Solaris 10 There are two strings '<Orin>sop' and '<Dup>two' which I wanted to look for in a file without the quotes on different lines and ONLY if both strings are... (5 Replies)
Discussion started by: keithTait309875
5 Replies

4. Shell Programming and Scripting

File lines starts with # not processed or exclude that lines from processing

I have a file like below #Fields section bald 1234 2345 456 222 abcs dddd dddd ssss mmmm mmm mmm i need do not process a files stating with # I was written code below while read -r line do if then echo ${line} >> elif then ... (3 Replies)
Discussion started by: Chenchireddy
3 Replies

5. Shell Programming and Scripting

Including EOL in egrep pattern for multiple lines

Hi all I need your help to get a high-performance solution. I am working on a extensive script to automate file restores using the bprestore tool on a Solaris 5.10 server (bash 3.00). I will only paste the needed parts of the script to avoid any confusion. To use the script the user has to... (2 Replies)
Discussion started by: Anonym
2 Replies

6. Shell Programming and Scripting

Egrep strings on different lines in file

test.txt: appleboy orangeletter sweetdeal catracer conducivelot I want to only grep out lines that contain "appleboy" AND "sweetdeal". however, the closest thing to this that i can think of is this: cat test.txt | egrep "appleboy|sweetdeal" problem is this only searches for all... (9 Replies)
Discussion started by: SkySmart
9 Replies

7. Shell Programming and Scripting

Extract strings from multiple lines into one csv file

Hi all, Please go through my requirement. I have a log file in the location /opt/WebSphere61/AppServer/profiles/EMQbatchprofile/logs/EMQbatch This file contains the follwing pattern data <af type="tenured" id="42" timestamp="May 14 13:44:13 2011" intervalms="955.624"> <minimum... (8 Replies)
Discussion started by: satish.vampire
8 Replies

8. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

9. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

10. 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
Login or Register to Ask a Question