简体   繁体   中英

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  

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

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.

This should work too:

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

regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM