Search code examples
bashshellparsingtokenizecut

How do you parse a filename in bash?


I have a filename in a format like:

system-source-yyyymmdd.dat

I'd like to be able to parse out the different bits of the filename using the "-" as a delimiter.


Solution

  • You can use the cut command to get at each of the 3 'fields', e.g.:

    $ echo "system-source-yyyymmdd.dat" | cut -d'-' -f2
    source
    

    "-d" specifies the delimiter, "-f" specifies the number of the field you require