RegEx交换日期

时间:2018-06-24 09:27:53

标签: regex bash

我有许多文件的名称中包含日期,格式为:

filename - day month year (e.g. myfile - 22 August 2017)

我想做的是采用以下格式:

filename - yearmonthday (e.g. myfile - 20170822)

寻找一种在bash中执行此操作的方法。

非常感谢-科林

3 个答案:

答案 0 :(得分:1)

使用bash参数扩展和(GNU?)日期

$ f="myfile - 22 August 2017"
$ d=${f##* - }; echo "$d"
22 August 2017
$ new_d=$(date -d "$d" "+%Y%m%d")
$ echo mv "$f" "${f/%$d/$new_d}"
mv myfile - 22 August 2017 myfile - 20170822

答案 1 :(得分:0)

使用perl重命名实用程序,您可以做到(虽然不是很优雅,但是可以!):

rename  'my %m=(Jan=>"01",Feb=>"02",Mar=>"03",Apr=>"04",May=>"05",Jun=>"06",Jul=>"07",Aug=>"08",Sep=>"09",Oct=>"10",Nov=>"11",Dec=>"12",);s/^(.+? - )(\d+) ([a-z]{3})[a-z]* (\d+)$/$1.$4.$m{$3}.$2/ie' *

答案 2 :(得分:0)

如果您的add_action( 'woocommerce_order_status_changed', 'sending_on_hold_email_notification', 20, 4 ); function sending_on_hold_email_notification( $order_id, $old_status, $new_status, $order ){ // Only "On hold" order status and "Free Shipping" Shipping Method if ( $order->has_shipping_method('free_shipping') && $new_status == 'on-hold' ){ // Getting all WC_emails objects $notifications = WC()->mailer()->get_emails(); // Send "On hold" email notification $notifications['WC_Email_Customer_On_Hold_Order']->trigger( $order_id ); } elseif ( ! $order->has_shipping_method('free_shipping') && $new_status == 'completed' ){ // Getting all WC_emails objects $notifications = WC()->mailer()->get_emails(); // Send "On hold" email notification $notifications['WC_Email_Customer_Completed_Order']->trigger( $order_id ); } } 从未包含空格,请尝试以下操作:

myfile

或使用正则表达式(与while read f minus day month year ; do case a$month in (aJan*) nm=01 ;; (aFeb*) nm=02 ;; (aMar*) nm=03 ;; (aApr*) nm=04 ;; (aMay*) nm=05 ;; (aJun*) nm=06 ;; (aJul*) nm=07 ;; (aAug*) nm=08 ;; (aSep*) nm=09 ;; (aOct*) nm=10 ;; (aNov*) nm=11 ;; (aDec*) nm=12 ;; esac; echo "$f - $year$nm$day" # or mv "$f - $day $mont $year" "$f - $year$nm$day" done 一起使用):

sed