Sed question

Matthew Seaman m.seaman at
Thu Apr 15 16:29:52 BST 2004

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Apr 15, 2004 at 03:25:06PM +0100, Dave Tiger wrote:

> Sorry to be a bore but I cant get my head around sed and adding text to a
> file using it.
> I need to find the instance of "" which is at the end of a
> line in a file and add a line of text under it.
> Sed -e "" -e "s/text to add/g" filename=20
> Dose not work, I got the example of a website.
> Any help with this last piece of the puzzle is appreciated.
> Thank you in advance

As I remember, you wanted to search a file for any lines containing
'' and add some text to the following line:

    % sed -i~ -e '/$/a\
    text-to-add' filename

Yes -- that is broken into two lines: it's a peculiarity of sed.  You
have to insert a *literal* newline protected with a backslash.  You
may need to double up the backslash at the end of the first line
depending on what shell you're using.

An alternative way of doing the job is:

    % perl -pi~ -e 'm/$/ && $_ .=3D "text-to-add\n";' filena=

Both of those commands save a copy of the original file as 'filename~'.=20



Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                      Savill Way
PGP:         Marlow
Tel: +44 1628 476614                                  Bucks., SL7 1TH UK

Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.2.4 (FreeBSD)



More information about the Ukfreebsd mailing list