wood burning stoves 2.0*
The moose likes Linux / UNIX and the fly likes SED script help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "SED script help needed" Watch "SED script help needed" New topic
Author

SED script help needed

Sanjay Singh
Ranch Hand

Joined: Mar 02, 2006
Posts: 37

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.


Regards, Sanjay Singh
SCJP-1.6, OCEWCD 6
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SED script help needed
 
Similar Threads
Implementing HttpSession in the Spring MVC
the value of <%@taglib uri="" %>?
access collection data in s:if
Passing values from child to parent form
parsing data and storing in the xml