-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.

M4tt
  • 1
  • 2

3 Answers3

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
Frederik
  • 3,293
  • 3
  • 30
  • 46
  • 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.

Florin Asăvoaie
  • 6,932
  • 22
  • 35
0

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

J.

cerien
  • 21
  • 2