Notinlist
Notinlist

Reputation: 16640

sed: toupper a character on a location

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)

Upvotes: 1

Views: 323

Answers (2)

JXG
JXG

Reputation: 7403

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.

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342463

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;
};

Upvotes: 1

Related Questions