From owner-freebsd-questions Thu Mar 18 4:38:57 1999 Delivered-To: freebsd-questions@freebsd.org Received: from alpha.comkey.com.au (alpha.comkey.com.au [203.9.152.215]) by hub.freebsd.org (Postfix) with SMTP id 56B3814DE0 for ; Thu, 18 Mar 1999 04:38:45 -0800 (PST) (envelope-from gjb@comkey.com.au) Received: (qmail 11032 invoked by uid 1001); 18 Mar 1999 12:28:11 -0000 Message-ID: <19990318122811.11031.qmail@alpha.comkey.com.au> X-Posted-By: GBA-Post 1.04 06-Feb-1999 X-PGP-Fingerprint: 5A91 6942 8CEA 9DAB B95B C249 1CE1 493B 2B5A CE30 Date: Thu, 18 Mar 1999 22:28:10 +1000 From: Greg Black To: cjclark@home.com Cc: freebsd-questions@FreeBSD.ORG Subject: Re: sed and newlines References: <199903172339.SAA06674@cc942873-a.ewndsr1.nj.home.com> In-reply-to: <199903172339.SAA06674@cc942873-a.ewndsr1.nj.home.com> of Wed, 17 Mar 1999 18:39:00 EST Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > The sed manpage says, [Snipped] > If I am reading this correctly, > > % sed 's/\n/ /' file > > Should take the file and subsitute three spaces in place of every > newline. However, it does not. It does not seem to understand '\n.' Looks neat and obvious, but it doesn't work because you (like 99.9 per cent of people) have misunderstood the man page, which is one of the great masterpieces of obscurity[1]. > Am I missing something obvious? Or is sed broken? No, sed is not broken -- it's just the wrong tool for this job. Although sed is a wonderful tool, and can do stupendous feats, as soon as you start trying to work on more than single input lines, you have to jump through such bizarre hoops that you are better off turning to the other tools that do that task easily. Look at the simple awk version of what you want: awk '{printf "%s ", $0}' file Not quite as lovely as the imaginary sed line, but still easy to understand and a snack to get right. And awk will let you do tricks like concatenating lines in groups of five, for example. And then you can do all sorts of other things with them. Yes, you can also use Perl, Tcl, Python, C, C++, etc., to do this; but awk is really the right tool for this level of work. ---------- [1] How many people understood what they could do with "hold" spaces and "pattern" spaces and how to shuffle stuff between them on their first reading of the sed man page? And how many then found they didn't understand it after all when they tried to make it work? -- Greg Black To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message