The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regex pppswing Shell Programming and Scripting 5 06-30-2008 10:22 PM
A simple find and replace without using any regex (bash) srikanths Shell Programming and Scripting 2 03-18-2008 08:08 AM
Regex deepakpv Shell Programming and Scripting 6 03-28-2007 04:18 AM
Regex?? Please help lunac UNIX for Dummies Questions & Answers 7 01-30-2007 01:13 PM
sed regex Shakey21 UNIX for Dummies Questions & Answers 4 01-31-2002 09:16 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2008
evilfreakz evilfreakz is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
Need Help with Simple Regex

I have got a question. How to do this? I mean AND expression in regex.

List all the files in current directory that do not contain the words use AND take.

Thx.
  #2 (permalink)  
Old 08-31-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Assuming you want an answer rather than a theory, something like

Code:
for f in *; do
  grep use "$f" >/dev/null && continue
  grep take "$f" >/dev/null && continue
  # file contains neither if we get to here; report its file name
  echo "$f"
done
If you really do require this to be done in regular expressions exclusively, there is no simple way to specify this in regular expressions. Theoretically there could be an operator & to parallel the operator | but in practice, it is fairly useless, and also complicates the regex engine a fair bit (if I recall the gist of the research papers on this topic correctly).
  #3 (permalink)  
Old 08-31-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 240
From the top of my head, I would use something like

Code:
grep -L use $(grep -L take *.txt)
which means, first (in the prenthesis) find all files that do not contain the word take, and in that list of files, find all the files that do not contain the word use.
But I'm sure there is a way to use OR in the regexp..

/Lakris
  #4 (permalink)  
Old 08-31-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Use egrep, search for $var1 OR $var2:

Code:
egrep "[$var1]|[$var2]" file
Invert match:

Code:
egrep -v "[$var1]|[$var2]" file
Search for $var1 AND $var2:

Code:
egrep "$var1.*$var2|$var2.*$var1" file
Invert match:

Code:
egrep -v "$var1.*$var2|$var2.*$var1" file
Regards

Last edited by Franklin52; 08-31-2008 at 06:29 AM.. Reason: Adding invert match
  #5 (permalink)  
Old 08-31-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
i think OP means the words "use" and "take" should not be in the file. So the egrep solution will not work is "use" and "take" are on separate lines.
  #6 (permalink)  
Old 08-31-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Quote:
Originally Posted by ghostdog74 View Post
i think OP means the words "use" and "take" should not be in the file. So the egrep solution will not work is "use" and "take" are on separate lines.
Sorry, just awake , I have to read the question thoroughly.

Regards
  #7 (permalink)  
Old 08-31-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 240
useless use of cat?

I guess I misinterpreted the OP, now here is my supersilly superuseless use of cat and pipe...

Code:
for x in *.txt;do cat $x|tr "\n" " "|egrep '(use.*take|take.*use)'&>/dev/null; [ $? == 1 ] && echo $x;done
but I think it gets the job done?

/Lakris
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0