remove XML parent start and end tags in same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove XML parent start and end tags in same file
# 1  
Old 10-22-2010
remove XML parent start and end tags in same file

Hi All,

Requirement: remove start and end tag of parent element

<DummyLevel>
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
</DummyLevel>

I have to delete the first <dummylevel> and last </DummyLevel> tags from each xml file and save in the same file.

As each xml is in a seperate xml file i do not want to redirect the output to another file.

Please suggest me how can i acheive this.

Thanks in advance.
# 2  
Old 10-22-2010
Code:
$
$ # list all XML files in this directory
$ ls -1 *.xml
f1.xml
f2.xml
f3.xml
$
$ # show the content of all 3 XML files
$ cat f1.xml
<DummyLevel>
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
</DummyLevel>
$
$ cat f2.xml
<DummyLevel>
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
</DummyLevel>
$
$ cat f3.xml
<DummyLevel>
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
</DummyLevel>
$
$ # Run a Perl one-liner to perform inline edit in each XML file.
$ # Each file is backed up first.
$ perl -i.bak -pe 's/(<DummyLevel>|<\/DummyLevel>)\n//' *.xml
$
$ # show the content of each XML file after the operation
$ cat f1.xml
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
$
$ cat f2.xml
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
$
$ cat f3.xml
<level1> </level1>
<level2> </level2>
<level3> </level3>
<level4> </level4>
<level5> </level5>
<level6> </level7>
$
$

HTH,
tyler_durden
# 3  
Old 10-22-2010
tyler_durden ,Thanks for the response

This is the error that i am getting when i try to run the command that you mentioned

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE DummyLevel>|</ at -e line 1.
# 4  
Old 10-22-2010
If given is the exact content of your xml file then..
Code:
sed -e '1d' -e '$d' inputfile> file2; mv file2 inputfile

else if DummyLevel is not your first and last line then

Code:
sed '/DummyLevel/d' inputfile> file2; mv file2 inputfile


Last edited by michaelrozar17; 10-22-2010 at 11:00 AM.. Reason: tuned a bit..
# 5  
Old 10-22-2010
Quote:
Originally Posted by dstage2006
...This is the error that i am getting when i try to run the command that you mentioned

Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE DummyLevel>|</ at -e line 1.
As the error message states, you have an unmatched bracket "(" in your regex.

tyler_durden
# 6  
Old 10-22-2010
tyler_durden,

perl -i.bak -pe 's/(<DummyLevel>|<\/DummyLevel>)\n//' one.xml

command ran but it is not getting any results. it is just preparing a .bak file with same file content and the original file haven't modified with any result.

Thanks for your suggestion.
# 7  
Old 10-22-2010
Quote:
Originally Posted by dstage2006
...
perl -i.bak -pe 's/(<DummyLevel>|<\/DummyLevel>)\n//' one.xml

command ran but it is not getting any results. it is just preparing a .bak file with same file content and the original file haven't modified with any result.
....
That's because the regex pattern didn't match anything in your file. So it didn't modify anything.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a String between start and end of a block in a file

Hi, I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file. Example give below, we need to search between the start block abc and end block def in a file, after that... (5 Replies)
Discussion started by: G.K.K
5 Replies

2. Shell Programming and Scripting

Split a file by start and end row.

I have a file which looks something as following, I would like to split to several files, The start and end of each file is 'FILE' and end with 'ASCII... ' . At the same time for each file in the first column add 100 and also second column add 100 the rest of the column as it is , see example of... (2 Replies)
Discussion started by: tk2000
2 Replies

3. Shell Programming and Scripting

How to extract start/end times from log file to CSV file?

Hi, I have a log file (log.txt) that which contains lines of date/time. I need to create a script to extract a CSV file (out.csv) that gets all the sequential times (with only 1 minute difference) together by stating the start time and end time of this period. Sample log file (log.txt) ... (7 Replies)
Discussion started by: Mr.Zizo
7 Replies

4. Shell Programming and Scripting

Grep start and end line of each segments in a file

Cat file1 -------- ---------- SCHEMA.TABLE1 insert------- update----- ------------- ---------- SCHEMA.TABLE2 insert------- update----- ----------- ------------ SCHEMA.TABLE3 insert------- update----- ------------ grep -n SCHEMA > header_file2.txt (2 Replies)
Discussion started by: Veera_V
2 Replies

5. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

6. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

7. Shell Programming and Scripting

remove some XML tags

Hi all, I have a file which i have to remove some line from it, the lines that i have to remove from my file is as below: </new_name></w"s" langue="Fr-fr" version="1.0" encoding="UTF-8" ?> <New_name> and it is finding at the middle of my file, is there any command line in linux to do it or do... (1 Reply)
Discussion started by: id_2pc
1 Replies

8. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

9. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

10. Shell Programming and Scripting

Remove unwanted XML Tags

I have set of sources and the respective resolution. Please advice how to resolve the same using Unix shell scripting. Source 1: ======= <ext:ContactInfo xmlns:ext="urn:AOL.FLOWS.Extensions"> <ext:InternetEmailAddress>AOL@AOL.COM</ext:InternetEmailAddress> </ext:ContactInfo> Resoultion... (1 Reply)
Discussion started by: ambals123
1 Replies
Login or Register to Ask a Question