Getting the date format for grep -E in Unix
12,628
Solution 1
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
Solution 2
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'|'))"
Related videos on Youtube
Author by
Dor Zohar
Updated on September 19, 2022Comments
-
Dor Zohar over 1 year
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
-
fedorqui over 10 yearsIt is not very clear what you mean. Can you show some input and desired output?
-