views:

79

answers:

2

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)

+1  A: 

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;
};
ghostdog74
I believe that a sed one-liner should solve my problem. I wouldn't go to awk only if the sed way become too complicated.
Notinlist
ok, so you want a sed one liner. see update.
ghostdog74
+1  A: 

This is nearly identical to http://stackoverflow.com/questions/689495/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.

JXG
The `\L` transforms the whole rest. `PublicKey` into `publickey` not `publicKey`. Updating question with this bit of info. +1
Notinlist
The first part of the edit is awesome. Thank you. I am sad I cannot give more points :-). The second is not location dependent, so it is not good for me.
Notinlist