• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

SED script help needed

 
Ranch Hand
Posts: 37
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I want to convert the following tag using SED command-

<PARTDESC>CIRCUIT BREAKER LIGHTING/EXT LT/NAV2/
&
LOGO</PARTDESC>

into something like-

<PARTDESC>CIRCUIT BREAKER LIGHTING/EXT LT/NAV2/&LOGO
</PARTDESC>

So that when I fetch the PARTDESC node value it should return me one line value with '&' replaced.

Any help in this will be appreciated.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjay,

Originally posted by Sanjay Singh:
Hi All,
I want to convert the following tag using SED command-

<PARTDESC>CIRCUIT BREAKER LIGHTING/EXT LT/NAV2/
&
LOGO</PARTDESC>

into something like-

<PARTDESC>CIRCUIT BREAKER LIGHTING/EXT LT/NAV2/&LOGO
</PARTDESC>

So that when I fetch the PARTDESC node value it should return me one line value with '&' replaced.

Any help in this will be appreciated.



I can help, but I won't have access to a Unix/Linux machine to test this. As such, we can take this discussion off line if you need to be my eyes and fingers to get through it.

So, it sounds like you want to append lines to the <PARTDESC> tag until the closing tag is encountered. Simple enough... I'm going to be using the pattern space and hold space to do this, so a combination of the sed commands g,G,h,H,x, etc. will be used. A point to take note of is when appending a line to the hold space, the CR/NL needs to be removed. I have a trick for that too.

As a quick review, the pattern space is a buffer that sed uses to read a line of input. The hold space is a buffer that is for the user. The pattern space is where string parsing is done. The hold space is just a buffer and cannot be directly modified.

Ok, here we go.
First, we'll clear the hold space when the first line of input is read. This is probably not necessary, but carried on by me when you're supposed to clear a memory buffer after allocating it.

Next, we'll start reading the input lines until the opening tag is found. We'll copy the pattern space to the hold space. We'll leave the CR/NL that also goes into the hold space for now.

We'll continue reading lines until the closing tag is found. While doing this, we'll swap the pattern space and the hold space to remove the CR/NL from the appended string we're creating. Then, we'll put the pattern space back to the hold space to do the same thing for the next line.

When the closing tag is found, output the hold space, then output the closing tag. Note that this assumes the opening and closing tags start at the beginning of a line and the closing tag is on a line by itself.
It also assumes that there is a closing tag for every opening tag. It's possible to handle these situations if not, but I've got other things to do today . Repeat these steps from the beginning by initializing the hold space and start looking for the next opening tag (or EOF).

Now the sed script:



Give that a shot and let me know how it goes. Sorry, I can't test it, but if you understand sed basics, you should be able to spot syntax and typo issues.

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic