From owner-freebsd-questions@FreeBSD.ORG Thu Feb 2 07:47:45 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C0F7B106566B for ; Thu, 2 Feb 2012 07:47:45 +0000 (UTC) (envelope-from bonomi@mail.r-bonomi.com) Received: from mail.r-bonomi.com (mx-out.r-bonomi.com [204.87.227.120]) by mx1.freebsd.org (Postfix) with ESMTP id 808048FC14 for ; Thu, 2 Feb 2012 07:47:45 +0000 (UTC) Received: (from bonomi@localhost) by mail.r-bonomi.com (8.14.4/rdb1) id q127puMO011247 for freebsd-questions@freebsd.org; Thu, 2 Feb 2012 01:51:56 -0600 (CST) Date: Thu, 2 Feb 2012 01:51:56 -0600 (CST) From: Robert Bonomi Message-Id: <201202020751.q127puMO011247@mail.r-bonomi.com> To: freebsd-questions@freebsd.org In-Reply-To: <20120202062236.GA3419@tinyCurrent> Subject: Re: some kind of binary sed(1) command X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Feb 2012 07:47:45 -0000 > From owner-freebsd-questions@freebsd.org Thu Feb 2 00:27:33 2012 > Date: Thu, 2 Feb 2012 07:22:36 +0100 > From: Matthias Apitz > To: freebsd-questions@freebsd.org > Subject: some kind of binary sed(1) command > > > Hello, > > I have a normal ASCII file wich has in some places two lines of '*', > separated by an empty line, i.e. > > ....\n > *********************\n > \n > *********************\n > ....\n > > and I want to substitute the \n between the star lines by \f; the > 'binary' sed command would just be > > s/*****\n\n*****/*****\n\f*****/ > > which ofc does not work with sed(1) because it is line oriented; > I could write something in perl, C, awk or whatever language, but I > would prefer to do it with the normal commands... any ideas? Use sed. *GRIN* As follows: sed -e ' t l :l /^[*]+$/ { N !/\n$/ { P s/^.*\n// t l } /\n$/ { N !/\n\n[*]*$/ { P s/^.*\n// P s/^.*\n// t l } /\n\n[*]*$/ s/\n\n/\n\f/ } }' Note: "how" this incantation works is left as an excercise for the student. Careful perusal of the FM will provide enlightenment. Caveat: this will convert: {stars} {blank} {stars} {blank} {stars} to; {stars} {formfeed} {stars} {blank} {stars} not: {stars} {formfeed} {stars} {formfeed} {stars}