sed:在某个位置上设置一个字符

发布于 2024-08-24 14:33:24 字数 903 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-08-31 14:33:24

如果您仍然喜欢 sed,这里是 awk 解决方案

awk '
$1=="public" && $3~/^get/ {
    sub(/^get/,"",$3)
    $3=tolower( substr($3,1,1) ) substr($3,2)
    $0="\t"$2" "$3
}1' file

输出

$ cat file
public class ChallengeTO extends AbstractTransferObject {
        public AuthAlgorithm getAlgorithm();
        public long getCode();
        public int getIteration();
        public String getPublicKey();
        public String getSelt();
};

$ ./shell.sh
public class ChallengeTO extends AbstractTransferObject {
        AuthAlgorithm algorithm();
        long code();
        int iteration();
        String publicKey();
        String selt();
};

,这里是对您的版本的修改,添加 \l

$ sed 's/public \(.*\) get\([A-Z]\)\(.*\)()/\1 \l\2\3/g' file
public class ChallengeTO extends AbstractTransferObject {
        AuthAlgorithm algorithm;
        long code;
        int iteration;
        String publicKey;
        String selt;
};

here's an awk solution

awk '
$1=="public" && $3~/^get/ {
    sub(/^get/,"",$3)
    $3=tolower( substr($3,1,1) ) substr($3,2)
    $0="\t"$2" "$3
}1' file

output

$ cat file
public class ChallengeTO extends AbstractTransferObject {
        public AuthAlgorithm getAlgorithm();
        public long getCode();
        public int getIteration();
        public String getPublicKey();
        public String getSelt();
};

$ ./shell.sh
public class ChallengeTO extends AbstractTransferObject {
        AuthAlgorithm algorithm();
        long code();
        int iteration();
        String publicKey();
        String selt();
};

if you still prefer sed, here's a modification to your version, adding \l

$ sed 's/public \(.*\) get\([A-Z]\)\(.*\)()/\1 \l\2\3/g' file
public class ChallengeTO extends AbstractTransferObject {
        AuthAlgorithm algorithm;
        long code;
        int iteration;
        String publicKey;
        String selt;
};
坏尐絯 2024-08-31 14:33:24

这与使用 sed 从大写到小写几乎相同。

编辑:那里的问题/答案使用 \L GNU sed 扩展。由 \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 GNU sed 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 the y command to change a character that matches one of a source character into a corresponding destination character. This is similar to the Unix utility tr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文