The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

sed help by imrambi2009-04-10 11:05:27
  Can I use awk rather than sed? by AndyA2009-04-10 11:25:44
    I can use gawk by imrambi2009-04-10 11:57:26
      Can you post the section of the actual file? by AndyA2009-04-10 12:01:44
        Slight refinement... by AndyA 2009-04-10 12:15:05
This removes the need for magic numbers in the code, just change the two strings in the BEGIN block.
It also copes the close tag not being the first item on that line.
BEGIN {
  inTag = 0
  tagToRemove = "<tag id=\"name\">"
  tagEndMarker = "</tag>"
}
{ 
  if (inTag == 0) {
    position = match ($0,tagToRemove)
    if (position == 0)
      print $0
    else {
      print substr($0,1,(position-1))
      inTag = 1
    } 
  } else {
    position = match ($0,tagEndMarker)
    if ( position != 0 ) {
      inTag = 0
      print substr($0,position + length(tagEndMarker))
    }
  }
}
-------------
My rest data is:
<tag1>
keep line 1
</tag1><tag id="name">
<removed line>
another removed line
</removed line>
</tag><tag2>
more lines
</tag2><tag id="asdf">
more stuff
</tag>
last line
and the output I get is:
<tag1>
keep line 1
</tag1>
<tag2>
more lines
</tag2><tag id="asdf">
more stuff
</tag>
last line
[ Reply ]
          Thanks! by imrambi2009-04-10 12:30:27

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)