Section Removal With sed; and With a Twist . . .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Section Removal With sed; and With a Twist . . .
# 1  
Old 10-03-2014
Section Removal With sed; and With a Twist . . .

Hello folks!

Raised a bump on my head trying to figure this one out Smilie

I have an xml file which needs to be edited, removing an entire property section in the work. Here's what the target section layout looks like:
Code:
<property name="something">
{any number of lines go here}
</property>
</property>

Now, sed-deleting all from <property name="something"> to the first </property> tag is a fairly straightforward affair:
Code:
sed -i '/<property name="something">[[:space:]]*$/,/<\/property>[[:space:]]*$/d' /path_to/filename.xml

...but encompassing the SECOND </property> tag is not quite so straightforward; and I can't quite decipher where the tweak goes to make things turn...

A quick note, anyone?


Thanks again Smilie

Last edited by LinQ; 10-04-2014 at 12:19 PM.. Reason: Quote tags ====>>>> CODE tags
# 2  
Old 10-03-2014
Code:
awk '
/<property name="something">/ {f++; next}
f==0 {print; next}
/<property/ {f++}
/<\/property>/ {f--}
' /path_to/filename.xml

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 10-03-2014
Hello Linq,

Could you please use following, not tested though.
Lets say we have file as follows.
Code:
cat file420
ydudjccjxtdzhsfz`
<property name="something">
{any number of lines go here}
</property>
</property> 
abcdefg
jhdwygdebehb
wehkcgiwegceb

Following is the code may help.

Code:
awk 'NR==1 && $0 !~ /<property/ {B=-1} /<property/ {B=1}  {if(B <= 0 && $0 !~ /<\/property>/) {print $0}} /<\/property>/ {B--}' file420

Output will be as follows.

Code:
ydudjccjxtdzhsfz`
abcdefg
jhdwygdebehb
wehkcgiwegceb

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-03-2014 at 06:02 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-03-2014
Thanks, folks, for the awk help; but isn't there a way of doing this with sed?

(kinda like the OP ... trying to learn this stuff)

Smilie
# 5  
Old 10-03-2014
I won't say there is no way to do this with sed, but a stream text editor (i.e., sed) is not designed to do arithmetic any more than a calculator (such as bc or dc) is designed to do text processing. If you have a problem that does text processing and math, awk is usually a much better tool to consider if you can't do what you want entirely in the shell.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-03-2014
@Don Cragun:

Thanks for the heads up.

One last question for the road: What might the proper syntax be to simply allow the OP commandline to add one line to the region which is deleted? Been having a goose of a time with braces, semicolons, et al; all while trying to get a grip on the logic flow...

While not exactly a full solution (unlike the awk blocks presented by MadeInGermany and RavinderSingh13), it'd help broaden my syntax mastery just a wee bit Smilie

Thanks again --
# 7  
Old 10-03-2014
Code:
sed -e '/start/,/end/ a\Adding this line after the found block'

sed -e '/start/,/end/ c\Changing the found block for this line'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to identify particular section using sed?

Hi, I have following data in a file. Not all but most of the lines start with letter 'T' has 8 SPACES from column 121 to 128 and I want to replace that portion with some dummy value. Is it possible through sed? Input File:- T1111111111111A 20140310000000005076358416369283 AAAAA ... (6 Replies)
Discussion started by: jnrohit2k
6 Replies

2. Shell Programming and Scripting

sed - String substitution within specified section in ini type file

Hello. I am trying to modify a config file which is in windows *.ini type file. I have found a piece of code here :linux - Edit file in unix using SED - Stack Overflow As I can't make it doing the job , I am trying to find a solution step by step. here a modified sample file : my_sample.ini... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Removal of last-semicolons in line with sed

Hello, I'm trying to remove an arbitrary number of semicolons at the end of each line in the input file. Input: 44;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746;;; 45;I;1000031;;;B;0137;0;;01.02.2008;03.02.2009;;;;;;;;;;;;;0028-101746;;;;; ... (6 Replies)
Discussion started by: uioreanu
6 Replies

4. Shell Programming and Scripting

sed help with character removal

Hello I've got a string of text with a number in pence, e.g. 0.52p, I need to remove the 'p' so that it just reads 0.52 without of course removing all the other 'p' characters. Many thanks (1 Reply)
Discussion started by: mrpugster
1 Replies

5. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

6. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

7. Shell Programming and Scripting

Incrementing with a twist - please help

I'm currently trying to write a ksh or csh script that would change the name of a file found in directories and attach to the name an incrementing three digit number. I know how to write a script that will go: 000, 001, 002, 003, etc The twist is I need more increments then allowed by a 3... (11 Replies)
Discussion started by: Rust
11 Replies

8. Shell Programming and Scripting

Special Character SED/AWK removal

I have a script that produces an output containing '/.ssh'. I am trying to find a way of parsing only this data from a single line, without removing any other special characters contained within the output as a result of the parse. Any help would be appreciated (6 Replies)
Discussion started by: Raggedranger333
6 Replies

9. Shell Programming and Scripting

Using Sed to duplicate a section of a file....

hello all, I have a file like this: section 1 blah1 blah2 section 2 blah1 blah2 section 3 blah1 blah2 and I want to use sed to duplicate section 2, like this: section 1 blah1 blah2 section 2 blah1 blah2 section 2 blah1 (2 Replies)
Discussion started by: nick26
2 Replies

10. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies
Login or Register to Ask a Question