上到下用sed。

时间:2022-10-29 15:29:12

I'd like to change the following patterns:

我想改变以下的模式:

getFoo_Bar

to:

:

getFoo_bar

(note the lower b)

(注意下b)

Knowing neither foo nor bar, what is the replacement pattern?

不知道foo和bar,什么是替换模式?

I started writing

我开始写

sed 's/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1

but I'm stuck: I want to write "\2 lower case", how do I do that?

但是我被卡住了:我想写“\2小写”,我该怎么做?

Maybe sed is not adapted?

也许sed不适应?

8 个解决方案

#1


24  

s/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1\L\2/g

Test:

测试:

$ echo 'getFoo_Bar' | sed -e 's/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1\L\2/g'
getFoo_bar

#2


33  

To change getFoo_Bar to getFoo_bar using sed :

使用sed更改getFoo_Bar到getFoo_Bar:

echo "getFoo_Bar" | sed 's/^\(.\{7\}\)\(.\)\(.*\)$/\1\l\2\3/'

The upper and lowercase letters are handled by :

上面和小写字母的处理方式是:

  • \U Makes all text to the right uppercase.
  • \U使所有的文本到正确的大写。
  • \u makes only the first character to the right uppercase.
  • \u只将第一个字符设置为右大写。
  • \L Makes all text to the right lowercase.
  • \L使所有的文本到正确的小写。
  • \l Makes only the first character to the right lower case. (Note its a lowercase letter L)
  • \l只将第一个字符用于右小写。(注意它是小写字母L)

The example is just one method for pattern matching, just based on modifying a single chunk of text. Using the example, getFoo_BAr transforms to getFoo_bAr, note the A was not altered.

这个示例只是模式匹配的一种方法,只是基于修改一段文本。使用这个例子,getFoo_BAr转换为getFoo_BAr,注意到A没有被修改。

#3


7  

Somewhat shorter:

有些短:

echo getFoo_Bar | sed 's/_\(.\)/_\L\1/'

#4


5  

Shortest I can come up with:

最短的我能想到:

echo getFoo_Bar | sed 's/_./\L&/'

#5


1  

You can use perl for this one:

你可以用perl来做这个:

perl -pe 's/(get[A-Z][A-Za-z0-9]*)_([A-Z])/\1_\l\2/'

The \l is the trick here.

这里有个小技巧。

sed doesn't do upper/lowercase on matched groups.

sed在匹配组上不做大写/小写。

#6


0  

From the FAQ: How do I convert a string to all lowercase or capital letters?

从常见问题:如何将字符串转换为小写字母或大写字母?

#7


0  

If you want to write everything in lowercase just after underscore, than this would work for you:

如果你想在下划线后用小写字母写所有东西,这对你来说很有用:

echo getFoo_Bar | gawk -F_ '{print tolower($2)}'

#8


-2  

echo getFoo_Bar | sed 's/[Bb]/\L&/g'

#1


24  

s/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1\L\2/g

Test:

测试:

$ echo 'getFoo_Bar' | sed -e 's/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1\L\2/g'
getFoo_bar

#2


33  

To change getFoo_Bar to getFoo_bar using sed :

使用sed更改getFoo_Bar到getFoo_Bar:

echo "getFoo_Bar" | sed 's/^\(.\{7\}\)\(.\)\(.*\)$/\1\l\2\3/'

The upper and lowercase letters are handled by :

上面和小写字母的处理方式是:

  • \U Makes all text to the right uppercase.
  • \U使所有的文本到正确的大写。
  • \u makes only the first character to the right uppercase.
  • \u只将第一个字符设置为右大写。
  • \L Makes all text to the right lowercase.
  • \L使所有的文本到正确的小写。
  • \l Makes only the first character to the right lower case. (Note its a lowercase letter L)
  • \l只将第一个字符用于右小写。(注意它是小写字母L)

The example is just one method for pattern matching, just based on modifying a single chunk of text. Using the example, getFoo_BAr transforms to getFoo_bAr, note the A was not altered.

这个示例只是模式匹配的一种方法,只是基于修改一段文本。使用这个例子,getFoo_BAr转换为getFoo_BAr,注意到A没有被修改。

#3


7  

Somewhat shorter:

有些短:

echo getFoo_Bar | sed 's/_\(.\)/_\L\1/'

#4


5  

Shortest I can come up with:

最短的我能想到:

echo getFoo_Bar | sed 's/_./\L&/'

#5


1  

You can use perl for this one:

你可以用perl来做这个:

perl -pe 's/(get[A-Z][A-Za-z0-9]*)_([A-Z])/\1_\l\2/'

The \l is the trick here.

这里有个小技巧。

sed doesn't do upper/lowercase on matched groups.

sed在匹配组上不做大写/小写。

#6


0  

From the FAQ: How do I convert a string to all lowercase or capital letters?

从常见问题:如何将字符串转换为小写字母或大写字母?

#7


0  

If you want to write everything in lowercase just after underscore, than this would work for you:

如果你想在下划线后用小写字母写所有东西,这对你来说很有用:

echo getFoo_Bar | gawk -F_ '{print tolower($2)}'

#8


-2  

echo getFoo_Bar | sed 's/[Bb]/\L&/g'