How to get the next character?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the next character?
# 1  
Old 05-13-2013
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:

Code:
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
# 2  
Old 05-13-2013
Code:
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


Last edited by balajesuri; 05-13-2013 at 10:34 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 05-13-2013
Sorry couldnt complete the script (Have to hit gym now Smilie).. However I guess you can work on this Smilie
Code:
 
 
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


Last edited by vidyadhar85; 05-13-2013 at 10:33 AM..
This User Gave Thanks to vidyadhar85 For This Post:
# 4  
Old 05-13-2013
Nice solution.
You could change v like this v="ABCDEFGHIJKLMNOPQRSTUVWXYZA"
And then use split(v,A,"")
# 5  
Old 05-13-2013
Another lengthy approach:
Code:
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[i] ~ /[a-zA-Z]/ && tolower(A[i]) == A[i] )
                        {
                                for ( j = 1; j <= c; j++ )
                                {
                                        if ( L[j] == A[i] )
                                        {
                                                if ( j == c )
                                                        j = 0
                                                s = s ? s L[j+1] : L[j+1]
                                                break
                                        }
                                }
                        }
                        if ( A[i] ~ /[a-zA-Z]/ && toupper(A[i]) == A[i] )
                        {
                                for ( j = 1; j <= c; j++ )
                                {
                                        if ( U[j] == A[i] )
                                        {
                                                if ( j == c )
                                                        j = 0
                                                s = s ? s U[j+1] : U[j+1]
                                                break
                                        }
                                }
                        }
                        if ( A[i] !~ /[a-zA-Z]/ )
                        {
                                s = s ? s A[i] : A[i]
                        }
                }
                str = s
                s = ""
                return ( str )
        }
        {
                $13 = inc_char( $13 )
                $16 = inc_char( $16 )
        }
        1
' OFS="," file.csv


Last edited by Yoda; 05-13-2013 at 10:54 AM..
This User Gave Thanks to Yoda For This Post:
# 6  
Old 05-14-2013
Hi All,

Thnx for your excellent solutions/suggestions.!

Regards,
Ravi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

3. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

4. Shell Programming and Scripting

Finding a certain character in a filename and count the characters up to the certain character

Hello, I do have folders containing having funny strings in their names and one space. First, I do remove the funny strings and replace the space by an underscore. find . -name '* *' | while read file; do target=`echo "$file" | sed 's/... (2 Replies)
Discussion started by: tempestas
2 Replies

5. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

7. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

8. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

9. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

10. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies
Login or Register to Ask a Question