How to get the next character?

Hi All,

I have the below requirement in my current project.

I have to replace every "character" with the "next character" in ascii table. Only exception is , when the character is "z", i want it to be replaced with "a".

I want to apply the above logic only to certain cols, ex: 13 and 16th here.

Example:

input_file : 
'2012-09-20 09:59:01.0','13','20','11','58376','22279','cell','11/58376/22279','20952A5','RHBF01','BT0111',,EVERGLADEZ 187950',,,'H3G J T0111Z',UP SC',,'310','52.982022','46.663897',,,,,,,,,

output_expecting: 
'2012-09-20 09:59:01.0','13','20','11','58376','22279','cell','11/58376/22279','20952A5','RHBF01','BT0111',,FWFSHMBEFA 187950',,,'I3H K U0111A',UP SC',,'310','52.982022','46.663897',,,,,,,,,

Any help?

Regards,
Ravi

perl -F',' -lane 'grep { s/([a-yA-Y])/chr(ord($1) + 1)/ge; s/([zZ])/chr(ord($1) - 25)/ge; } ($F[12], $F[15]); print join(",",@F)' file
1 Like

Sorry couldnt complete the script (Have to hit gym now ;)).. However I guess you can work on this :slight_smile:

 
 
awk -F"," 'BEGIN{v="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A"}{
split(v,A," ")
for(i=0;i<=length($13);i++){
for(j=1;j<=27;j++){
if(A[j]==substr($13,i,1)){
var=var""A[j+1]
}}}} {print var}' filename
1 Like

Nice solution.
You could change v like this v="ABCDEFGHIJKLMNOPQRSTUVWXYZA"
And then use split(v,A,"")

Another lengthy approach:

awk -F, '
        BEGIN {
                c = split ("ABCDEFGHIJKLMNOPQRSTUVWXYZ", U, "" )
                c = split ("abcdefghijklmnopqrstuvwxyz", L, "" )
        }
        function inc_char( str )
        {
                n = split ( str, A, "" )
                for ( i = 1; i <= n; i++ )
                {
                        if ( A ~ /[a-zA-Z]/ && tolower(A) == A )
                        {
                                for ( j = 1; j <= c; j++ )
                                {
                                        if ( L[j] == A )
                                        {
                                                if ( j == c )
                                                        j = 0
                                                s = s ? s L[j+1] : L[j+1]
                                                break
                                        }
                                }
                        }
                        if ( A ~ /[a-zA-Z]/ && toupper(A) == A )
                        {
                                for ( j = 1; j <= c; j++ )
                                {
                                        if ( U[j] == A )
                                        {
                                                if ( j == c )
                                                        j = 0
                                                s = s ? s U[j+1] : U[j+1]
                                                break
                                        }
                                }
                        }
                        if ( A !~ /[a-zA-Z]/ )
                        {
                                s = s ? s A : A
                        }
                }
                str = s
                s = ""
                return ( str )
        }
        {
                $13 = inc_char( $13 )
                $16 = inc_char( $16 )
        }
        1
' OFS="," file.csv
1 Like

Hi All,

Thnx for your excellent solutions/suggestions.!

Regards,
Ravi