reverse matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reverse matching
# 1  
Old 04-07-2011
reverse matching

Hello guys

How can I use egrep to match word1 but not word2...word1.

What I mean

suppose that I have the following text, and my word1=pizza and word2=eat

I hate to eat pizza because I ma eating it each day
Pizza is good
I like vegetarian and Italian Pizza
eating healthy food is good, but I can not resist The pizza
what about pizza or do you prefer burger

So I want to get

pizza is good
I like vegetarian and Italian Pizza
what about pizza or do you prefer burger


Thanks in advance
# 2  
Old 04-07-2011
Something like:
Code:
grep pizza file | grep -v eat

?
# 3  
Old 04-07-2011
In there is another way to do it without -v for two reasons. The files I am working on is extremely large and I am just interested to see if the pattern exist or not
i.e
Currently I am using
cat $path/tmpFile.csv | egrep "$word1" -i -q
if [ "$?" -eq "0" ]; then
echo "found"
...

I found this link
regex - Regular expression to match string not containing a word? - Stack Overflow

But I could not get it
# 4  
Old 04-07-2011
Try:
Code:
grep -i pizza "$path/tmpFile.csv" | grep -iv eat > /dev/null
if [ "$?" -eq "0" ]; then 
echo "found"

# 5  
Old 04-07-2011
thanks for the reply bartus11
I will use your method
Regards
# 6  
Old 04-07-2011
you can use awk with operators
Code:
$ awk 'BEGIN{IGNORECASE=1}/pizza/ && !/eat/' file

Or Ruby(1.9+)

Code:
$ ruby -ne 'print if /pizza/i && !/eat/' file

# 7  
Old 04-07-2011
But then $? is useless Smilie It is set to "0" even if the string was not found, so you have to code whole solution in AWK.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

4. Shell Programming and Scripting

To reverse a string

Hi All, I would like to know , how to reverse a given string example : Hi how are you Required Output: you are how HiThanks (7 Replies)
Discussion started by: santhoshks
7 Replies

5. Shell Programming and Scripting

reverse string matching

Guys, I am trying to find a way to achieve this. I need to print /usr/local/apche/htdocs only from the string /usr/local/apache/htdocs/file.php using the regex. The below did not work. I know a solution with normal cut, I need a way to do this with the awk regex. awk '/+file.php/' (6 Replies)
Discussion started by: anilcliff
6 Replies

6. Programming

String reverse

Hi all, I jus wanna print string b after reversing it. but the out put is blank. My code snippet is below. :wall: int main() { char * a, * b; b = new char; a = new char; int len, le; le = 0; cout<< " enter your string \n"; cin>> a; len = strlen(a); for(int i =... (8 Replies)
Discussion started by: vineetjoshi
8 Replies

7. IP Networking

Reverse DNS

Hello, I'm trying to get reverse dns to point to my domain on network but I'm failing. I am using bind dns with port 53 enabled and my ISP is mediacom. Currently my reverse dns is *.client.mchsi.com and I would like to make it example.com basically. My bind configuration I have 2 records, one... (4 Replies)
Discussion started by: GRMrGecko
4 Replies

8. Shell Programming and Scripting

How to reverse output?

hi, I have to reverse the command output like below: output: online offline disable maintening killed How to reverse this output like: killed maintening disable offline online It should be ksh script. (4 Replies)
Discussion started by: a2156z
4 Replies

9. Shell Programming and Scripting

Reverse FTP

Hi Everybody, I want to write a script in unix which will automatically FTP a .txt file from my client machine D: drive(Windows) That is I want to FTP a file from my PC to UNIX box but this should be done from UNIX box by a shell script. (i.e. I will invoke the script in UNIX and FTP will be... (4 Replies)
Discussion started by: ganesh123
4 Replies

10. Shell Programming and Scripting

Reverse *

when I do $ ls z* List of all files begining with 'z'. But what if I want to do a reverse lookup. Just for interest sake ;) $ ls ztr should be same as $ ls ztr* $ ls zt* $ ls z* (2 Replies)
Discussion started by: azmathshaikh
2 Replies
Login or Register to Ask a Question