Need help on shell script (sed)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on shell script (sed)
# 1  
Old 12-27-2013
Display Need help on shell script (sed)

Hi Guys,

I have a xml file as below where I need to find the particular XML tag(<dpath>) and delete the content of it.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<title>XML</title><head>Ram</head><ControlCenter><doStatus>1</doStatus></ControlCenter><exitlabel>Gzip thefile</exitlabel><dpath>/internal/SAP</dpath><dqm>SAPNODE</dqm>
<username>Vinoth</username>

I tried using sed but the o/p is not correct:

Command tried: sed ' /<dpath>/,/<\/dpath>/d' filename

But it gives the below o/p:

Code:
<?xml version="1.0" encoding="UTF-8"?>

Output expected:

Code:
<title>XML</title><head>Ram</head><ControlCenter><doStatus>1</doStatus></ControlCenter>
<exitlabel>Gzip the file</exitlabel>dqm>SAPNODE</dqm>
<username>Vinoth</username>

Note: XMLStarlet is not in my platform. So please provide some other option to fix this.


Your help is much appreciated!!!

Last edited by Don Cragun; 12-27-2013 at 03:06 AM.. Reason: Add CODE & ICODE tags.
# 2  
Old 12-27-2013
If both <dpath> and </dpath> are always on the same line, there is never more than one set of these tags on a single line, and the length of your longest input lines is no more than LINE_MAX bytes, the following should work:
Code:
sed 's/<dpath>.*<\/dpath>//' filename

The value of LINE_MAX on your system can be found using the command:
getconf LINE_MAX
# 3  
Old 12-27-2013
Hi Don,

Thanks a lot!!! It works. 1 more help how can I find and delete for multiple xml files in a folder?
# 4  
Old 12-27-2013
do the same in a loop.. for.. or while...

Code:
ls <YOUR DIR>/*.xml | while read file_name
do
sed 's/<dpath>.*<\/dpath>//' $file_name > $file_name.mod
done

# 5  
Old 12-27-2013
Hello Vinoth Kumar G,

Here is the code which will allow us to find files with name xml.
It will delete the files forcefully so please don't use it in directly Production/Live environment. Kindly test it and then try it.

Code:
find . -name "*.xml" -exec rm -rf {} \;


Kindly let us know if you need any more assistance.


Thanks,
R. Singh

---------- Post updated at 04:34 AM ---------- Previous update was at 04:17 AM ----------

Quote:
Hi Guys,

I have a xml file as below where I need to find the particular XML tag(<dpath>) and delete the content of it.


Code:
<?xml version="1.0" encoding="UTF-8"?><title>XML</title><head>Ram</head><ControlCenter><doStatus>1</doStatus></ControlCenter><exitlabel>Gzip thefile</exitlabel><dpath>/internal/SAP</dpath><dqm>SAPNODE</dqm><username>Vinoth</username>

I tried using sed but the o/p is not correct:

Command tried: sed ' /<dpath>/,/<\/dpath>/d' filename

But it gives the below o/p:



Code:
<?xml version="1.0" encoding="UTF-8"?>

Output expected:



Code:
<title>XML</title><head>Ram</head><ControlCenter><doStatus>1</doStatus></ControlCenter><exitlabel>Gzip the file</exitlabel>dqm>SAPNODE</dqm><username>Vinoth</username>

Note: XMLStarlet is not in my platform. So please provide some other option to fix this.


Your help is much appreciated!!!
Hello Vinoth,

As per your requirement here is a awk solution, it may help.


Code:
 awk -vs="<dpath>.*<\/dpath>" -vs1="<exitlabel>Gzip thefile</exitlabel>" 'NR>1 {gsub(s,X); gsub(s1,ORS s1); print}' file_name

Output will be as follows.



Code:
<title>XML</title><head>Ram</head><ControlCenter><doStatus>1</doStatus></ControlCenter>
<exitlabel>Gzip thefile</exitlabel><dqm>SAPNODE</dqm>
<username>Vinoth</username>


Thanks,
R. Singh
# 6  
Old 12-27-2013
Quote:
Originally Posted by RavinderSingh13
Hello Vinoth Kumar G,

Here is the code which will allow us to find files with name xml.
It will delete the files forcefully so please don't use it in directly Production/Live environment. Kindly test it and then try it.

Code:
find . -name "*.xml" -exec rm -rf {} \;

Kindly let us know if you need any more assistance.


Thanks,
R. Singh
@ Ravinder :

He wants to delete tag and value within tag using sed for multiple xml files in folder I think, not to delete xml file.

@ Vinoth : Don't use above one, it will delete all your xml files


You can use for like below
Code:
for file in directory/*.xml; do
     sed 's/<dpath>.*<\/dpath>//' $file >"Output_"$file
done


Last edited by Akshay Hegde; 12-27-2013 at 05:05 AM.. Reason: To report solution might be harmful
# 7  
Old 12-27-2013
Thanks a lot for your support!!

I used as below and it works as expected:

ls RequestTemplate_$1/*.xml | while read line
do
sed 's/<dpath>.*<\/dpath>//' $line > $line.mod
mv $line.mod $line
done

---------- Post updated at 06:46 AM ---------- Previous update was at 06:33 AM ----------

Hi Guys,

I used sed to trail out blank spaces in xml files. Since I have huge no of files sed takes lot of time. Is there any other command which can do the same faster?

command used: sed '/^\s*$/d' filename
sed '/^$/d' filename

Thanks in Advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Asst: Shell Script to sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Generalize your sub2 script, producing a new script sub3 that will apply a substitution to any number of files... (16 Replies)
Discussion started by: raymondbn
16 Replies

2. Programming

Shell script using sed or awk

Hi, I want to read a file from the command line and remove all the spaces, tabs in it, replacing it with comma(,), and write it to a new file. can you help me out with this scenario. (1 Reply)
Discussion started by: sudhanshu12788
1 Replies

3. Shell Programming and Scripting

SED shell script

Generate a ddl file, we have a flat file which contains many commands like REM, CREATE table, TRuncate etc This script should copy only create table statements to the ddl file. Create stmts can be in a single or multiple lines. ex:- INPUT FILE create table tbl1 (fld1,... (5 Replies)
Discussion started by: learninfo
5 Replies

4. Shell Programming and Scripting

using sed in shell script

Hi, i want to replace sub text which is in the middle of long text. let me be more clear with an example. Here is the actual text in the xml file <module-option name="principalDNSuffix">,cn=Users,dc=X,dc=Y</module-option> Now, in the above text, i want to replace all the content lying... (3 Replies)
Discussion started by: sunrexstar
3 Replies

5. UNIX for Dummies Questions & Answers

Problems in shell script if sed is used

Hi All, Below is the script which i have written in cygwin: #!/usr/bin/sh fname=$1 cat $fname | sed 's/ //g' > fname1 for i in `cat $fname1` do echo $i > file1 #param1 is script name param1=`awk -F , '{print $1}' file1` param1="$param1.sql" #param2 is BL param2=`awk -F , '{print... (5 Replies)
Discussion started by: janardhanamk
5 Replies

6. Shell Programming and Scripting

using sed in shell script

Hi all, I have files with the following names; afgcxa.pem4jan.rain.nc afgcxa.pem4feb.rain.nc afgcxa.pem4mar.rain.nc afgcxa.pem4apr.rain.nc I want to rename them to afgcxa.pem4-01.jan.rain.nc afgcxa.pem4-02.feb.rain.nc afgcxa.pem4-03.mar.rain.nc ... (5 Replies)
Discussion started by: Muhammad Rahiz
5 Replies

7. Shell Programming and Scripting

Help with shell script and sed!

Hi folks, maybe somebody here can help me. I have 2 files. File A and File B. File A contains URLs leading to files on the Internet, with one URL per line; The format of the URLs on each line in File A is . File B contains only the filenames from each line in File A; One filename per line. ... (11 Replies)
Discussion started by: o0110o
11 Replies

8. UNIX for Dummies Questions & Answers

shell script - sed

Hi i have a problem how to put som lines on top or in the bottom of the text with sed. problem is that i am reading from stdin and when i have som lines starting with ++, i have to put some characters on the top of the lines starting with that ++ example of input oooo ++abcd ++befg... (1 Reply)
Discussion started by: Dun
1 Replies

9. Shell Programming and Scripting

shell script by sed

Hey everybody I need some help on how to order the data in file such as a file have first name last name and city and i would like to order them to in the same order by using sed thanks alot for your time (6 Replies)
Discussion started by: halola85
6 Replies

10. Shell Programming and Scripting

Sed command in shell script

I have a current code working(named subst1) having a user be able to type this line to substitute words using the sed command: subst1 old-pattern new-pattern filename Here is my shell script: #!/bin/bash # subst1 ARGS=3 E_BADARGS=65 if then echo "Usage: `basename $0`... (1 Reply)
Discussion started by: Todd88
1 Replies
Login or Register to Ask a Question