【发布时间】:2017-03-25 14:36:49
【问题描述】:
我的 bashrc 文件中有命令脚本,一个以字符串为标题,另一个使用以标题为大小写的字符串创建目录。命令是:
tc() {
sed 's/.*/\L&/; s/[a-z]*/\u&/g' <<<"$1"
}
tcdir() {
mkdir "$(tc "$1")";
}
我怎样才能修改 tc() 命令,使其不使用“the”、“a”、“of”、“in”、“for”等单词的标题,除非它们是字符串中的第一个单词?例如:
the name of the website is stackoverflow
转化为
The Name Of The Website Is Stackoverflow
理想情况下我希望它转变为
The Name of the Website is Stackoverflow
【问题讨论】:
-
通过管道传送到
sed -E 's/ (The|A|Of|I[sn]|For)\b/\L&/g'...您需要手动指定所有单词... -
@Sundeep 好的,我尝试使用 sed -E 's/ (The|A|Of|I[sn]|For)\b/\L&/g' | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
-
订单应该是
echo "$1" | sed 's/.*/\L&/; s/[a-z]*/\u&/g' | sed -E 's/ (The|A|Of|I[sn]|For)\b/\L&/g' -
纯 bash(不调用外部程序):codegolf.stackexchange.com/a/113666/29143 :)