Loading...

Extract email addresses from text file using regex with bash or command line

Answer #1 100 %

If you know the field position then it is much easier with awk or cut:

awk -F ',' '{print $7}' file

OR

cut -d ',' -f7 file
Answer #2 100 %

If you still want to go the grep -o route, this one works for me:

$ grep -i -o '[A-Z0-9._%+-]\[email protected][A-Z0-9.-]\+\.[A-Z]\{2,4\}' file.csv
[email protected]
$ 

I appear to have 2 versions of grep in my path, 2.4.2 and 2.5.1. Only 2.5.1 appears to support the -o option.

Your regular expression is close, but you're missing 2 things:

  • regular expressions are case sensitive. So you can either pass -i to grep or add extra a-z to your square bracket expressions
  • The + modifiers and {} curly braces appear to need to be escaped.

You’ll also like:


© 2023 CodeForDev.com -