LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 07-20-2009, 08:52 PM   #1
luananeko
LQ Newbie
 
Registered: Jul 2009
Posts: 1

Rep: Reputation: 0
Question How to delete lines and insert another line in some xml files using a shell script?


So here's the situation... I have a shell script that makes several wget calls to various webservices to generate and save the results to xml files, which are then transferred to another server for a different system to consume. The problem is, the webservices return a different xml format than the target system accepts. So I'm trying to modify my shell script to do the following two things to each of my xml files (all located in the same directory):
1. Remove the <response> and </response> lines.
2. Add the following line: as the line immediately following the xml version definition line (<?xml version="1.0" encoding="UTF-8"?>):
<!DOCTYPE RECORDS SYSTEM "records.dtd">

I've been told that sed is the way to do this, but the man pages for it don't make any sense to me. All the online tutorials seem to be written with a linux user in mind, not a java-programmer-attempting-to-use-linux user. Anyone know how to do this? Bonus points if you know a way that doesnt make me save the sed results to a different file and then rename it back to the name of the original file. Thanks!
 
Old 07-20-2009, 10:53 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Hi,

Welcome to LQ!

Quote:
1. Remove the <response> and </response> lines.
If these are on a line by themselves (w/o a payload)...
sed '/<[\/]*response>/d' file


Quote:
2. Add the following line: as the line immediately following the xml version definition line (<?xml version="1.0" encoding="UTF-8"?>):
<!DOCTYPE RECORDS SYSTEM "records.dtd">
For both steps together w/o a temporary file:
Code:
$ cat huhu.xml 
<?xml version="1.0" encoding="UTF-8"?>
<markup>
  <response>
    some content here
  </response>
  <othertag>
    yaddayadda
  </othertag>
</markup>

$ cat sedml                                                                                                                  
/<?xml version="1.0" encoding="UTF-8"?>/ i\
<!DOCTYPE RECORDS SYSTEM "records.dtd">
/<[\/]*response/d

$ sed -i.bak -f sedml huhu.xml                                                                                                      

$ cat huhu.xml                                                                                                               
<!DOCTYPE RECORDS SYSTEM "records.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<markup>
    some content here
  <othertag>
    yaddayadda
  </othertag>
</markup>


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SHELL Script to insert a line. santhoshv Programming 9 04-03-2008 07:10 AM
SHELL Script to insert lines after certain Intervals rahulruns Linux - General 9 02-26-2008 02:21 AM
Unable to delete the lines in xml FILES ragavendran31 Linux - Newbie 1 01-19-2008 02:24 PM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 08:48 AM


All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration