Whin3
Whin3

Reputation: 725

awk find patterns (stored in a file) in a file

I am learning awk and I am having a hard time trying to do this :

I have a file, let's name it pattern_file.txt, which contains multiple patterns, one per line. For example, it looks like this :

pattern_file.txt

PATTERN1
PATTERN2
PATTERN3
PATTERN4

I have a second file, containing some text. Let's name it text_file.txt. It looks like this:

text_file.txt

xxxxxxxxPATTERN1xxxxxxx
yyyyyPATTERN2yyyy
zzzzzzzzzPATTERN3zzzzzz

What I am trying to do is : If one of the patterns in the pattern_file.txt is present in the current line read in text_file.txt, print the line.

I know how to print a line with awk, what gives me a hard time is to use the pattern stored in the pattern_file.txt and verify if one of them is present.

Upvotes: 1

Views: 2744

Answers (2)

Inian
Inian

Reputation: 85560

A variation of this helpful James Brown's answer using match() which also does regex match (and) returns the starting index of the matching string,

awk 'FNR==NR{a[$0]; next}{for (i in a) if (match($0,i)) print}' pattern_file.txt text_file.txt

which returns me the lines needed.

On printing the return values from the match() function

awk 'FNR==NR{a[$0]; next}{for (i in a) if (match($0,i)) print RSTART}' pattern_file.txt text_file.txt

gives an output as

9   # Meaning 'PATTERN1' match started at index 9 in 'xxxxxxxxPATTERN1xxxxxxx' 
6
10

Upvotes: 2

James Brown
James Brown

Reputation: 37394

In awk using index

awk 'NR==FNR{a[$1];next}{for(i in a)if(index($0,i)) print}' pattern text
xxxxxxxxPATTERN1xxxxxxx
yyyyyPATTERN2yyyy
zzzzzzzzzPATTERN3zzzzzz

Store "patterns" to a hash and for each record use index to try to find the "patterns" from the record.

Upvotes: 4

Related Questions