Reputation: 199
I write the dates on my file with the next format
date +%a-%b-%d-%Y
My goal in my exercise is to get the list of the dates in my file.
I know I need to do it with grep -E
, but I don't know how to put correctly the format of the date.
Desired input:
"grep -E (the format of the dates I'm looking for)" ~/file1
Desired output:
Tue-Oct-15-2013
Wen-Oct-16-2013
Wen-Oct-16-2013
Thu-Oct-17-2013
Upvotes: 4
Views: 14693
Reputation: 47099
If you want to be explicit about it and locale-aware, you could do it like this with sh
, date
, grep
and coreutils
:
days="($( for i in $(seq 7); do date -d 2013/01/$i +%a; done | paste -sd'|'))"
months="($(for i in $(seq 12); do date -d 2013/$i/01 +%b; done | paste -sd'|'))"
You can now grep the date format like this:
grep -E "${days}-${months}-[0-9]{1,2}-[0-9]{4}" infile
Note that you seem to be using a non-standard Wednesday abbreviation, if this is not a locale variation, you need to modify the days
line to this:
days="($(for i in $(seq 7); do date -d 2013/01/$i +%a; done | sed s/Wed/Wen/ | paste -sd'|'))"
Upvotes: 1
Reputation: 11703
Try following:
grep -E '[[:alpha:]]{3}-[[:alpha:]]{3}-[[:digit:]]{2}-[[:digit:]]{4}' ~/file1
Or more concise
grep -E '\w{3}-\w{3}-[0-9]{2}-[0-9]{4}' ~/file1
Upvotes: 6