[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sheflug] command as argument for grep



> 
> > Or you may need to escape the > in sed:
> >
> > 	sed -e 's/.*href=// -e 's/\>.*//' < input > output
> >
> > Chris...
> >
> 
> I needed an extra ' after href=//
> 

My bad... missed that out (I did say untested after all ;) ).

> I'm presuming the -e bit just indicates "carry out the following" which
> appears to be a reg exp substitution of what I didn't want with nothing
> (//).  I'm assuming you can put as many -e's on a line as you want to.  I
> was trying to pull out what I wanted rather than binning what I didn't want
> and had got stuck.  Same result different method.  Cool thanks.

Yep ... -e "expression". You don't need it if you only have one
expression in a sed command, thus these are identical:

	sed 's/bob//' < in > out
	sed -e 's/bob//' < in > out

But using -e allows you to specify multiple expressions per line, so this:

	sed -e 's/bob//' -e 's/cat/dog/' < in > out

gives the same as the pipeline:

	sed 's/bob//' < in | sed 's/cat/dog/' > out

but with more efficiency as the shell doesn't need to start a second sed.

Btw, in my example, you can cut the thing down to a single expression using
backreferences, summat like this (again, untested):

	sed 's/.*href\(.*\)>.*/\1/' < in > out
or
	sed -e 's/.*href\(.*\)>.*/\1/' < in > out

(I may have got the escaping of the brackets wrong. If so, remove the leading
blackslash from them; same rule also applies for the right-chevron - you may
need to put one in).

The \1 in the replace string says "replace with the first group in the 
search string" where grouping is controlled by the brackets. You can have
up to 9 backreferences, allowing you to extract bits of strings as required :)

Chris...

-- 
\ Chris Johnson           \
 \ cej [at] nightwolf.org.uk    ~-----,   
  \ http://cej.nightwolf.org.uk/  ~-----------------------------------, 
   \ Redclaw chat - http://redclaw.org.uk - telnet redclaw.org.uk 2000 \____
___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.