linux下的bash,骆驼字符串由​​破折号分开 [英] linux bash, camel case string to separate by dash

查看:135
本文介绍了linux下的bash,骆驼字符串由​​破折号分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来转换是这样的:

Is there a way to convert something like this:

MyDirectoryFileLine

my-directory-file-line

我找到了一些方法来转换所有字母为大写或小写,但不能以这种方式;任何想法?

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

推荐答案

您可以使用 S / \\([AZ] \\)/ - \\大号\\ 1 / G 找到一个大写字母和破折号替换它和它的小写。但是,这给你在该行的开头一个破折号,所以你需要另一个SED前pression来处理。

You can use s/\([A-Z]\)/-\L\1/g to 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.

这应该工作:

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

这篇关于linux下的bash,骆驼字符串由​​破折号分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆