What the equivalent of "grep | cut" using sed or awk?

13,002

Solution 1

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

Solution 2

The exact equivalent would be something like:

sed -n '/email2/{s/^[^=]*=\([^=]*\).*/\1/;p;}' < file

But you'd probably want instead:

sed -n 's/^[^=]*email2[^=]*=[[:blank:]]*//p' < file

(that is match email2 only on the part before the first = and return everything on the right of the first = (skipping leading blanks), not only the part up to the second = if any).

Solution 3

perl -nlE 's/email2\s*=\s*// and say'    file

Where:

  • perl -nl is a for each line do...
  • s/email2 = // removes the searched email id and if you could do it ...
  • say prints the current input input line
  • \s* zero or more spaces (equivalent to [ \t\n]*)
Share:
13,002

Related videos on Youtube

Peter Turner
Author by

Peter Turner

Faithful Catholic - Father of 5, Husband of 1 Programmer of cloudish things from Southern Wisconsin.

Updated on September 18, 2022

Comments

  • Peter Turner
    Peter Turner over 1 year

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

    email1 = [email protected] 
    email2 = [email protected]
    email3 = [email protected]
    

    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?

  • cuonglm
    cuonglm about 9 years
    Is space required between { s?
  • Stéphane Chazelas
    Stéphane Chazelas about 9 years
    @cuonglm, no. AFAICT, ;} above is not POSIX but I don't know of any modern sed implementation where that fails.
  • cuonglm
    cuonglm about 9 years
    Yes, that's exactly I have stucked when working with this. After re-reading POSIX sed documentation, I see ;} is an accepted extension, but it's not required. It's strange that GNU sed with --posix option still allow {command} form. Do you think it's a bug?
  • Peter Turner
    Peter Turner about 9 years
    can it trim the leading space too?
  • Stéphane Chazelas
    Stéphane Chazelas about 9 years
    @cuonglm {command} is not required to report an error by POSIX. It's just that a POSIX script must not use it as the behaviour is not specified there. Where GNU sed is not conformant even with --posix is that it doesn't accept ; in label names.
  • Peter Turner
    Peter Turner about 9 years
    is it preferable to us "<" vs just using the file as the argument (I'm putting this into a bash script)?
  • Peter Turner
    Peter Turner about 9 years
    I'll give you a +1 for golfing, but where I'm running this (some sort of stripped down Cisco appliance), I'm not sure I'm going to have perl, especially the version of perl that has say.
  • JJoao
    JJoao about 9 years
    perl 5.10 appeared 7 year ago but sometimes we don't have it... perl -nle 's/email2\s*=\s*// and print'
  • Stéphane Chazelas
    Stéphane Chazelas about 9 years
  • Wildcard
    Wildcard over 7 years
    I can't find where ;} is mentioned in the Sed POSIX specs as an accepted extension. It seems to be required by The <right-brace> shall be preceded by a <newline> or <semicolon>. @cuonglm or Stephane, could you clarify?
  • cuonglm
    cuonglm over 7 years
    @Wildcard Yes, ;} is not POSIX, that what Stephane said in his comment above. But I remember it will be in next POSIX version, there's an issue in austin bug group.
  • Wildcard
    Wildcard over 7 years
    @cuonglm, no, I mean I don't see it as just an extension. "The right brace shall be preceded by a newline or semicolon...." So if you precede it with a semicolon in your Sed script, haven't you followed the explicit instructions of POSIX?
  • Wildcard
    Wildcard over 7 years
    @cuonglm, huh, I still don't see it in the specs. But if it will be changed in the next POSIX edition I suppose it's not critical. :)
  • cuonglm
    cuonglm over 7 years
    @Wildcard: The austinbug group link austingroupbugs.net/view.php?id=961
  • cuonglm
    cuonglm over 7 years
    @Wildcard Ops, look like the spec was changed follow that issue.
  • Wildcard
    Wildcard over 7 years
  • cbarrick
    cbarrick over 5 years
    Unfortunately, this answer does little to explain why the command works. I've posted an alternative sed answer with that explanation.