使用date命令在bash脚本中的sed命令中重新格式化日期 [英] Use date command to reformat date within sed command in bash script
问题描述
我正尝试从以下文件名重命名一系列pdf: The New Town Cryer-2020年10月1日。pdf
更改为该 2020- 10-01 _-_ The_New_Town_Cryer.pdf
。我已经编写了一个使用 sed
的bash脚本来完成此任务,但是我很难弄清楚如何使用<从当前的三个字母月份的格式转换日期。 code> date 命令。到目前为止,这是我的脚本行(先前的 newname
变量为 The New Town Cryer-2020年10月1日pdf
:
I am trying to rename a series of pdfs from filenames like this: The New Town Cryer - 01 Oct 2020.pdf
to this 2020-10-01_-_The_New_Town_Cryer.pdf
. I've written a bash script that uses sed
to accomplish this, but I'm having trouble figuring out how to convert the date from the current three letter month format using the date
command. This is the line of my script so far (the previous newname
variable is The New Town Cryer - 01 Oct 2020 pdf
:
newname="$(echo "$newname" | sed -re 's/^(.*) - (.*) ([^ ]+)$/echo "$(date -d "\2" "+%Y-%m-%d")-\1".\3/')"
此行的输出是 echo $(date -d 2020年10月1日 ; +%Y-%m-%d)-The New Town Cryer .pdf
,我希望它将是 2020-10-01-The New Town Cryer.pdf
The output from this line is echo "$(date -d"01 Oct 2020" "+%Y-%m-%d")-The New Town Cryer".pdf
, where I was hoping it would be 2020-10-01-The New Town Cryer.pdf
有人可以告诉我我要去哪里吗?
Can anyone tell me where I'm going wrong? Thanks!
编辑:在此处进行说明到目前为止,这是我的整个脚本,因为似乎我的代码段不清楚。文件名的原始格式为 The New Town Cryer-No.1,032 [2020年10月1日] .pdf
,我尝试将其转换为格式 2020-10-01_The_New_Town_Cryer.pdf
。
to clarify here is my whole script so far, since it seems that my snippet was unclear. The original format of the filenames is The New Town Cryer - No. 1,032 [01 Oct 2020].pdf
, which I am trying to convert to the format 2020-10-01_The_New_Town_Cryer.pdf
.
#!/bin/bash
find "$1" "*.pdf" -type f -printf "%f\n" | while IFS= read -r f ; do #find all pdfs
name=$f
newname="$(echo "$name" | sed -re 's/\./ /g')" # replace .s with spaces to allow 'date'-command to parse the date
newname="$(echo "$newname" | sed -re 's/\[/!/g')" # replace [s with spaces to allow 'date'-command to parse the date
newname="$(echo "$newname" | sed -re 's/\]/!/g')" # replace [s with spaces to allow 'date'-command to parse the date
newname="$(echo "$newname" | sed -re 's/(.*) - (.*) (!.*!)/\1\ - \3/')" # remove issue number
newname="$(echo "$newname" | sed -re 's/\!//g')" # replace !s with spaces to allow 'date'-command to parse the date
newname="$(echo "$newname" | sed -re 's/^(.*) - (.*) ([^ ]+)$/echo "$(date -d "\2" "+%Y-%m-%d")-\1".\3/')" # reorder the date and name, split at '-', keep the file extension, prepare for date conversion
newname="$(echo "$newname" | bash )"
newname="$(echo "$newname" | sed -re 's/ /./g')" # replace remaining spaces with .
mv "$name" "$newname"
done
推荐答案
使用bash的本机正则表达式支持尝试在此处使用($)$ sed 的代码使代码(虽然可能更长)更易于阅读。
作为解决方案,您可以在 https://ideone.com/Suw9Ow 上进行工作:
Using bash's native regex support instead of trying to (ab)use sed
here makes the code -- while perhaps longer -- much clearer to read.
As a solution you can see working at https://ideone.com/Suw9Ow:
oldname='The New Town Cryer - 01 Oct 2020.pdf'
date_re='(^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*)'
if [[ $oldname =~ $date_re ]]; then
basename=${BASH_REMATCH[1]}
day=${BASH_REMATCH[2]}
month=${BASH_REMATCH[3]}
year=${BASH_REMATCH[4]}
ext=${BASH_REMATCH[5]}
new_date=$(date -d "${day} ${month} ${year}" +%Y-%m-%d)
newname="${new_date} - ${basename}${ext}"
echo "Old name: $oldname"
echo "New name: $newname"
fi
这篇关于使用date命令在bash脚本中的sed命令中重新格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!