# Validate date file format using awk

**URL:** <https://community.unix.com/t/validate-date-file-format-using-awk/340977>\
**Category:** Shell Programming and Scripting\
**Created:** [March 24, 2014, 1:23am UTC](https://community.unix.com/t/validate-date-file-format-using-awk/340977 "2014-03-24T01:23:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krish2014](https://community.unix.com/letter_avatar/krish2014/32/6_449c533f586939b6e5f246632a46421f.png) [@krish2014](https://community.unix.com/u/krish2014)\
**Post date:** [March 24, 2014, 1:23am UTC](https://community.unix.com/t/validate-date-file-format-using-awk/340977/1 "2014-03-24T01:23:44Z")

</div>

Here is my sample data  
Test.txt

```nohighlight
column 1|columne 2|start Date|end Date
test|test|03/24/2014|03/24/2014
test|test|03/24-2014|03/24/2014
test|test|03/24/2014|03/24/2014
test|test|03/24/2014|03/24-2014
test|test|03/24/2014|03/24/2014

```

Now in the file i am expecting the date fields should be `mm/dd/yyyy` format.  
How i can validate without open the file data row number 2 and 4 having wrong date format and print the data rows.

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [March 24, 2014, 1:43am UTC](https://community.unix.com/t/validate-date-file-format-using-awk/340977/2 "2014-03-24T01:43:36Z")

</div>

Hello,

Kindly use the coede tage as per forum rules for any command or code please.  
Following may help you for same.

```nohighlight
awk -F"\|" 'BEGIN {print "Showing lines which have issues."} {a=match($3,/..\/..\/.....*/); substr($3,RSTART,RLENGTH)} {if(a==0) {print $0}} {b=match($4,/..\/..\/.....*/); substr($4,RSTART,RLENGTH)} {if(b==0) {print $0}}' check_date_format1211134

```

Output will be as follows.

```nohighlight
test|test|03/24-2014|03/24/2014
test|test|03/24/2014|03/24-2014

```

NOTE: Where check\_date\_format1211134 is the input file.

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [March 24, 2014, 2:59am UTC](https://community.unix.com/t/validate-date-file-format-using-awk/340977/3 "2014-03-24T02:59:11Z")

</div>

```nohighlight
awk -F'|' -v OFS='|' ' $3 !~ "^(0[1-9]|1[12])/(0[1-9]|[12][0-9]|3[01])/[0-9]{4}$" || $4 !~ "^(0[1-9]|1[12])/(0[1-9]|[12][0-9]|3[01])/[0-9]{4}$" ' file 

```

---

<div class="post-metadata">

**Author:** ![krish2014](https://community.unix.com/letter_avatar/krish2014/32/6_449c533f586939b6e5f246632a46421f.png) [@krish2014](https://community.unix.com/u/krish2014)\
**Post date:** [March 24, 2014, 3:15am UTC](https://community.unix.com/t/validate-date-file-format-using-awk/340977/4 "2014-03-24T03:15:32Z")

</div>

Thanks RavinderSingh13 and anbu23

---

<div class="post-metadata">

**Author:** ![pravin27](https://community.unix.com/letter_avatar/pravin27/32/6_449c533f586939b6e5f246632a46421f.png) [@pravin27](https://community.unix.com/u/pravin27)\
**Post date:** [March 24, 2014, 6:06am UTC](https://community.unix.com/t/validate-date-file-format-using-awk/340977/5 "2014-03-24T06:06:18Z")

</div>

```nohighlight
awk -F "|" '{match($3FS$4,/..\/..\/....\|..\/..\/..../);printf RSTART==0?$0"\n":x}' filename

```
