Bash: How to Extract Date from String


You can use the following basic syntax to extract the date from a string in Bash:

echo $my_string | grep -Eo '[[:digit:]]{2}/[[:digit:]]{2}/[[:digit:]]{4}'

This particular example extracts the a date in the format of mm/dd/yyyy from the string variable named my_string.

Note the following in the syntax:

  • -E: The pattern should be interpreted as an extended regular expression.
  • -o: Show only the part of the matching string that matches the pattern
  • [:digit:]{n}: Only retrieve digits from the input with specific number

In this particular example we specified that the date had a pattern of mm/dd/yyyy in the string but you can specify another format if the date has a different format.

The following examples show how to use this syntax in several different scenarios.

Example 1: Extract Date from String with mm/dd/yyyy Format

Suppose that we have the following string:

  • date_created:03/15/2024

And suppose that we would like to extract only the date from the string to get the following values:

  • 03/15/2024

We can use the following syntax to do so:

my_string="date_created:03/15/2024"

echo $my_string | grep -Eo '[[:digit:]]{2}/[[:digit:]]{2}/[[:digit:]]{4}'

The following screenshot shows how to use this syntax in practice:

Bash extract date from string

Notice that we’re able to extract only 03/15/2024 from the string, which represents only the date.

Example 2: Extract Date from String with yyyy-mm-dd Format

Suppose that we have the following string:

  • date_created:2024-03-15:official

And suppose that we would like to extract only the date from the string to get the following values:

  • 2024-03-15

We can use the following syntax to do so:

my_string="date_created:2024-03-15:official"

echo $my_string | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'

The following screenshot shows how to use this syntax in practice:

Bash extract date in yyyy-mm-dd format

Notice that we’re able to extract only 2024-03-15 from the string, which represents only the date.

Example 3: Extract Date from String with yyyymmdd Format

Suppose that we have the following string:

  • date_created:20240315:official

And suppose that we would like to extract only the date from the string to get the following values:

  • 20240315

We can use the following syntax to do so:

my_string="date_created:20240315:official"

echo $my_string | grep -Eo '[[:digit:]]{8}'

The following screenshot shows how to use this syntax in practice:

Bash extract date from string in yyyymmdd format

Notice that we’re able to extract only 20240315 from the string, which represents only the date.

Note that we used [[:digit:]]{8} as the pattern to look for in this example since all 8 digits in the date occurred without any separators between them.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Extract Text Between Two Strings
Bash: How to Replace Dash with Underscore
Bash: How to Replace Multiple Characters in String
Bash: How to Replace All Occurrences of String in File

Leave a Reply