2

I have file which has following format

----------------------------------------
  Name: cust foo
  mail: cust.foo@example.com
  Account Lock: FALSE
  Last Password Change: 20170721085748Z
----------------------------------------
  Name: cust xyz
  mail: cust.xyz@example.com
  Account Lock: TRUE
  Last Password Change: 20181210131249Z
----------------------------------------
  Name: cust bar
  mail: cust.bar@example.com
  Account Lock: FALSE
  Last Password Change: 20170412190854Z
----------------------------------------
  Name: cust abc
  mail: cust.abc@example.com
  Account Lock: FALSE
  Last Password Change: 20191030080405Z
----------------------------------------

I want to change Last Password Change data format to YYYY-MM-DD but not sure how to do that with sed or awk or is there any other method, I can try for loop it and use date -d option but not sure if there is easier way to do with regex

  • using date -d will be the most robust solution and likely the most simple solution as well – jesse_b Nov 1 '19 at 18:45
  • Down to just YYYY-MM-DD from YYYYMMDDHHmmSS? – Jeff Schaller Nov 1 '19 at 18:55
  • yess, that is correct format – Satish Nov 1 '19 at 18:57
8

Since all you're doing is adding two dashes, and dropping some extra characters, there's not much need for date.

$ sed -Ee 's/(Last Password Change: )(....)(..)(..).*Z/\1\2-\3-\4/' < foo.txt
...
  Name: cust foo
  mail: cust.foo@example.com
  Account Lock: FALSE
  Last Password Change: 2017-07-21
...

For a stricter pattern, the dots (that match any character) could be replaced with [0-9] to only match digits. \1 etc. in the replacement of course expand to what ever the patterns in parenthesis matched.

| improve this answer | |
  • Very very very cool..... that was clever – Satish Nov 1 '19 at 19:05
4

This is my proposal:

sed -E 's/([0-9]{4})([0-9]{2})([0-9]{2})[0-9]{6}Z/\1-\2-\3/' file

Output:

  Name: cust foo
  mail: cust.foo@example.com
  Account Lock: FALSE
  Last Password Change: 2017-07-21
----------------------------------------
  Name: cust xyz
  mail: cust.xyz@example.com
  Account Lock: TRUE
  Last Password Change: 2018-12-10
----------------------------------------
  Name: cust bar
  mail: cust.bar@example.com
  Account Lock: FALSE
  Last Password Change: 2017-04-12
----------------------------------------
  Name: cust abc
  mail: cust.abc@example.com
  Account Lock: FALSE
  Last Password Change: 2019-10-30
----------------------------------------
| improve this answer | |
  • Wow!!! that is also really good example – Satish Nov 1 '19 at 19:15
4

I was able to accomplish this with the following awk command:

awk -F\: '$1 ~ /Last Password Change/{ OFS=":"; $2 = substr($2, 1, 5)"-"substr($2, 6, 2)"-"substr($2, 8, 2) }1'

Using : as a field separator, if the first column matches Last Password Change we will then use substr to extract the year, month, and date and replace the second column with those values in the format you desire.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.