Sed question

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


--wRRV7LY7NUeQGEoC
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.
>=20
> I need to find the instance of "domain.com-edit" which is at the end of a
> line in a file and add a line of text under it.
>=20
> Sed -e "domain.com-edit" -e "s/text to add/g" filename=20
>=20
> Dose not work, I got the example of a website.
>=20
> Any help with this last piece of the puzzle is appreciated.
>=20
> Thank you in advance

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

    % sed -i~ -e '/domain.com-edit$/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/domain.com-edit$/ && $_ .=3D "text-to-add\n";' filena=
me

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

	Cheers,

	Matthew

--=20
Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                      Savill Way
PGP: http://www.infracaninophile.co.uk/pgpkey         Marlow
Tel: +44 1628 476614                                  Bucks., SL7 1TH UK

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (FreeBSD)

iD8DBQFAfqpwdtESqEQa7a0RAsdpAKCRo5izHWedExnxlwEgatV6yPUOnACfU7xU
f+rbt8gOE/WwH4qOswlk7lM=
=widn
-----END PGP SIGNATURE-----

--wRRV7LY7NUeQGEoC--




More information about the Ukfreebsd mailing list