Grep -v lines starting with pattern 1 and not matching pattern 2


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep -v lines starting with pattern 1 and not matching pattern 2
# 1  
Old 07-14-2015
Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all!

Thanks for taking the time to view this!

I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern.

Example:

Code:
Drink a soda
Eat a banana
Eat multiple bananas
Drink an apple juice
Eat an apple
Eat multiple apples

I want to remove all lines that starts with Eat but does not contain apple. Other lines that dont start with Eat should be ignored. Result:

Code:
Drink a soda
Drink an apple juice
Eat an apple
Eat multiple apples

I have looked for a solution but so far no luck... Is it possible to grep it?

Thanks a lot!

Last edited by Don Cragun; 07-14-2015 at 08:43 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 07-14-2015
I believe awk would be easier. Your logic would be ! ( /^Eat/ && ! /apple/), or, following De Morgan's law:

Code:
$ awk '! /^Eat/ || /apple/' input
Drink a soda
Drink an apple juice
Eat an apple
Eat multiple apples

or if it really has to be grep:

Code:
mute@tiny:~$ grep -e '^[^E][^a][^t]' -e apple input
Drink a soda
Drink an apple juice
Eat an apple
Eat multiple apples

These 2 Users Gave Thanks to neutronscott For This Post:
# 3  
Old 07-14-2015
Thanks a lot!

The awk worked out just nice, however I could not get the grep one to do the same... anyways, thank you very much!
# 4  
Old 07-15-2015
grep only solution, but NOT elegant nor efficient:
Code:
{ grep -iv "^eat" file; grep "apple" file; } | grep -f- file
Drink a soda
Drink an apple juice
Eat an apple
Eat multiple apples

# 5  
Old 07-15-2015
Code:
sed '/^Eat/{/apple/!d;}' input

No easy solution with grep, because it reports all positives or all negatives (-v option).

---------- Post updated at 06:37 AM ---------- Previous update was at 03:53 AM ----------

neutronscott's grep solution does not print
Code:
Fat fish

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 07-15-2015
Another inelegant grep, but one with a single pass :
Code:
grep -E '^[^E]|^E[^a]|^Ea[^t]|apple' file

Like MadeInGermany says, grep isn't really well suited for this..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-15-2015
Hi.

An alternative to grep, a Ruby code, glark, can do this:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate multiple matches, inverted, glark.
# For glark, see repository (Debian, Mac OS X "port"), otherwise:
# https://github.com/jpace/glark

# 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 "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C ruby glark

FILE=${1-data1}

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

pl " Expected output:"
cat expected-output.txt

# Remove lines that start with Eat  and does not contain apple
pl " Results (no color, no line numbers):"
glark -U -N -v --and 0 '/^Eat/' '!/apple/' $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
glark version 1.8.0

-----
 Input data file data1:
Drink a soda
Eat a banana
Eat multiple bananas
Drink an apple juice
Eat an apple
Eat multiple apples
Fat fish

-----
 Expected output:
Drink a soda
Drink an apple juice
Eat an apple
Eat multiple apples
Fat fish

-----
 Results (no color, no line numbers):
Drink a soda
Drink an apple juice
Eat an apple
Eat multiple apples
Fat fish

Best wishes ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep file starting from pattern matching line

I have a file with a list of references towards the end and want to apply a grep for some string. text .... @unnumbered References @sp 1 @paragraphindent 0 2017. @strong{Chalenski, D.A.}; Wang, K.; Tatanova, Maria; Lopez, Jorge L.; Hatchell, P.; Dutta, P.; @strong{Small airgun... (1 Reply)
Discussion started by: kristinu
1 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Grep lines before a pattern having some other pattern

Hi All, I am trying to fetch lines before a pattern, I got to know about -B flag in grep but we have to pass the number to get those lines before some pattern say (X), now what if I want to get line/s with some other pattern say (Y) before X pattern? How to get about it? please help. Input:... (5 Replies)
Discussion started by: dips_ag
5 Replies

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

delete lines starting with a pattern

i have a file sample.txt containing i want to delete lines starting with 123 neglecting spaces and tabs. but not lines containing 123. i.e. i want files sample.txt as help me thanxx (4 Replies)
Discussion started by: yashwantkumar
4 Replies

8. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question