From owner-freebsd-questions@freebsd.org Fri Mar 4 13:04:39 2016 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4156E9DB22B for ; Fri, 4 Mar 2016 13:04:39 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from mailman.ysv.freebsd.org (mailman.ysv.freebsd.org [IPv6:2001:1900:2254:206a::50:5]) by mx1.freebsd.org (Postfix) with ESMTP id 2F6F281E for ; Fri, 4 Mar 2016 13:04:39 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: by mailman.ysv.freebsd.org (Postfix) id 2BC1B9DB22A; Fri, 4 Mar 2016 13:04:39 +0000 (UTC) Delivered-To: questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2B5679DB229 for ; Fri, 4 Mar 2016 13:04:39 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from bede.qeng-ho.org (bede.qeng-ho.org [217.155.128.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "fileserver.home.qeng-ho.org", Issuer "fileserver.home.qeng-ho.org" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id C951B81C for ; Fri, 4 Mar 2016 13:04:38 +0000 (UTC) (envelope-from freebsd@qeng-ho.org) Received: from arthur.home.qeng-ho.org (arthur.home.qeng-ho.org [172.23.1.2]) by bede.home.qeng-ho.org (8.15.2/8.15.2) with ESMTP id u24D4TWc086544; Fri, 4 Mar 2016 13:04:30 GMT (envelope-from freebsd@qeng-ho.org) Subject: Re: sed help please To: David Banning , questions@freebsd.org References: <20160304040536.GA7729@skytracker.ca> From: Arthur Chance Message-ID: <56D987DD.8080801@qeng-ho.org> Date: Fri, 4 Mar 2016 13:04:29 +0000 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <20160304040536.GA7729@skytracker.ca> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Mar 2016 13:04:39 -0000 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.