Skip to content

Instantly share code, notes, and snippets.

@rainyear
Last active December 20, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainyear/6150585 to your computer and use it in GitHub Desktop.
Save rainyear/6150585 to your computer and use it in GitHub Desktop.
Rename literature files.
#!/bin/bash
## 重命名规则
# 1. 长于3个字母的单词首字母大写,短连接线前后大写;
# 2. 只允许出现[空格、短线|单词、数字],点号只允许出现在后缀名之前;
# 3. 单词与数字、数字与数字之间以短线相连。
if [ $# != 1 ] || [ ! -f "$1" ];then
echo Input file-name error!
exit 1
fi
mv "$1" "`basename "$1"|sed 's/[ ]*:[ ]*/-/g;s/\./=/g;s/\(.*\)=/\1./g;s/[ ]*[+|?|_|,|;][ ]*/ /g;s/=/ /g;s/ \([0-9]\)/-\1/g;s/\([0-9]\) /\1-/g'|awk -F- '{for(i=1;i<=NF;i++){$i=tolower($i);sub(".",toupper(substr($i,1,1)),$i)}}1' OFS="-"|awk '{for(i=1;i<=NF;i++){if(length($i)>3||i==1){sub(".",toupper(substr($i,1,1)),$i)}}}1'`"
echo "`basename "$1"|sed 's/[ ]*:[ ]*/-/g;s/\./=/g;s/\(.*\)=/\1./g;s/[ ]*[+|?|_|,|;][ ]*/ /g;s/=/ /g;s/ \([0-9]\)/-\1/g;s/\([0-9]\) /\1-/g'|awk -F- '{for(i=1;i<=NF;i++){$i=tolower($i);sub(".",toupper(substr($i,1,1)),$i)}}1' OFS="-"|awk '{for(i=1;i<=NF;i++){if(length($i)>3||i==1){sub(".",toupper(substr($i,1,1)),$i)}}}1'`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment