What the equivalent of “grep | cut” using sed or awk

awkcatcutgrepsed

Say I had a config file /etc/emails.conf

email1 = user@dinkum.dorg 
email2 = user@winkum.worg
email3 = user@stinkum.storg

and I wanted to get email2

I could do a:

grep email2 /etc/emails.conf | cut -d'=' -f2 

to get the email2, but how do I do it "cooler" with one sed or awk command and remove the whitespace that the cut command would leave?

Best Answer

How about using awk?

awk -F = '/email2/ { print $2}' /etc/emails.conf
  • -F = Fields are separated by '='

  • '/email2/ { print $2}' On lines that match "email2", print the second field

Related Question