我需要一些正则表达式帮助。其范围是更改文件名。这是模式。
Super subject - Subject - Subsubject - Extra History - #number--hash.mp4
如以下两个示例所示,超级主题并不总是存在。我想将#number移到主题之后,并在末尾删除哈希。
我需要一个可以执行此操作的bash命令或脚本。
Africa - Zulu Empire - Diamonds in South Africa - Extra History - #3--JG-5otw0O8.mp4
Simón Bolívar - Defeat is Not Surrender - Extra History - #4-Ie0IoiQQFKA.mp4
我正在寻找以下内容的输出
Africa - Zulu Empire 3 - Diamonds in South Africa - Extra History.mp4
Simón Bolívar 4 - Defeat is Not Surrender - Extra History.mp4
我这样问这个问题,因为我知道正则表达式非常具体并且我还没有学过正则表达式。如果有我可以自己解决的特定工具,请告诉我。此工具需要易于使用。
答案 0 :(得分:0)
您的意思是这样的吗?
sed 's/\([^-]*\) - \([^-]*\)\([^#]*\) - #\([0-9]*\)[^.]*\(.*\)$/\1 \4 - \2\3\5/' testdata.txt
testdata.txt
Africa - Zulu Empire - Diamonds in South Africa - Extra History - #3--JG-5otw0O8.mp4
Simón Bolívar - Defeat is Not Surrender - Extra History - #4-Ie0IoiQQFKA.mp4
输出
Africa 3 - Zulu Empire - Diamonds in South Africa - Extra History.mp4
Simón Bolívar 4 - Defeat is Not Surrender - Extra History.mp4
说明
sed 's/ # use sed substitution
\([^-]*\) # store all characters unequal - in arg1 (\2)
- # ignore separator
\([^-]*\) # store all characters unequal - in arg2 (\2)
\([^#]*\) # store all characters unequal # in arg1 (\3)
- # # ignore separator
\([0-9]*\) # store number in arg4 (\4)
[^.]* # ignore all chars unequal .
\(.*\) # store suffix in arg5 (\5)
/\1 \4 - \2\3\5/' # create output
用于重命名本地目录中文件的脚本
#!/bin/bash
for file in *.mp4; do
newname=$(sed 's/\([^-]*\) - \([^-]*\)\([^#]*\) - #\([0-9]*\)[^.]*\(.*\)$/\1 \4 - \2\3\5/' <<<"$file")
mv "$file" "$newname"
done
答案 1 :(得分:0)
如果您选择一个perl单线版,则可以执行以下操作:
在拥有文件的目录中
perl -e '@l=glob("*.mp4");for(@l){$old=$_;@e=split(/ - /,$_);($n)=$e[-1]=~/(\d+)/;$_=($e[-5]?"$e[-5] - ":"")."$e[-4] $n - $e[-3] - $e[-2].mp4";rename$old,$_}'
说明:
perl -e ' # invoke perl
@l=glob("*.mp4"); # search all mp4 in current directory
for(@l){ # for each file found
$old=$_; # save old name
@e=split(/ - /,$_); # explode filename on ' - '
($n)=$e[-1]=~/(\d+)/; # extract the number from last element
$_= # new name
($e[-5]?"$e[-5] - ":""). # first element if exists
"$e[-4] $n - $e[-3] - $e[-2].mp4"; # all other element except the last
rename$old,$_ # rename the file
}' # end script
我建议您在运行此目录之前先保存原始目录
结合您的两个示例:
~/devel/tmp$ll
total 8
drwxr-xr-x 2 olivier olivier 4096 mai 28 17:13 ./
drwxr-xr-x 6 olivier olivier 4096 nov. 19 2018 ../
~/devel/tmp$touch "Africa - Zulu Empire - Diamonds in South Africa - Extra History - #3--JG-5otw0O8.mp4"
~/devel/tmp$touch "Simón Bolívar - Defeat is Not Surrender - Extra History - #4-Ie0IoiQQFKA.mp4"
~/devel/tmp$ll
total 8
drwxr-xr-x 2 olivier olivier 4096 mai 28 17:13 ./
drwxr-xr-x 6 olivier olivier 4096 nov. 19 2018 ../
-rw-r--r-- 1 olivier olivier 0 mai 28 17:13 Africa - Zulu Empire - Diamonds in South Africa - Extra History - #3--JG-5otw0O8.mp4
-rw-r--r-- 1 olivier olivier 0 mai 28 17:13 Simón Bolívar - Defeat is Not Surrender - Extra History - #4-Ie0IoiQQFKA.mp4
~/devel/tmp$perl -e '@l=glob("*.mp4");for(@l){$old=$_;@e=split(/ - /,$_);($n)=$e[-1]=~/(\d+)/;$_=($e[-5]?"$e[-5] - ":"")."$e[-4] $n - $e[-3] - $e[-2].mp4";rename$old,$_}'
~/devel/tmp$ll
total 8
drwxr-xr-x 2 olivier olivier 4096 mai 28 17:14 ./
drwxr-xr-x 6 olivier olivier 4096 nov. 19 2018 ../
-rw-r--r-- 1 olivier olivier 0 mai 28 17:13 Africa - Zulu Empire 3 - Diamonds in South Africa - Extra History.mp4
-rw-r--r-- 1 olivier olivier 0 mai 28 17:13 Simón Bolívar 4 - Defeat is Not Surrender - Extra History.mp4