0

I am working on shell scripting and automation,i want to fulfil cetain tasks without writing more code.

below is sample xml of web service, i want to hit below request (Data.xml) with changing startdate in each segment.

mean in first invocation, only startdate of First tag will be modified and remaining startdate will be unchanged and so on.

my approach was removing all newlines and form entire xml in one line, then replace desired occurrence using sed.

i tried using below command

cat Data.xml | awk '{ print $1}'| xargs  echo | sed 's/<startdate>01011970<\/startdate>/<startdate>01011979<\/startdate>/2' | sed 's/> </>\n</g'

Output:

<Data>
<First>
<Name>Micheal</Name>
<Account-Validation>
<startdate>01011970</startdate>
<enddate>01019999</enddate>
</Account-Validation>
</First>
<Second>
<Name>Adam</Name>
<Account-Validation>
<startdate>01011979</startdate>
<enddate>01019999</enddate>
</Account-Validation>
</Second>
<Third>
<Name>Raul</Name>
<Account-Validation>
<startdate>01011970</startdate>
<enddate>01019999</enddate>
</Account-Validation>
</Third>
</Data>

the only issue which i see is it changes entire xml format. so i am looking for doing the same using hold buffer and pattern buffer approach. here is sample command,

cat Data.xml | sed  '{/<Second>/,/<\/Second>/g;s/<startdate>01011970<\/startdate>/<startdate>01011979<\/startdate>/p};h;

but above doesn't work, so can any one help me to do this using hold buffer and pattern buffer approach. the advantage i see using this approach is you don't need to count no of occurrence, you can easily provide tag name only in which startdate to be replace.

Data.xml

<Data>
        <First>
                        <Name>Micheal</Name>
                        <Account-Validation>
                                <startdate>01011970</startdate>
                                <enddate>01019999</enddate>
                        </Account-Validation>
        </First>
        <Second>
                        <Name>Adam</Name>
                        <Account-Validation>
                                <startdate>01011970</startdate>
                                <enddate>01019999</enddate>
                        </Account-Validation>
        </Second>
        <Third>
                        <Name>Raul</Name>
                        <Account-Validation>
                                <startdate>01011970</startdate>
                                <enddate>01019999</enddate>
                        </Account-Validation>
        </Third>
</Data>

Thanks, Priyank Shah

4

1 回答 1

1

该命令不起作用的原因是:您正确选择了地址空间,但什么也不做。's' 命令必须在地址空间内。

例子:

sed '/<Second>/,/<\/Second>/{s/<startdate>01011970<\/startdate>/<startdate>01011979<\/startdate>/}' Data.xml

{}当您只执行一个命令时,大括号是可选的。

于 2013-04-30T08:05:55.900 回答