0

I need to change date format in XML tags. I have written awk to find-replace date format in command line - echo '2012-01-13' | awk -v FS=- -v OFS=/ '{print $2,$3,$1}' . But not sure how to proceed with using this in XML..

XML used

<OrderNbr>136642</OrderNbr>
<CustomerName>MIKE</CustomerName>
<CustomerType>NEW</CustomerType>
<DateOfBirth>1986-09-03</DateOfBirth>
<LastUpdated>2012-03-28 00:01:02.133</LastUpdated>

Need to change DoB format across XML.

Expected Output:

<OrderNbr>136642</OrderNbr>
<CustomerName>MIKE</CustomerName>
<CustomerType>NEW</CustomerType>
<DateOfBirth>09/03/1986</DateOfBirth>
<LastUpdated>2012-03-28 00:01:02.133</LastUpdated>
1
  • 1
    Don't do that. If the input is valid XML (which your example is not), then use an XML-aware tool like xmlstarlet or xsltproc. Secondly, you should not convert a perfectly good date format into a stupid date format. Commented Oct 5, 2016 at 12:16

2 Answers 2

2

Here's one way to do, provided the <DateOfBirth> tag and date are on same line

$ cat ip.xml
<OrderNbr>136642</OrderNbr>
<CustomerName>MIKE</CustomerName>
<CustomerType>NEW</CustomerType>
<DateOfBirth>1986-09-03</DateOfBirth>
<LastUpdated>2012-03-28 00:01:02.133</LastUpdated>

$ sed -E '/<DateOfBirth>/ s|([0-9]{4})-([0-9]{2})-([0-9]{2})|\2/\3/\1|' ip.xml 
<OrderNbr>136642</OrderNbr>
<CustomerName>MIKE</CustomerName>
<CustomerType>NEW</CustomerType>
<DateOfBirth>09/03/1986</DateOfBirth>
<LastUpdated>2012-03-28 00:01:02.133</LastUpdated>
  • -E extended regex option
  • /<DateOfBirth>/ substitute only on lines matching <DateOfBirth>
  • ([0-9]{4})-([0-9]{2})-([0-9]{2}) extract date with numbers alone captured
  • \2/\3/\1 required output format

This might work if extended regex option is not available:

sed '/<DateOfBirth>/ s|\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)|\2/\3/\1|' ip.xml


Similar solution with perl

$ perl -pe 's|(\d{4})-(\d{2})-(\d{2})|$2/$3/$1| if /<DateOfBirth>/' ip.xml 
<OrderNbr>136642</OrderNbr>
<CustomerName>MIKE</CustomerName>
<CustomerType>NEW</CustomerType>
<DateOfBirth>09/03/1986</DateOfBirth>
<LastUpdated>2012-03-28 00:01:02.133</LastUpdated>
Sign up to request clarification or add additional context in comments.

5 Comments

I guess my bash is old. sed -E ain't working.. Thanks very much :)
@Siva, in that case, it is better to add which sed version you have and may be which OS too.. would a perl solution be okay? would be very similar to sed
@Siva, I've also added a solution without -E option, can you check if that works?
Thanks Sundeep, the second one worked. Thanks a ton :) Didn't try perl.
@Siva, glad that worked, don't forget to accept the answer which worked the best for you :)
1

Here is awk version: This will check for the string "DateOfBirth". If this string is seen then extract the date and then format it. use sub to replace date with modified date.

awk -F'<|>' '/DateOfBirth/{split($3,a,"-");sub($3, a[2]"/"a[3]"/"a[1])}1' xml
<OrderNbr>136642</OrderNbr>
<CustomerName>MIKE</CustomerName>
<CustomerType>NEW</CustomerType>
<DateOfBirth>09/03/1986</DateOfBirth>
<LastUpdated>2012-03-28 00:01:02.133</LastUpdated>

Note: Use some XML aware tool. warned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.