[Cialug] sed -e -e

Jeffrey Ollie jeff at ocjtech.us
Wed May 8 17:05:11 UTC 2019


On Wed, May 8, 2019 at 11:20 AM Charles Dunbar <ccdunbar at gmail.com> wrote:

> >
> > As an aside, I sometimes wish I could do that with grep, instead of
> > using pipes. Like, grep a file for lines that contain "pattern1" *and*
> > "pattern2": grep -e "pattern1" -e "pattern2" file
>
> Good news!
>
> `egrep "pattern1|pattern2" file`
> `grep -E "pattern1|pattern2" file`
>

Which doesn't do what Todd asked for - "|" is an OR condition, when Todd
wants lines that contain both. Off the top of my head I can't think of a
way that grep can do what Todd wants except to pipe the results of one grep
through another. As powerful as grep and regular expressions are they do
have limits.

You could use:

(pattern1.*pattern2)|(pattern2.*pattern1)

but that would fail if it's possible for the patterns to overlap.

Sed can do it though:

cat file | sed -e '/pattern1/!d' -e '/pattern2/!d'

That should output only lines from "file" that contain both patterns.


> On Wed, May 8, 2019 at 11:04 AM Todd Walton <tdwalton at gmail.com> wrote:
>
> > It looks to me like if I run a sed command with multiple "-e" scripts,
> > each one applies in turn. If I add a line that says "blah blah" with
> > one "-e", and then a subsequent "-e" replaces "blah" with "yak", I'll
> > end up with text that says "yak yak". I was wondering if each one
> > applied in turn, or if each one operated on the original text, or
> > what. But it seems to be that it's basically adding each "-e" as a
> > line to a full script.
> >
> > As an aside, I sometimes wish I could do that with grep, instead of
> > using pipes. Like, grep a file for lines that contain "pattern1" *and*
> > "pattern2": grep -e "pattern1" -e "pattern2" file
> >
> > --
> > Todd
> > _______________________________________________
> > Cialug mailing list
> > Cialug at cialug.org
> > https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>


-- 
Jeff Ollie
The majestik møøse is one of the mäni interesting furry animals in Sweden.


More information about the Cialug mailing list