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]*)
Related videos on Youtube
Author by
Peter Turner
Faithful Catholic - Father of 5, Husband of 1 Programmer of cloudish things from Southern Wisconsin.
Updated on September 18, 2022Comments
-
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 about 9 yearsIs space required between
{ s
? -
Stéphane Chazelas about 9 years@cuonglm, no. AFAICT,
;}
above is not POSIX but I don't know of any modernsed
implementation where that fails. -
cuonglm about 9 years
-
Peter Turner about 9 yearscan it trim the leading space too?
-
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 GNUsed
is not conformant even with--posix
is that it doesn't accept;
in label names. -
Peter Turner about 9 yearsis it preferable to us "<" vs just using the file as the argument (I'm putting this into a bash script)?
-
Peter Turner about 9 yearsI'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 about 9 yearsperl 5.10 appeared 7 year ago but sometimes we don't have it...
perl -nle 's/email2\s*=\s*// and print'
-
Stéphane Chazelas about 9 years@PeterTurner, see When should I use input redirection?.
-
Wildcard over 7 yearsI can't find where
;}
is mentioned in the Sed POSIX specs as an accepted extension. It seems to be required byThe <right-brace> shall be preceded by a <newline> or <semicolon>
. @cuonglm or Stephane, could you clarify? -
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 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 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 over 7 years@Wildcard: The austinbug group link austingroupbugs.net/view.php?id=961
-
cuonglm over 7 years@Wildcard Ops, look like the spec was changed follow that issue.
-
Wildcard over 7 yearsLet us continue this discussion in chat.
-
cbarrick over 5 yearsUnfortunately, this answer does little to explain why the command works. I've posted an alternative
sed
answer with that explanation.