dengar81
dengar81

Reputation: 2525

Extract date from filename using bash script

I know that similar things have been asked before, but I haven't been able to really make hand and foot out of what's been posted.

I've got a whole bunch of files that contain the date in the format YYYYMMDD at some point in the filename. Luckily this is the only 8 digit substring in all the filenames!

I will need to write the dates into another file later, but that should be fine. I'm struggling to extract the date into a variable first...

I know I can get it with grep:

for d in $( ls *.csv | grep -Po "\d{8}"; do 
echo $d done

However, as I want to get the full filename into a variable too while I iterate through them, that's not an option right now.

I've tried using sed, but I don't think I know how to use it:

for f in $( ls *.csv ); do
    d=$( $f | sed -e 's/^.*\(\d{8}\).*$')
    echo $d
done

Thanks for pointing me in the right direction!

Upvotes: 0

Views: 4677

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295278

#!/bin/bash
#      ^-- important: bash, not not /bin/sh

for f in *.csv; do                       # Don't use ls for iterating over filenames
  [[ $f =~ [[:digit:]]{8} ]] && {        # native built-in regex matching
    number=${BASH_REMATCH[0]}            # ...refer to the matched content...
    echo "Found $number in filename $f"  # ...and emit output.
  }
done

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74596

Loop through your csv files like this (don't parse ls):

for f in *.csv; do
    echo "$f"
    d=$(echo "$f" | grep -oE '[0-9]{8}')
done

I've used grep in extended mode (-E) but perl mode is equally valid.

As you have tagged with , you can do d=$(grep -oE '[0-9]{8}' <<<"$f" instead if you prefer. You can also use built-in regular expression support, which is slightly more verbose but saves calling an external tool:

re='[0-9]{8}'
[[ $f =~ $re ]] && d="${BASH_REMATCH[0]}"

The array BASH_REMATCH contains the matches to the regular expression. If there is a match, we assign it to d.

Upvotes: 2

Related Questions