Extracting date & time from file name

Hi,

I am having a file name as exp_bkp_tables_18_Oct_2010_10_50_28.dmp which is used for import the records.

Now, I want to print the output using the selected file name as below :

Table records will get restored as on date 18-Oct-2010 and time 10:50:28

How it can be done ?

With Regards

Something like this ?

echo "exp_bkp_tables_18_Oct_2010_10_50_28.dmp" | awk -F"_" '{print "Table records will get restored as on date " $4"-"$5"-"$6 " and time " $7":"$8":" substr($9,1,(length($9)-4))}'
1 Like
echo "${filename%%.*}" | cut -d_ -f4- | ( IFS=_ ; read d m Y H M S )

echo "Table records will get restored as on date $d-$m-$Y and time $H:$M:$S"

Same but a bit optimized :

echo "${filename%%.*}" | ( IFS=_ ; read d d d d m Y H M S )
echo "Table records will get restored as on date $d-$m-$Y and time $H:$M:$S"

The below line may help...

echo "exp_bkp_tables_18_Oct_2010_10_50_28.dmp" | awk -F"_" '{ print  $4 "-"$5"-"$6" and time "$7":"$8":"$9}'

Hi,

Your help is highly appreciated. Thanks for the same.

One more query...regarding sort the files in descending order having name as below:

exp_bkp_tables_18_Oct_2010_10_50_28.dmp
exp_bkp_tables_18_Oct_2010_10_57_00.dmp
exp_bkp_tables_17_Oct_2010_10_58_00.dmp
exp_bkp_tables_17_Oct_2010_10_59_00.dmp

want to display the list of ten latest export copy in descending order

How it can be done ?

With Regards

latest on top

ls -t *.dmp | head

or
latest on bottom

ls -rt *.dmp | tail