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 AndyA 2009-04-10 11:25:44
The following script seems to do what you want:
#! /usr/bin/awk -f

BEGIN {
  inTag = 0
}
{ 
  if (inTag == 0) {
    position = match ($0,"<tag id=\"name\">")
    if (position == 0)
      print $0
    else {
      print substr($0,1,(position-1))
      inTag = 1
    } 
  } else {
    if ( substr($0,1,6) == "</tag>") {
      inTag = 0
      print substr($0,7)
    }
  }
}

Notes, warnings etc...
The first line will probably have to be changed/removed depending on your exact environment and how you invoke the script (e.g. remove the first line and then do awk -f <script_file> <xml file> should work)
There is a built in assumption that there are no white spaces at the start of the lines.
The closing tag must be at the start of a line.
On the check for the closing tag you need to make sure the length (currently 6) is consistent with the tag length you are looking for and that the print starts at that value plus 1.

All of these limitations are removable but assuming they are not going to cause issues why make the script more complex than it needs to be.
[ Reply ]
    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 AndyA2009-04-10 12:15:05
          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.)