Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

by way of example, I've a big text file with many email address, using bash I need search/verify that an email exists (or no). Should be use (only) the "anchors"?

grep '^user1@example.com' text_file

or there're better ways? I need create a bash script and I'd like be safe.

share|improve this question
1  
Is the email the only word on a line? –  glenn jackman Jul 24 at 13:32
    
indeed: the file has this format: user1@example.com example.com/user1 –  Pol Hallen Jul 24 at 13:38
1  
In that case, I'd use grep -q '^user1@example\.com\>' -- with a line anchor at the start, and an end-of-word anchor at the end. –  glenn jackman Jul 24 at 14:27

4 Answers 4

See the -F (fixed string, as opposed to regular expression) and -x (exact: match the whole line) options.

grep -Fx user1@example.com text_file

would be the equivalent of:

grep '^user1@example\.com$' text_file

(remember that . is a regular expression operator that matches any character).

Use the -q option if you only want to check if there's such a line:

grep -Fxq user1@example.com text_file &&
  echo yes, that address is in that file.

If the line to search and the file name are variable:

grep -Fxqe "$email" < "$file"

Or

grep -Fxq -- "$email" < "$file"

You don't want:

grep -Fxq "$email" "$file"

as that would cause problems if $email or $file started with -.

If the file is sorted (in your current locale, preferably C), you can possibly speed things up by using comm instead of grep:

printf '%s\n' user1@example.com | comm -12 - text_file

The advantage will become more obvious when you have several email addresses to check (for instance in another sorted file):

comm -12 text_file emails_to_check

would be faster than:

grep -Fxf emails_to_check text_file
share|improve this answer
    
AFAIK, grep -Fxq -- "$email" "$file" also works. –  vinc17 Jul 24 at 13:28
    
stephane, why did you switch from a file input (handled by grep) to stdin using the < redirector? are there any advantages? –  umläute Jul 24 at 13:30
    
@umläute and vinc17. As I said, it's to cover for filenames starting with -. even grep -- "$email" "$file" would be a problem for a file called - (which grep treats specially as meaning stdin) –  Stéphane Chazelas Jul 24 at 14:08

To be as efficient as possible, you want to stop after the first match is found. If you have GNU grep, you can do this:

grep -m 1 '^user1@example\.com$' your_file

If you don't, you can use Perl:

perl -nlE 'say and last if $_ eq q{user1@example.com}' your_file
share|improve this answer
3  
-m is GNU specific. Use the POSIX -q if you want to efficiently check that there's such a line. –  Stéphane Chazelas Jul 24 at 13:12

There are a lot of email checks there. One of those is:

grep -E -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" text_file

To elaborate my answer.

You are using the ^ anchor which indicates the start of the string. This won't match if an email address is somewhere in between a long string.

share|improve this answer
2  
Thanks. That's is a generic grep options to "extract" all email address inside a file. I need searching for one by one email address using read EMAIL then using grep to check it. –  Pol Hallen Jul 24 at 13:01

your grep command will match everything that starts with ^user1@example.com, including the email address itself, but also user1@example.com.spammer.com. since . is a special character in regular expressions that matches any key, you should escape it as \.

assuming that your textfile contains one address per line, use:

EMAIL=user1@example\\.com
egrep "^${EMAIL}$" text_file

the trailing $ will make sure that the line ends after the email-address. i'm also using double-quotes ", as these allow to use variables (unlike single-quotes ')

share|improve this answer
1  
That also matches user1@example-com. –  Stéphane Chazelas Jul 24 at 13:09
    
@StéphaneChazelas you're of course right; updated the answer. –  umläute Jul 24 at 13:28
    
@umläute You need to double the backslash. But it's better to use -Fx. –  vinc17 Jul 24 at 13:33
    
@vinc17, doh; bash escaping; anyhow, yes i agree that it is better to use -Fx but that's stephane's answer :-) –  umläute Jul 24 at 13:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.