Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 Mar 1999 22:28:10 +1000
From:      Greg Black <gjb@comkey.com.au>
To:        cjclark@home.com
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: sed and newlines 
Message-ID:  <19990318122811.11031.qmail@alpha.comkey.com.au>
In-Reply-To: <199903172339.SAA06674@cc942873-a.ewndsr1.nj.home.com>  of Wed, 17 Mar 1999 18:39:00 EST
References:  <199903172339.SAA06674@cc942873-a.ewndsr1.nj.home.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> 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 <gjb@acm.org>



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




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