conditional multiple string matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting conditional multiple string matching
# 1  
Old 11-13-2010
conditional multiple string matching

I need to print fileNames that contains multiple search strings conditionally.

Condition: Any file in the folder that has search strings
---> (b1b && d1d) && ( a1a || c1c ).

i.e File should have both (b1b AND d1d) AND (either a1a or c1c).

Where && is logical AND
|| is logical OR.

Code:
input_xxx.txt
a1=a1a
b1=b1b
c1=c1c
d1=d1d

Here is what I have so far, but its not working.

Code:
 
# How can I get this syntax working!?
$> find . -name \*.* -exec awk '(/b1b/ && /d1d/) && ( /a1a/ || /c1c/ ) {print FILENAME }' {} \;

Code:
 
# But If I give only one condition its working. 
$> find . -name \*.txt -exec awk '/b1b/ {print FILENAME }' {} \;
input_xxx.txt

# 2  
Old 11-13-2010
What you show as code means:
print a file name when a single record has 3 out of 4 search strings.

The example that works tells me that this is not what is in the data, nor is it what you mean. I am guessing that what you want is a list of files that have 3 out of 4 sprinkled somewhere in the file, not all on the same line. This works in ksh:
Code:
grep -l -e 'a1a' -e 'c1c' <( grep -l 'b1b' $(find . -name '*.txt' -exec grep -l 'd1d' {} \;  ))
#expanded out
grep -l  'b1b' $(find . -name '*.txt' -exec grep -l 'd1d' {} \; ) > t.lis
grep -l  -e 'c1c' -e 'a1a' $( < t.lis )

This is process substitution - feeding the output of process a into process b
My bash version == 2.05, so I can't test this in bash, just ksh.
# 3  
Old 11-13-2010
Try this,
Code:
find . -name "input_*.txt" -exec awk '{if(/b1b/){flg++};if(/d1d/){flg++};if(/a1a/ || /c1c/){flg1++} {if(flg==2 && flg1>=1){print FILENAME;flg=0;flg1=0}} }' {} \;

# 4  
Old 11-13-2010
For pravin27's code, if the input txt file has two b1b, but no d1d, it will also export the filename. If there are more b1b, d1d, (more than 2), if will not list it.

Here is the fix:

Code:
find . -name "*.txt" -exec awk '
/b1b/{a=1}
/d1d/{b=1}
/a1a/ || /c1c/ {c=1}
{if (a==1&&b==1&&c==1) {print FILENAME;exit}}' {} \;
./infile.txt

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 11-13-2010
Code:
....
{if (a b c==111) {print FILENAME;exit}}' {} \;

Just another way Smilie
# 6  
Old 11-13-2010
Code:
find ./   \( -name "*b1b*"  -name "*d1d*" \) -a  \( -name "*a1a" -o -name "*c1c*" \)

# 7  
Old 11-13-2010
@becket !!
Quote:
Originally Posted by kchinnam
I need to print fileNames that contains multiple search strings conditionally.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional string search

Hi, I need to search/grep userid and its value based on a condition that the user id is under the heading "*central_test*" in a big file of about 70808MB. The * represents some strings before and after the string "centrla_test". Could anyone please guide. OS is hp-ux. (2 Replies)
Discussion started by: sam_bd
2 Replies

2. Shell Programming and Scripting

Problems with Multiple Pattern String Matching

I am facing a problem and I would be grateful if you can help me :wall: I have a list of words like And I have a datafile like the box of the box of tissues out of of tissues out of the book, the the book, the pen and the the pen and the I want to find Patterns of “x.*x” where... (2 Replies)
Discussion started by: A-V
2 Replies

3. Shell Programming and Scripting

awk if conditional, multiple lines

Hi experts, I have a file with hundreds of lines that looks something like this: Cake 1 3 4 2 3 1 3 Onion 3 2 4 1 3 4 2 Apple 2 3 4 4 1 2 1 Orange 4 3 4 1 2 4 1 Cake 3 1 4 2 4 2 1 Onion 2 4 2 1 4 2 1 Apple 4 3 3 3 3 2 1 Orange 2 1 2 2 2 4 1 Cake 4 3 4 3 1 3 3 ... I'm trying to... (2 Replies)
Discussion started by: david_seeds
2 Replies

4. Shell Programming and Scripting

Matching string from input to string of file

Hi, i want to know how to compare string of file with input string im trying following code: file_no=`paste -s -d "||||\n" a.txt | cut -c 1` #it will return collection number from file echo "enter number" read " curr_no" if ; then echo " current number already present" fi ... (4 Replies)
Discussion started by: a_smith
4 Replies

5. Shell Programming and Scripting

String matching

I have a string like ab or abc of whatever length. But i want to know whether another string ( for example, abcfghijkl, OR a<space> bcfghijkl ab<space> cfghijkl OR a<space>bcfghijkl OR ab<space> c<space> fghijkl ) starts with ab or abc... space might existing on the longer string... If so, i... (4 Replies)
Discussion started by: nram_krishna@ya
4 Replies

6. Shell Programming and Scripting

Matching multiple values in a string

I've been battling with parsing a comma-delimited string, and have had what I would call B- success. I'm using perl and trying to parse out specific identifiers from a string, into a new string. When things are "normal," my regex works fine. When things get complicated, my script fails... (1 Reply)
Discussion started by: linber2880
1 Replies

7. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies

8. UNIX for Dummies Questions & Answers

Matching string

Hello, i have a program where i have to get a character from the user and check it against the word i have and then replace the character in a blank at the same position it is in the word. (7 Replies)
Discussion started by: nehaquick
7 Replies

9. Shell Programming and Scripting

String matching

for a certain directory, I want to grep a particular file called ABCD so what I do is ls /my/dir | grep -i "ABCD" | awk '{print $9}' however, there is also this file called ABCDEFG, the above command would reurn both file when I only want ABCD, please help! (3 Replies)
Discussion started by: mpang_
3 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question