linux bash,用破折号分隔的驼峰式字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8502977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:10:16  来源:igfitidea点击:

linux bash, camel case string to separate by dash

linuxbashcamelcasing

提问by armandfp

Is there a way to convert something like this:

有没有办法转换这样的东西:

MyDirectoryFileLine

to

my-directory-file-line

I found some ways to convert all letters to uppercase or lowercase, but not in that way; any ideas?

我找到了一些将所有字母转换为大写或小写的方法,但不是那样;有任何想法吗?

回答by dogbane

You can use s/\([A-Z]\)/-\L\1/gto find an upper case letter and replace it with a dash and it's lower case. However, this gives you a dash at the beginning of the line, so you need another sed expression to handle that.

您可以使用s/\([A-Z]\)/-\L\1/g查找大写字母并将其替换为破折号,并且它是小写的。但是,这会在行的开头给您一个破折号,因此您需要另一个 sed 表达式来处理它。

This should work:

这应该有效:

sed --expression 's/\([A-Z]\)/-\L/g' \
    --expression 's/^-//'              \
    <<< "MyDirectoryFileLine"

回答by 4ndrew

I propose to use sed to do that:

我建议使用 sed 来做到这一点:

NEW=$(echo MyDirectoryFileLine        \
     | sed 's/\(.\)\([A-Z]\)/-/g' \
     | tr '[:upper:]' '[:lower:]')

UPDI forget to convert to lower case, updated code

UPD我忘记转换为小写,更新代码

回答by eumiro

echo MyDirectoryFileLine | perl -ne 'print lc(join("-", split(/(?=[A-Z])/)))'

prints my-directory-file-line

印刷 my-directory-file-line

回答by bilalq

None of the solutions posted here worked for me. Most didn't support multiple platforms well. The one from @4ndrew was close, but it failed on edge cases that had multiple capitalized characters next to each other (example: FooMVPClientturns into foo-mv-pclientinstead of foo-mvp-client).

这里发布的所有解决方案都不适合我。大多数都不能很好地支持多个平台。来自@4ndrew 的那个很接近,但它在具有多个大写字符彼此相邻的边缘情况下失败(例如:FooMVPClient变成foo-mv-pclient而不是foo-mvp-client)。

This worked for me:

这对我有用:

echo "MyDirectoryMVPFileLine"              \
| sed 's/\([a-z]\)\([A-Z]\)/-/g'       \
| sed 's/\([A-Z]\{2,\}\)\([A-Z]\)/-/g' \
| tr '[:upper:]' '[:lower:]'

output:

输出:

my-directory-mvp-file-line

回答by Peter

Slight variation on @bilalq's answer that covers some more possible edge cases:

@bilalq 的回答略有不同,涵盖了更多可能的边缘情况:

echo "MyDirectoryMVPFileLine"                          \
| sed 's/\([^A-Z]\)\([A-Z0-9]\)/-/g'               \
| sed 's/\([A-Z0-9]\)\([A-Z0-9]\)\([^A-Z]\)/-/g' \
| tr '[:upper:]' '[:lower:]'

output is still:

输出仍然是:

my-directory-mvp-file-line

but also:

但是也:

WhatADeal -> what-a-deal
TheMVP -> the-mvp
DoSomeABTesting -> do-some-ab-testing
The3rdThing -> the-3rd-thing
The3Things -> the-3-things
ThingNumber3 -> thing-number-3

回答by KARASZI István

With GNUsed:

使用GNUsed:

echo "MyDirectoryFileLine"|sed -e 's/\([A-Z]\)/-\L/g'

You just need to strip the first dash if that's bothers you:

如果这让您感到困扰,您只需要去掉第一个破折号:

echo "MyDirectoryFileLine"|sed -e 's/\([A-Z]\)/-\L/g' -e 's/^-//'

With BSDsed it it's a bit longer:

使用BSDsed 它会更长一点:

echo "MyDirectoryFileLine"|sed -e 's/\([A-Z]\)/-/g' -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' -e 's/^-//'

Update: the BSD version will work with the GNU version, so I recommend using the latter.

更新:BSD 版本将与 GNU 版本一起使用,因此我建议使用后者。

回答by potong

This might work for you:

这可能对你有用:

<<<"MyDirectoryFileLine" sed 's/[A-Z]/-\l&/g;s/.//'
my-directory-file-line