Date: Fri, 4 Mar 2016 13:04:29 +0000 From: Arthur Chance <freebsd@qeng-ho.org> To: David Banning <david+dated+1457496338.968870@skytracker.ca>, questions@freebsd.org Subject: Re: sed help please Message-ID: <56D987DD.8080801@qeng-ho.org> 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 04/03/2016 04:05, 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.
>
> Any pointers would be helpful.
Always nice to have a chance to play with sed, it's the assembler of
editing, complete with branches and labels. Put this into file
foobar.sed (without the delimiters of course)
---- start ----
:copy
/foo/bfoo
p
n
bcopy
:foo
s/foo.*//p
:cut
n
/bar/bbar
bcut
:bar
s/.*bar//
bcopy
---- end ----
then invoke with
sed -nf foobar.sed < infile > outfile
The one difference from what you asked for is that it produces
line 1
line 2
leave this text
line 6
line 7
with the space after the bar remaining. If you want to lose that, change
s/.*bar// to s/.*bar //. It also won't handle a single line with both
foo and bar together, that's left as an exercise for the reader (but is
fairly trivial).
--
Moore's Law of Mad Science: Every eighteen months, the minimum IQ
necessary to destroy the world drops by one point.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?56D987DD.8080801>
