How to find a word and move it a specific location in xml file using perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find a word and move it a specific location in xml file using perl?
# 1  
Old 03-07-2013
Computer How to find a word and move it a specific location in xml file using perl?

Hi friends,

I have one XML file having below structure :-
Code:
INput XML file :-
<?xml version="1.0" encoding="UTF-8"?>
<START> 
<A=value1>
<attr name1="a1">
</A>
<B=value2>
<attr name2="b1">
<attr name3="c1">
</B>
</START>

output xml file should be
===
Code:
<?xml version="1.0" encoding="UTF-8"?>
<START> 
<A=value1>
<attr name1="a1">
<attr name2="b1">
<attr name3="c1">
</A>
</START>

Can anybody please help me. I am having little knowledge of perl.
I just know how to open file and read it :-

Code:
open xmlsource, "$filename";
undef $/;#slurp
$myfile = <xmlsource>;
close xmlsource;
chomp $myfile;
print $myfile;


Last edited by harpal singh; 03-07-2013 at 11:18 AM..
# 2  
Old 03-07-2013
Must you update the file in place, or can we make a copy? Is the word moving to a equally legal place in the xml schema? Within a file, move upward is copy source word to memory, rewrite file between target and source sliding the data down, then write the source word. For a copy, you need to copy the earlier file part first and the later file part last. For downward, you need to buffer input whose file space is being overwritten into two buffers, write the word, the first buffer, reload the first buffer, write the second buffer, reload the second buffer, etc. through the middle. You might copy the word to move and the middle data into memory and then rewrite it all with the word moved, if it will fit. I'd feel safer making a new file. Disk is cheap.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 03-07-2013
Thanks a lot for replying DGPickett.
In my case i need to changed input xml file and then move it to other location.Here copy of input file is not allowed.
In input file <attr name1="a1"> is fixed always and <attr name2="b1"> and <attr name3="c1"> should come after <attr name1="a1"> only.
If i execute script on input.xml file then after everything is done ..all changes should reflect in same input.xml file.


Can we do something like open xml file then use find and replace and remove whatever is extra..But as i dont have idea about perl..i dont know how to do it...

Once again thanks for you time Smilie
# 4  
Old 03-07-2013
There are PERL tutorials on the web that are a fine use of your time, and I am told it is the most versatile tool of all, being midway between c/C++/JAVA and shell/sed/awk. Learning it is a big task for a newcomer to computers, but learning your first language and O/S language interface is a big task regardless.

A tool like sed or sed-like-perl can copy the file, capturing lines (sed h, d) it finds by context (sed /pattern/ and N) and moving them (sed G) to new places it finds by context. https://www.unix.com/man-page/All/1/sed/

However, the only really robust way to adjust an XML file is to use or create an XML-aware tool. PERL/C++/JAVA have XML parsers that will put the document in memory as a container object (DOM). Once there, you can adjust the content and write a new XML file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. Shell Programming and Scripting

Bash to move specific files from folders in find file

I have a directory /home/cmccabe/nfs/exportedReports that contains multiple folders in it. The find writes the name of each folder to out.txt. A new directory is then created in a new location /home/cmccabe/Desktop/NGS/API, named with the date. What I am trying to do, unsuccessfully at the moment,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Find and Copy file of specific location

Dear All, I need to transfer all files present in one location to another but those files should be of specific extension like. Find and copy all files of extension .xls, .pdf, .txt from location usr/tmp to location /per/Treat (6 Replies)
Discussion started by: yadavricky
6 Replies

4. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

5. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

6. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

7. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

8. Shell Programming and Scripting

Finding a word at specific location in a string

Hi All , I have different strings (SQL queries infact) of different lengths such as: 1. "SELECT XYZ FROM ABC WHERE ABC.DEF='123' " 2. "DELETE FROM ABC WHERE ABC.DEF='567'" 3. "SELECT * FROM ABC" I need to find out the word coming after the... (1 Reply)
Discussion started by: swapnil.nawale
1 Replies

9. Shell Programming and Scripting

how to Add word at specific location in line

eg . i have file x.txt contains : java coding , shell scriptting etc... now i want to add "is langauge" after word java. output should be java is langauge coding , shell scriptting etc... any idea how to use shell script to do it ? (10 Replies)
Discussion started by: crackthehit007
10 Replies

10. Shell Programming and Scripting

find pid of process run in specific location

Hello, I have a process a.out that runs from /a and /b How can I get the pid of the one running from /a ps -C /a/a.out does not work Thanks! (4 Replies)
Discussion started by: JCR
4 Replies
Login or Register to Ask a Question