Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Mar 2016 08:56:27 -0700 (MST)
From:      Warren Block <wblock@wonkity.com>
To:        David Banning <david+dated+1457496338.968870@skytracker.ca>
Cc:        questions@freebsd.org
Subject:   Re: sed help please
Message-ID:  <alpine.BSF.2.20.1603040855350.53946@wonkity.com>
In-Reply-To: <20160304040536.GA7729@skytracker.ca>
References:  <20160304040536.GA7729@skytracker.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 3 Mar 2016, David Banning wrote:

> I am trying to change hundreds of lines of text. Given the following text;
>
> line 1
> line 2 foo take this text
> line 3
> line 4
> line 5 bar leave this text
> line 6
> line 7
>
> I need a sed command that would take everything between foo and bar -
> including foo and bar.
>
> Ideally the output would look like;
>
> line 1
> line 2
> leave this text
> line 6
> line 7
>
> Keep in mind that foo and bar appear in different
> locations - sometimes at the beginning of a line, sometimes at the end,
> and sometimes in the middle.  I found someone who posted the following
> solution;
>
> sed '/foo/,/bar/{s/./x/g}' file
>
> but I found that this does not execute under FreeBSD.  I have looked
> around for differences between FreeBSD and other unix like SED operations
> but only see the -s "", regarding backup file.

FreeBSD's sed and awk are... well, let's say "historical".  Perl has
much more powerful regular expressions, character classes, and slurp
mode.

However, this bit of Perl reads a file from stdin or as a following
argument and removes everything between the "foo" and "bar", inclusive:

perl -0777 -pe 's/foo.*?bar//sg'




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?alpine.BSF.2.20.1603040855350.53946>