2

I have a text file which contains several occurrences of dates. All the dates are written in the format Mon-DD-YYYY. I have to update all these dates in the current date keeping the format Mon-DD-YYYY.

The scripting language is bash. One big problem is that I have no idea how to match a specific format date with sed, awk, bash built-in functions, etc.

Could you help me, please?

2 Answers 2

3

Set current_date=$(date +"%m-%d-%Y") or to whatever date you want and then run

sed -i.bak "s/[0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\}/$current_date/g" your_file.txt 

With this, you can match any date in a file and replace it with current_date

The sed command in this case works like: sed "s/tobereplace/replacement/g". The g at the end stands for global, that means every occurrence of the tobereplace pattern will be replaced by replacement.

For tobereplaced the regex pattern [0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\} is used, which translates to

  • [0-9] any number from 0 to 9
  • \{2\} two times
  • hyphen
  • the same again and the same again but four times \{4\}

Edit: As FelixJN mentioned in the comment, you probably wanted the month as a string, for that you would use %b in the date command and replace the first part of the regex like this:

current_date=$(date "+%b-%d-%Y") sed -Ei "s/[A-Z][a-z]\{2\}-[0-9]\{2\}-[0-9]\{4\}/$current_date/g" your_file.txt 

In this case, the regex works like this:

  • [A-Z] Matches any uppercase letter from A to Z.
  • [a-z]\{2\} Matches any lowercase letter from a to z two times

The rest is the same as mentioned above

7
  • 1
    I think Mon in OP's request asks for using %b and [A-Z][a-z]\{2\} for the month. Commented Jun 30, 2023 at 9:32
  • 1
    @FelixJN with Mon-DD-YYYY I mean, for instance, Jan-14-2023 Commented Jun 30, 2023 at 10:11
  • 1
    @user9952796 I fixed my regex for you. If this answers your question, please mark is as the correct answer. Commented Jun 30, 2023 at 10:38
  • 3
    To reduce potential false matches, the regex might be /(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-[0-9]{2}-[0-9]{4}/ -- use sed -E to not have to escape the regex characters Commented Jun 30, 2023 at 13:09
  • 2
    I doubt any of this is the right approach. Today the file has several dates which should be replaced with current. Tomorrow, a new version of the file might mention some date in the same format which should not be replaced, but wrongly will be. Blindly replacing regexes absolutely everywhere in a file, which only match the item you are looking for without any context, is rarely a good idea. Commented Jun 30, 2023 at 17:30
1

Using Raku (formerly known as Perl_6)

If you're starting from a numeric (2-digit) month:

~$ raku -pe 's:g/ \d**2 \- \d**2 \- \d**4 /{Date.today.mm-dd-yyyy("-")}/;' file 

If you're starting from an alphabetic (3-letter) month:

~$ raku -pe 'BEGIN my %h = [Z=>] 1..12, <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec>; \ s:g/ <{%h.values}> \- \d**2 \- \d**4 /{ join "-", %h{Date.today.month}, Date.today.day, Date.today.year }/;' file 

Sample Input:

Delaware Dec-07-1787; Pennsylvania Dec-12-1787; New Jersey Dec-18-1787; Georgia Jan-02-1788; Connecticut Jan-09-1788; Massachusetts Feb-06-1788; Maryland Apr-28-1788; South Carolina May-23-1788; New Hampshire Jun-21-1788; Virginia Jun-25-1788; New York Jul-26-1788; North Carolina Nov-21-1789; Rhode Island May-29-1790; Vermont Mar-04-1791; Kentucky Jun-01-1792; Tennessee Jun-01-1796; Ohio Mar-01-1803; Louisiana Apr-30-1812; Indiana Dec-11-1816; Mississippi Dec-10-1817; Illinois Dec-03-1818; Alabama Dec-14-1819; Maine Mar-15-1820; Missouri Aug-10-1821; Arkansas Jun-15-1836; Michigan Jan-26-1837; Florida Mar-03-1845; Texas Dec-29-1845; Iowa Dec-28-1846; Wisconsin May-29-1848; California Sep-09-1850; Minnesota May-11-1858; Oregon Feb-14-1859; Kansas Jan-29-1861; West Virginia Jun-20-1863; Nevada Oct-31-1864; Nebraska Mar-01-1867; Colorado Aug-01-1876; North Dakota Nov-02-1889; South Dakota Nov-02-1889; Montana Nov-08-1889; Washington Nov-11-1889; Idaho Jul-03-1890; Wyoming Jul-10-1890; Utah Jan-04-1896; Oklahoma Nov-16-1907; New Mexico Jan-06-1912; Arizona Feb-14-1912; Alaska Jan-03-1959; Hawaii Aug-21-1959; 

Sample Output:

Delaware Jul-4-2023; Pennsylvania Jul-4-2023; New Jersey Jul-4-2023; Georgia Jul-4-2023; Connecticut Jul-4-2023; Massachusetts Jul-4-2023; Maryland Jul-4-2023; South Carolina Jul-4-2023; New Hampshire Jul-4-2023; Virginia Jul-4-2023; New York Jul-4-2023; North Carolina Jul-4-2023; Rhode Island Jul-4-2023; Vermont Jul-4-2023; Kentucky Jul-4-2023; Tennessee Jul-4-2023; Ohio Jul-4-2023; Louisiana Jul-4-2023; Indiana Jul-4-2023; Mississippi Jul-4-2023; Illinois Jul-4-2023; Alabama Jul-4-2023; Maine Jul-4-2023; Missouri Jul-4-2023; Arkansas Jul-4-2023; Michigan Jul-4-2023; Florida Jul-4-2023; Texas Jul-4-2023; Iowa Jul-4-2023; Wisconsin Jul-4-2023; California Jul-4-2023; Minnesota Jul-4-2023; Oregon Jul-4-2023; Kansas Jul-4-2023; West Virginia Jul-4-2023; Nevada Jul-4-2023; Nebraska Jul-4-2023; Colorado Jul-4-2023; North Dakota Jul-4-2023; South Dakota Jul-4-2023; Montana Jul-4-2023; Washington Jul-4-2023; Idaho Jul-4-2023; Wyoming Jul-4-2023; Utah Jul-4-2023; Oklahoma Jul-4-2023; New Mexico Jul-4-2023; Arizona Jul-4-2023; Alaska Jul-4-2023; Hawaii Jul-4-2023; 

The replacement half uses raku code in a {...} curly-brace wrapped codeblock to generate the replacement date. Note: in the recognition domain, the above code (second answer) uses <{%h.values}> to generate months. You could use <alpha>**3 instead, or even @GlennJackman's list of months (in the comments).


Shipley, Samuel. "list of U.S. states’ dates of admission to the union". Encyclopedia Britannica, 11 Feb. 2020, https://www.britannica.com/topic/list-of-U-S-states-by-date-of-admission-to-the-Union-2130026. Accessed 4 July 2023.

https://docs.raku.org/type/Date
https://docs.raku.org/routine/mm-dd-yyyy
https://raku.org

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.