Skip to Content.
Sympa Menu

baslinux - Re: [BL] USING SED TO SUBSTITUTE: was Re: bash help; changing case of file names, hyperlinks

baslinux AT lists.ibiblio.org

Subject: Baslinux mailing list

List archive

Chronological Thread  
  • From: Lee Forrest <lforrestster AT gmail.com>
  • To: baslinux AT lists.ibiblio.org
  • Subject: Re: [BL] USING SED TO SUBSTITUTE: was Re: bash help; changing case of file names, hyperlinks
  • Date: Sun, 25 Feb 2007 13:03:26 +0000

On Sun, Feb 25, 2007 at 05:23:54AM +0000, sindi keesan wrote:
> > I made a testfile containing hello.
> >
> >
> >
> > cat testfile | sed -e "/hello/bye/" > testfile2
> > I get the same file copied, with hello not bye.
> >
> > I tried with and without -e and " "
> >
> >
> > cat testfile | sed /hello/goodbye/ > testfile3
> > sed: Unsupported command o
> > I get an empty file.
> >
> > cat testfile | sed /hello/gbye > testfile4
> > sed: Can't find label for jump to `ye'
> >
> >
> > Where did I go wrong?
> >
> >
> > Sindi
> >
>
> man sed
>
> Searched on substitute and found
>
> [addr2]s/re/replacement/flags
>
> and made a lucky guess at the meaning of it.
>
> cat testfile | sed "s/hello/goodbye/" > testfile2 worked as expected.
>
> You cannot add or subtract slashes but " " is optional here

I'd use either single or double quotes. That's standard. Single quotes
will prevent any variables from being interpreted.

You can use different seperators:

s:/usr:/usr/foo: # :
s|/usr|/usr/foo| # |

Notice that they allow you to use forward slashes without escaping them.
Can be very handy.

> s is for substitute, and without it the g and b in previous tests are
> misinterpreted?

Yes. And no substitutions are done without the s.

/foo/s/aaa/bbb/

That would limit the substitutions to the first instance of aaa only on
lines with foo in them.

/foo/d

would delete any lines with foo in them

sed -n '/foo/p'

would print any line line with foo in it.

(the -n prevents the printing of the unwanted lines - otherwise you'd
get the whole file with the selected lines duplicated)

Sed was derived from ed, and this would be the equivalent
command using that editor:

g/foo/p

or

g/RE/p

with RE meaning 'regular expression'.

And there you have the origin of the grep utility.


Lee

--
BasicLinux: Small is Beautiful
http://www.basiclinux.com.ru





Archive powered by MHonArc 2.6.24.

Top of Page