LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Opposite of Cat >>

[ILUG] Opposite of Cat >>

Brian Foster blf at utvinternet.ie
Mon Jun 10 16:59:08 IST 2002


  | Date: Mon, 10 Jun 2002 14:22:18 +0100
  | From: Stephen Shirley <diamond at skynet.ie>
  | 
  | On Mon, Jun 10, 2002 at 12:57:06PM +0100, Brian Foster wrote:
  |  > this is trivial to un-install --- just delete all lines
  |  > between (and including) the two magic marker lines.
  |  > numerous tools can do this, sed(1) is perhaps a good choice.
  | 
  | I'm just wondering, could you give a snippet of sed code to demonstrate
  | this?  I've used sed for line editing lots of times, but anything more
  | complex, and the man page maketh no sense to me.  I've even tried looking
  | for example sed scripts on the sed homepage, but no joy.  Therefore, for
  | this sort of thing, i'd use sed, cause i know how -)

 b.t.w., I'm told O'Reilly's sed & awk book (or is it awk & sed?)
 is good ....  I cannot vouch for that book myself, as I learned
 sed (and awk) the hard way.

 the basic sed command in this case is simply:

    sed -e 'addr1,addr2 d' ...

 i.e., starting with the line identified by <addr1>, delete each
 line until (and including) the line identified by <addr2>.  for
 the solution proposed, addr1 is a bog-simple /^...$/ RE matching
 the begin magic marker, and addr2 is a similar RE for the end.
 below is a Bourne-ish example, tested with GNU sed 3.02, and
 which (AFAIK) will work with any version of any sed anywhere.

=====(cut here and below)=====(sed multi-line delete example)=====(bash script)=====
#!/bin/bash

    # the following two sh variables are not strictly necessary.
    # they are used only to keep the sed(1)-command readable.
    # $begin is an RE matching exactly the first magic marker line;
    # and $end is an RE matching exactly the second magic marker.
    #
readonly begin='^#BEGIN "example" !DO _NOT_ DELETE THIS LINE!$'
readonly   end='^#END   "example" !DO _NOT_ DELETE THIS LINE!$'

    # this is really just a one-liner sed/shell script:
    #
    #    sed -e '/Begin/,/End/d' ...
    #
    # which deletes all lines between each and every Begin-End
    # pair (inclusive).  if there is an End-less Begin, then
    # all lines from that End-less Begin to EOF are deleted!
    #
sed -e "/$begin/,/$end/d" <<\EOF
here are some lines
to be kept
#BEGIN "example" !DO _NOT_ DELETE THIS LINE!
these lines
are to be removed
#END   "example" !DO _NOT_ DELETE THIS LINE!
and these lines
at the end
should also be kept
EOF
=====(cut here and above)=====(sed multi-line delete example)=====(bash script)=====

cheers!
	-blf-
--
 Innovative, very experienced, Unix and      | Brian Foster    Dublin, Ireland
 Chorus (embedded RTOS) kernel internals     | e-mail: blf at utvinternet.ie
 expert looking for a new position ...       | mobile: (+353 or 0)86 854 9268
  For a résumé, contact me, or see my website  http://www.blf.utvinternet.ie

    Stop E$$o (ExxonMobile):  «Whatever you do, don't buy Esso --- they
     don't give a damn about global warming.»    http://www.stopesso.com
     Supported by Greenpeace, Friends of the Earth, and numerous others...




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell