Editing a file and saving it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing a file and saving it
# 1  
Old 10-16-2012
Editing a file and saving it

HI

I have a file samp1.xml which has the following content

Code:
<ErrorMessage><ExceptionNumber>5117</ExceptionNumber><OriginalMessage><![CDATA[<?xml version="1.0" encoding="UTF-8"?> <EMAILS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation=".\SMTPSchema.xsd"> <EMAIL SOURCE="" DEST="" CC="" BCC="" SUBJECT="tesr" BODY="simply a data fix.
simply a data fix."/> </EMAILS>]]></OriginalMessage><ExceptionList><RecoverableException><File>/build/S610_P/src/DataFlowEngine/ImbComputeNode.cpp</File><Line>464</Line><Function>ImbComputeNode::evaluate</Function><Type>ComIbmComputeNode</Type><Name>WBI_EMAIL_XMLtoEMAIL#FCMComposite_1_6</Name><Label>

I want to edit the file and create anew file which contains only the below:

Code:
<?xml version="1.0" encoding="UTF-8"?> <EMAILS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation=".\SMTPSchema.xsd"> <EMAIL SOURCE="" DEST="" CC="" BCC="" SUBJECT="tesr" BODY="simply a data fix.
simply a data fix."/> </EMAILS>

On a nut shell i want to edit a file and create new file containing only the text between starting tag <?xml and ending tag </EMAILS>, can some one please help??

Last edited by Scott; 10-17-2012 at 12:55 PM.. Reason: Code tags
# 2  
Old 10-16-2012
Code:
perl -lne 'BEGIN{undef $/} while (/CDATA(.*?)<\/OriginalMessage>/sg){print $1}' samp1.xml

# 3  
Old 10-16-2012
this is using perl, can this be done using shell scripting...
# 4  
Old 10-16-2012
another (Kind of cheesy):
Code:
sed 's/</\n</g;s/\]/\n]/g;' samp1.xml | sed -n '/<?xml/,/<\/EMAILS>/p' | awk '{printf $0} END {print}'

# 5  
Old 10-17-2012
hi the above command works but i have an issue --- i am getting an extra ending tag if i run it....

Code:
<?xml version="1.0" encoding="UTF-8"?> <EMAILS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation=".\SMTPSchema.xsd"> <EMAIL SOURCE="" DEST="" CC="" BCC="" SUBJECT="tesr" BODY="simply a data fix.
simply a data fix."/> </EMAILS> </EMAILS>


Last edited by Scott; 10-17-2012 at 12:54 PM.. Reason: Code tags, please...
# 6  
Old 10-17-2012
Code:
sed 's/</\n</g;s/\]/\n]/g;' samp1.xml | sed -n '/<?xml/,/<\/EMAILS>/p' | awk '{printf $0} END {print ""}'

# 7  
Old 10-17-2012
Thank you very much for the help

one final question ----- if i have multiple messages in the file then i am getting all of the search returns in one paragraph...
How do i get each search in a new line??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Snoop - saving the file

Hi Guy's, I want to run a snoop on an interface and then save it to the local jumpbox that I am connected to the end device via. Can anyone help with the syntax please. Cheers (3 Replies)
Discussion started by: mutley2202
3 Replies

2. Programming

[Solved] Removing duplicates from the file and saving as new file

Dear All I have 200 data files and each files has many duplicates. I am looking for the automated awk script such that it checks and removes the duplicates from the each file and saving them as new files for all 200 files in the respective folder. For example my data looks like this.. ... (12 Replies)
Discussion started by: bala06
12 Replies

3. Solaris

Modifying the contents of a file and saving it

I have a file with the following content: -------------------- SQL> @DBmonitor_WMHA_SQL_script.sql; Tablespace Size (GB) Free (GB) % Free % Used ------------------------------ ---------- ---------- ---------- ---------- WMHAIT_IS ... (8 Replies)
Discussion started by: Avik Datta
8 Replies

4. UNIX for Dummies Questions & Answers

Help with saving file in desired format

Hi I have the following file in the unix named emp. ID,NAME,SAL,DEPT 101,aaa,2000,10 102,bbb,3000,20 103,ccc,4000,30 104,ddd,5000,40 105,aaa,2000,50 106,bbb,3000,60 107,ccc,4000,70 108,ddd,5000,10 109,aaa,2000,80 I need to save first 3 columns(ID, NAME, SAL) in another file with... (2 Replies)
Discussion started by: alok3141
2 Replies

5. Shell Programming and Scripting

saving output from bash into a file

I am ssh to many servers to get some information... however sometimes the server is unreacheable and i am getting an error. I want to save that output to a file but I am not able to do so... I want to be able to save output of bash into a file.. so when I run this command on a script ssh... (5 Replies)
Discussion started by: eponcedeleonc
5 Replies

6. UNIX for Dummies Questions & Answers

Editing & Saving using shell script

Hi, OS: Unix, linux, hp-unix (all unix flavours) Iam trying to change some string values, special characters or otherwise in a file. As of now, i go using vi <filename>, open the file and do the edit manually and save it. Is it possible to write a shell script so that i just run the script... (4 Replies)
Discussion started by: kenkanya
4 Replies

7. Shell Programming and Scripting

Retain file permissions when saving .sh file from internet [OS X]

Hello. I have written a bash script that I am sharing with an OS X community I am a member of. The purpose of the script is to execute a series of commands for members without them having to get involved with Terminal, as it can be daunting for those with no experience of it at all. I have renamed... (4 Replies)
Discussion started by: baza210
4 Replies

8. UNIX for Dummies Questions & Answers

Control saving file by type

Is there a way to control that under a specific directory only a file with certain extension is allowed to be saved, others are denied. For example, only *.txt extension is allowed to be saved in directory /abc, and files with extension .rtf is allowed in /xyz (I know that UX does not care... (4 Replies)
Discussion started by: saswerks
4 Replies

9. Programming

Saving a vector to a file

Another C++ question, please do notify me if this forum is C-only! :) I'm having a vector... vector<Person> x; ... with a class I've defined (Person). I have several elements in this vector and I would like to save it to a file (binary (?)). And later on open up this file to retrieve... (1 Reply)
Discussion started by: J.P
1 Replies

10. UNIX for Dummies Questions & Answers

Saving a redirected file

What command do I use in order to save a file in directory A/B/C to directory A/D/E. (1 Reply)
Discussion started by: JSP
1 Replies
Login or Register to Ask a Question