Hi,
I tried to go through a lot of online material but could not find concrete solution.
My issues is like this :
I've got a input file like this :
<a>
<startDate>19700101000000</startDate>
<endDate>20300101000000</endDate>
</a>
<startDate>19700101000000</startDate>
<endDate>20300101000000</endDate>
<b>
<startDate>19700101000000</startDate>
<endDate>20300101000000</endDate>
</b>
<startDate>19700101000000</startDate>
<endDate>20300101000000</endDate>
<c>
<startDate>19700101000000</startDate>
<endDate>20300101000000</endDate>
</c>
<d>
<startDate>19700101000000</startDate>
<endDate>20300101000000</endDate>
</d>
Now I want to write a single sed command in such a way that I would give $i as input to sed statement to replace date value of ith occurance.
e.g. if i = 2 , then value of 19700101000000 within <stardate></startdate> within <b></b> is changed to 20200101000000. Similarly if i =1, then startdate within a,if i=3 then from within <c> is changed to 20200101000000.
I tried doing :
sed '/startDate/{s/19700101000000/20200101000000/;};H' "FILENAME.xml"
how to iterate it to loop through a to d and change values as mentioned above.
Please assist.
How does your actual file look like? Does it have a,b,c,d? And why a single sed?
--ahamed
No Actual file does not have tags like a,b,c...
Actual file has <PRE1>,<PRE2>,<PRE3>......
Also here i mean by single sed command is a one liner command. We can append as many -e,-n but want to complete it as a single liner.
Will the elements within the <PREn> tags remain the same? i.e. 2?
Will awk do?
--ahamed
No they will also not remain same. But will definitely contain <startdate> and <enddate> tags.
I know awk would be much easier to do, but can't we do it with sed ?
what do you mean not in sequential order?
--ahamed
Please see my updated reply.
So, you are not sure about the no of elements. You want to change the date for only certain ones you select by giving some number i.e. 1 for a etc like you mentioned in your previous post. But how will you know 1 is for this specific element?
--ahamed
We need not worry about specific element. As if input number is 1, it will search for first occurance of <startdate> and replace date within that tag. Similarly if input number is 2, it will search for 2nd occurance of <startdate> and replace date within second <startdate> tag.
Try this
awk '/startDate/{if(++x==i){sub(/>.*<\//,">"new"</")}}1' i=3 new=20200101000000 infile
--ahamed
Can you give me an idea on how we can achieve through sed ?
I am not sure if its really possible via sed. May be you should wait for an expert.
--ahamed
alright.
awk statement works like a charm.
Thanks a bunch for spending time and giving a faster solution.
Thanks again.