aocferreira
aocferreira

Reputation: 693

Using grep with regular expression

The command grep -vf 1.txt 2.txt > 3.txt puts in 3.txt what 2.txt has that 1.txt doesn't have, but how can I use grep to only to compare the strings that have (0-9)?

Example:

(0001) compare  
test   ignore  
984    ignore  
(10)   compare  
(1242342542) compare  

Upvotes: 0

Views: 169

Answers (3)

pyr0
pyr0

Reputation: 377

This should work too:

awk 'FNR==NR {arr[$0];next} !($1 in arr)' 1.txt 2.txt

regards

Upvotes: 0

Kent
Kent

Reputation: 195039

the easy way would be:

before saving to 3.txt, pipe your result to grep -E '[0-9]+' > 3.txt

looks like: grep -vf.... |grep -E '[0-9]+' > 3.txt

if you give some example of 1.txt , 2.txt and expected 3.txt, there might be an efficient way.

Upvotes: 0

Shawn Chin
Shawn Chin

Reputation: 86844

If you wish to keep only results that match the pattern, you can simply post-process the output:

grep -vf 1.txt 2.txt | grep '([0-9]\+)' > 3.txt

Or, if you wish to use only lines from 1.txt that matches the pattern for the comparison, you could try:

grep -vf <(grep "([0-9]\+)" 1.txt) 2.txt > 3.txt

Upvotes: 1

Related Questions