# converting date format

**URL:** <https://community.unix.com/t/converting-date-format/197899>\
**Category:** Shell Programming and Scripting\
**Created:** [May 21, 2008, 10:21am UTC](https://community.unix.com/t/converting-date-format/197899 "2008-05-21T10:21:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohan705](https://community.unix.com/letter_avatar/mohan705/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohan705](https://community.unix.com/u/mohan705)\
**Post date:** [May 21, 2008, 10:21am UTC](https://community.unix.com/t/converting-date-format/197899/1 "2008-05-21T10:21:33Z")

</div>

Hi

Please anyone to help to write script to convert date to 'yyyy-mm-dd'(If column is date convert) because my file has large and lot of date colums.

If file is small ,using awk command to achive.

cat file1|awk 'BEGIN {FS=OFS='|'} {$1=substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2);{$2=substr($2,1,4)"-"substr($2,5,2)"-"substr($2,7,2);print $0}'

Thanks,  
MR

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [May 21, 2008, 7:20pm UTC](https://community.unix.com/t/converting-date-format/197899/2 "2008-05-21T19:20:43Z")

</div>

Please provide an example of part of your file.

---

<div class="post-metadata">

**Author:** ![mohan705](https://community.unix.com/letter_avatar/mohan705/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohan705](https://community.unix.com/u/mohan705)\
**Post date:** [May 21, 2008, 10:45pm UTC](https://community.unix.com/t/converting-date-format/197899/3 "2008-05-21T22:45:43Z")

</div>

Hi,

Sample data  
99990101|20080331|0.0000000|FBF7.375 12/09|0.0000000|20070809|20080908|0.0000000|12/09|.....

Thanks  
MR

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/5_5575768a8748004e209b776fc1b2916d.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [May 21, 2008, 11:17pm UTC](https://community.unix.com/t/converting-date-format/197899/4 "2008-05-21T23:17:54Z")

</div>

> [@mohan705](#):
>
> cat file1|awk 'BEGIN {FS=OFS='|'} {$1=substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2);{$2=substr($2,1,4)"-"substr($2,5,2)"-"substr($2,7,2);print $0}'

This should give you a boost.

```plaintext
awk 'BEGIN {FS=OFS="|"} {for (i=1;i<=NF;i++) if (length($i)==8) $i=substr($i,1,4)"-"substr($i,5,2)"-"substr($i,7,2);print }' file1

```

---

<div class="post-metadata">

**Author:** ![mohan705](https://community.unix.com/letter_avatar/mohan705/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohan705](https://community.unix.com/u/mohan705)\
**Post date:** [May 22, 2008, 1:09am UTC](https://community.unix.com/t/converting-date-format/197899/5 "2008-05-22T01:09:08Z")

</div>

Hi danmero,

Thanks ,This is what I am looking for.

Thanks,  
MR

---

<div class="post-metadata">

**Author:** ![mohan705](https://community.unix.com/letter_avatar/mohan705/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohan705](https://community.unix.com/u/mohan705)\
**Post date:** [May 22, 2008, 3:31am UTC](https://community.unix.com/t/converting-date-format/197899/6 "2008-05-22T03:31:01Z")

</div>

Hi danmero

How it can be handled If any othey colum that having length of 8 other than date columns ? (Is there any command to validate the date ).In that case it will take the columns data and convert to date format

Thanks,  
MR

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/5_5575768a8748004e209b776fc1b2916d.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [May 22, 2008, 7:57am UTC](https://community.unix.com/t/converting-date-format/197899/7 "2008-05-22T07:57:35Z")

</div>

Yes, you can validate the date if you know the date range, here is a basic example:

```nohighlight
awk 'BEGIN {FS=OFS="|"} {for (i=1;i<=NF;i++) if ($i ~ /[1-2][0-9][0-9][0-9][0-1][0-9][0-3][0-9]/) $i=substr($i,1,4)"-"substr($i,5,2)"-"substr($i,7,2);print }' file1

```

---

<div class="post-metadata">

**Author:** ![mohan705](https://community.unix.com/letter_avatar/mohan705/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohan705](https://community.unix.com/u/mohan705)\
**Post date:** [May 22, 2008, 11:10am UTC](https://community.unix.com/t/converting-date-format/197899/8 "2008-05-22T11:10:57Z")

</div>

Hi danmero,

Thanks for your prompt reply

Thanks ,  
MR
