Need Help with Simple Regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with Simple Regex
# 1  
Old 08-31-2008
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.Smilie
# 2  
Old 08-31-2008
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  
Old 08-31-2008
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  
Old 08-31-2008
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 07:29 AM.. Reason: Adding invert match
# 5  
Old 08-31-2008
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  
Old 08-31-2008
Quote:
Originally Posted by ghostdog74
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 Smilie, I have to read the question thoroughly.

Regards
# 7  
Old 08-31-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. UNIX for Dummies Questions & Answers

All combinations from simple regex

Hi ! Before trying to write a code, is there any program or code that generates all the combinations of strings that simple awk regex can match. By "simple regex" I mean let's say without "+", "*", and with a limited number of characters (e.g. from "1" to "5"). e.g: input: 34?5 output:... (9 Replies)
Discussion started by: beca123456
9 Replies

4. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

6. Shell Programming and Scripting

help with simple regex expression

I am trying to grep the following line in a file using a bash shell: (..) admin1::14959:::::: (..) It works with the following expression (as expected) # cat file | grep ^*:: admin1::14959:::::: but it does not work with (not expected) # cat /etc/shadow | grep ^+:: I assume the... (2 Replies)
Discussion started by: schms
2 Replies

7. Shell Programming and Scripting

Help with simple RegEx on grep

Hello, I am trying to grep my log files for ORA errors, except ORA-00001. I have tried: grep 'ORA*!(-00001)' *.log but it is not working. Any help will be much appreciated. Thank you. (5 Replies)
Discussion started by: drbiloukos
5 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. Shell Programming and Scripting

Simple regex problem?

Hi all, I am looking to create words from a sentence which adhere to a custom search pattern from my website: Example: ! +! / += ~ where the terms ! = not, +! = AND NOT, += - and equals and ~ = can be like.... Now here is the issue...i want to split a sentence like the one above on... (1 Reply)
Discussion started by: muay_tb
1 Replies

10. Shell Programming and Scripting

A simple find and replace without using any regex (bash)

Hi, I need to do an exact find and replace (I don't want to use regular expressions because the input comes from user). I want to find a line that matches the user's input text and replace it with an empty string. For example, let's say the user enters I love "Unix" and the contents of the... (2 Replies)
Discussion started by: srikanths
2 Replies
Login or Register to Ask a Question