有没有办法转换这样的东西:
MyDirectoryFileLine
至
my-directory-file-line
我找到了一些将所有字母转换为大写或小写的方法,但不是这样;有任何想法吗?
有没有办法转换这样的东西:
MyDirectoryFileLine
至
my-directory-file-line
我找到了一些将所有字母转换为大写或小写的方法,但不是这样;有任何想法吗?
您可以使用s/\([A-Z]\)/-\L\1/g
查找大写字母并将其替换为破折号,它是小写字母。但是,这会在行首给你一个破折号,所以你需要另一个 sed 表达式来处理它。
这应该有效:
sed --expression 's/\([A-Z]\)/-\L\1/g' \
--expression 's/^-//' \
<<< "MyDirectoryFileLine"
我建议使用 sed 来做到这一点:
NEW=$(echo MyDirectoryFileLine \
| sed 's/\(.\)\([A-Z]\)/\1-\2/g' \
| tr '[:upper:]' '[:lower:]')
UPD我忘记转换为小写,更新代码
echo MyDirectoryFileLine | perl -ne 'print lc(join("-", split(/(?=[A-Z])/)))'
印刷my-directory-file-line
@bilalq 的答案略有不同,涵盖了一些更可能的边缘情况:
echo "MyDirectoryMVPFileLine" \
| sed 's/\([^A-Z]\)\([A-Z0-9]\)/\1-\2/g' \
| sed 's/\([A-Z0-9]\)\([A-Z0-9]\)\([^A-Z]\)/\1-\2\3/g' \
| tr '[:upper:]' '[:lower:]'
输出仍然是:
my-directory-mvp-file-line
但是也:
WhatADeal -> what-a-deal
TheMVP -> the-mvp
DoSomeABTesting -> do-some-ab-testing
The3rdThing -> the-3rd-thing
The3Things -> the-3-things
ThingNumber3 -> thing-number-3
我的微薄贡献适用于“/”(可能用于目录名称或 github 存储库名称)。它不像它可能的那样干净,但可以完成工作。我以@Peter 的贡献为基础,然后进行了一些调整。
function kebab_case() {
echo -n "$1" |\
sed 's/\([^A-Z+]\)\([A-Z0-9]\)/\1-\2/g' |\
sed 's/\([0-9]\)\([A-Z]\)/\1-\2/g' |\
sed 's/\([A-Z]\)\([0-9]\)/\1-\2/g' |\
sed 's/--/-/g' |\
sed 's/\([\/]\)-/\1/g' |\
tr '[:upper:]' '[:lower:]'
}
function assert_kebab_equal() {
local Actual
local Expected
Expected="$1"
Actual="$(kebab_case "$2")"
if [ "${Expected}" != "${Actual}" ]; then
echo Error:
echo " Actual: ${Actual}"
echo "Expected: ${Expected}"
else
echo "$2" "$1" | awk '{ printf "%-30s -> %-40s\n", $1, $2}'
fi
}
assert_kebab_equal "abc-def" "AbcDef"
assert_kebab_equal "/abc-def-ghi/def" "/AbcDef-Ghi/Def"
assert_kebab_equal "/ab-cd-ef" "/AbCdEf"
assert_kebab_equal "repo-owner/repo-name" "RepoOwner/RepoName"
assert_kebab_equal "repo-12-owner/repo-12-name" "Repo12Owner/Repo12Name"
assert_kebab_equal "repo-12-3-owner/repo-12-name" "Repo12-3Owner/Repo12Name"
assert_kebab_equal "repo-owner/repo-name" "REPO-OWNER/REPO-NAME"
assert_kebab_equal "repo-owner-2/repo-name" "REPO-OWNER2/REPO-NAME"
assert_kebab_equal "repo-1-owner" "REPO1-OWNER"
assert_kebab_equal "repo-1-owner-1/22-repo-2-name" "REPO1-OWNER1/22REPO-2NAME"
# Outputs:
AbcDef -> abc-def
/AbcDef-Ghi/Def -> /abc-def-ghi/def
/AbCdEf -> /ab-cd-ef
RepoOwner/RepoName -> repo-owner/repo-name
Repo12Owner/Repo12Name -> repo-12-owner/repo-12-name
Repo12-3Owner/Repo12Name -> repo-12-3-owner/repo-12-name
REPO-OWNER/REPO-NAME -> repo-owner/repo-name
REPO-OWNER2/REPO-NAME -> repo-owner-2/repo-name
REPO1-OWNER -> repo-1-owner
REPO1-OWNER1/22REPO-2NAME -> repo-1-owner-1/22-repo-2-name
此处发布的所有解决方案都不适合我。大多数都不能很好地支持多个平台。来自@4ndrew 的那个很接近,但它在具有多个大写字符彼此相邻的边缘情况下失败了(例如:FooMVPClient
变成foo-mv-pclient
而不是foo-mvp-client
)。
这对我有用:
echo "MyDirectoryMVPFileLine" \
| sed 's/\([a-z]\)\([A-Z]\)/\1-\2/g' \
| sed 's/\([A-Z]\{2,\}\)\([A-Z]\)/\1-\2/g' \
| tr '[:upper:]' '[:lower:]'
输出:
my-directory-mvp-file-line
这可能对您有用:
<<<"MyDirectoryFileLine" sed 's/[A-Z]/-\l&/g;s/.//'
my-directory-file-line
使用GNU sed:
echo "MyDirectoryFileLine"|sed -e 's/\([A-Z]\)/-\L\1/g'
如果这让您感到困扰,您只需要去掉第一个破折号:
echo "MyDirectoryFileLine"|sed -e 's/\([A-Z]\)/-\L\1/g' -e 's/^-//'
使用BSD sed 它会更长一些:
echo "MyDirectoryFileLine"|sed -e 's/\([A-Z]\)/-\1/g' -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' -e 's/^-//'
更新:BSD 版本将与 GNU 版本一起使用,所以我建议使用后者。
echo "SomeACRONYMInCamelCaseString" \
| sed -e 's/\([a-z]\)\([A-Z]\)/\1-\L\2/' \
| sed -e 's/\(.*\)/\L\1/')
sed -e 's/\([a-z]\)\([A-Z]\)/\1-\L\2/'
仅当前面有小写字母时,才用连字符和小写字母替换大写字母。
sed -e 's/\(.*\)/\L\1/'
将整个字符串小写