sed:在某个位置上设置一个字符
这
sed "s/public \(.*\) get\(.*\)()/\1 \2/g"
会将其转换
public class ChallengeTO extends AbstractTransferObject {
public AuthAlgorithm getAlgorithm();
public long getCode();
public int getIteration();
public String getPublicKey();
public String getSelt();
};
为
public class ChallengeTO extends AbstractTransferObject {
AuthAlgorithm Algorithm;
long Code;
int Iteration;
String PublicKey;
String Selt;
};
我想将 Algorithm
更改为 algorithm
,将 PublicKey
更改为 publicKey
等等。如何将第二段的第一个字符 (\2
) 转换为小写?
UPDATE
sed "s/public \(.*\) get\([AZ]\)\(.*\)()/\1 \2\3/g"
选择“我的信”作为 \2
,但如果我在它之前放置 \L
它会转换太多(包括 \3
)
This
sed "s/public \(.*\) get\(.*\)()/\1 \2/g"
will transfor this
public class ChallengeTO extends AbstractTransferObject {
public AuthAlgorithm getAlgorithm();
public long getCode();
public int getIteration();
public String getPublicKey();
public String getSelt();
};
into this
public class ChallengeTO extends AbstractTransferObject {
AuthAlgorithm Algorithm;
long Code;
int Iteration;
String PublicKey;
String Selt;
};
I want to change Algorithm
to algorithm
, PublicKey
to publicKey
and so on. How can I transform the first character of the second segment (\2
) to lower case?
UPDATE
sed "s/public \(.*\) get\([A-Z]\)\(.*\)()/\1 \2\3/g"
selects "my letter" as \2
, but if I place a \L
before it it transforms too much (including \3
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您仍然喜欢 sed,这里是 awk 解决方案
输出
,这里是对您的版本的修改,添加
\l
here's an awk solution
output
if you still prefer sed, here's a modification to your version, adding
\l
这与使用 sed 从大写到小写几乎相同。
编辑:那里的问题/答案使用
\L
GNUsed
扩展。由\L
开始的转换可以由\E
关闭,因此,如果您将“您的字母”梳理到\2
中,您可以在其前面放置一个\L
,并在其后面放置一个\E
。请参阅 GNU
sed
文档 了解更多信息。如果您没有 GNU 扩展,则可以使用两个单独的 sed 命令来完成此操作。您可以使用
y
命令将与源字符之一匹配的字符更改为相应的目标字符。这类似于 Unix 实用程序tr
。This is nearly identical to upper- to lower-case using sed.
EDIT: The question/answer there uses the
\L
GNUsed
extension. The conversion begun by\L
can be turned off by\E
, so if you tease out "your letter" into\2
, you can put a\L
before it and a\E
immediately after it.See the GNU
sed
documentation for more information.If you don't have GNU extensions, you can do this with two separate
sed
commands. You can use they
command to change a character that matches one of a source character into a corresponding destination character. This is similar to the Unix utilitytr
.