-5

I've got a file with some e-mails:

info@example.com info51351@hotmail.com
test@hello.com test6261@gmail.com
example@example.com example64262@gmail.com

And now... I need to find a way to search for specific e-mails address from the list on the left hand-side (before white-space), I already tried to grep/sed it but normally when I'm searching for word "info", I'm getting both results info@example.com and info51351@hotmail.com...

How I can tell linux to check only string before white-space?

Thanks.

1

Take the following dataset:

# cat mailaddresses.txt
info@example.com info51351@hotmail.com
test@hello.com test6261@gmail.com
example@example.com example64262@gmail.com
infotest@example2.com yadayada@example.com
superinfo@example.com yada@example.com

Using awk to get the first column:

# awk '{print $1;}' mailaddresses.txt
info@example.com
test@hello.com
example@example.com
infotest@example2.com
superinfo@example.com

And to get all the info@ you could just pipe the output to grep:

# awk '{print $1;}' mailaddresses.txt | grep '^info@'
info@example.com

And if you want all emails containing info before the @:

# awk '{print $1;}' mailaddresses.txt | egrep '^.*info.*@'
info@example.com
infotest@example2.com
superinfo@example.com
  • 1
    You don't even need to use grep, awk can also perform pattern/string matching natively: awk '$1 ~ /^info@/ {print $1;}' mailaddresses.txt – HBruijn Sep 13 '16 at 11:28
0

For example

grep ' info@' file for matching the one on the second column.

egrep '^info@' file for matching the one on the first column.

Just put the match string between quotes so that bash executes grep with the whole string as a parameter rather than parsing them as different parameters.

  • Can I display the first value (from the left hand-side) without the an e-mail from the right hand-side? – M4tt Sep 11 '16 at 15:18
  • 2
    Learn how to use awk for that. – Florin Asăvoaie Sep 11 '16 at 17:59
0

grep 'info@.* ' file should do the trick alternatively, and ugly, awk '{print $1}' file | grep 'info@'

J.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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