Parsing a date from a string

Bash:
Parsing a date from a string

How to:

Bash itself is quite limited in direct date parsing capabilities, often relying on external tools like date and awk for more sophisticated manipulation. Here’s how you can parse a specific format and then use it with the date command to convert it or perform operations.

Example 1: Extract a date string and convert it to another format.

Suppose you have a date in the format yyyy-mm-dd and you want to convert it to dd-mm-yyyy.

original_date="2023-04-01"
formatted_date=$(date -d $original_date '+%d-%m-%Y')

echo $formatted_date

Sample Output:

01-04-2023

This uses the date command with the -d option to specify the input date string, and +%d-%m-%Y to format the output.

Example 2: Using awk to parse a date from a structured text line and convert it.

Assuming you have a log file line:

2023-04-01 12:00:00 User logged in

You can extract and convert the date part using awk and date.

log_line="2023-04-01 12:00:00 User logged in"
date_part=$(echo $log_line | awk '{print $1}')
formatted_date=$(date -d $date_part "+%A, %B %d, %Y")

echo $formatted_date

Sample Output:

Saturday, April 01, 2023

This example uses awk to split the log line and extract the date part ($1 represents the first space-delimited field), and then date is used to reformat it.

Using third-party tools

For more complex parsing or when dealing with a wide variety of date formats, third-party tools like dateutils can be very handy.

Example with dateutils:

Assuming you have a date string in a non-standard format, for instance, April 01, 2023.

original_date="April 01, 2023"
formatted_date=$(dateconv -i "%B %d, %Y" -f "%Y-%m-%d" <<< $original_date)

echo $formatted_date

Sample Output:

2023-04-01

This command uses dateconv from dateutils, specifying the input format with -i and the desired output format with -f. dateutils supports a vast range of date and time formats, making it very versatile for date parsing tasks in Bash scripts.