Search code examples
linuxawkcsh

how to extract specific name from folder or file name


I'm new to linux command.

Now I would like to extract only date value from listed file name and compare as a date value.

Example:
/underdirectory

20080206
20080207
bk_20080208

I want to listed all above directories and compare date, means this directory is greater or smaller than which is according to specified date. If all of the listed directories will be date, it's ok for condition check.

code

foreach date_directory ( ls )
if ( "$date_directory" >= "$fdate" && "$date_directory" <= "$tdate") then
echo ${target_del}${date_directory} >> ${output}
endif
end

But if include some words such as "bk_20080228" or "bk_20080228_bk" or "20080228_bk" or "20080228_tt", there condition check will be error.

bk_20080208 want to take only 20080208.

please help me. Thanks!


Solution

  • use grep regexp grep -Eo '[0-9]{4}[0-9]{2}[0-9]{2}' or grep -Eo '[0-9]+'