Replace alphabets in a string with their ASCII values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace alphabets in a string with their ASCII values
# 8  
Old 10-04-2016
@RavinderSingh13: What happens if the to-be-converted string has trailing alpha chars, e.g. val="AB123DE"?
This User Gave Thanks to RudiC For This Post:
# 9  
Old 10-04-2016
Of all the replies, Don Cragun's has been the eye opener for me.

I thank all of you for the support, but the actuality is ABCA653 and ABCAA3
needs to be handled seperately even if the expected command evaluates to the same numerical value, which is 65666567123.

Let me brief you my business requirement, which I can manifest only to the slightest amount.
these alphanumeric strings are portfolio#'s, and we are receiving multiple files for each portfolio#, and we are required to always move files for each folio into the same folder.

We have to distribute all folio files in a round robin manner into 3 folders and all files for ABCA653 should be moved into a single folder, and similarly for ABCAA3, but these two are totally different folio#.


How can I do that?

Please revert back if you need any further info.

thanks.
Kumarjit

Last edited by rbatte1; 10-05-2016 at 05:46 AM.. Reason: Added ICODE tags
# 10  
Old 10-04-2016
Ok,
Can you change your specifies as below ?
  • letter -> hex value
  • number (0-9) -> 00-09
  • In this case, "ABCA653" -> "65666765060503"
  • and "ABCAA3" -> "656667656503"
In perl:
Code:
echo "ABCA653" | perl -ne 'chomp;print map /[0-9]/ ? "0".$_ : ord, split //'

Regards.

Last edited by rbatte1; 10-04-2016 at 10:22 AM..
# 11  
Old 10-04-2016
Quote:
Originally Posted by RudiC
@RavinderSingh13: What happens if the to-be-converted string has trailing alpha chars, e.g. val="AB123DE"?
Hello RudiC,

Thank you for letting me know here. So following are the 2 solutions.
I- Which will work for a variable as per OP's request.
II- Where we could pass an Input_file to it.
Solution 1st:
Code:
awk -vs1="'" -vval="AB123DE" BEGIN'{
                                        num=split(val,array,"");
                                        for(i=1;i<=num;i++){
                                                                if(array[i] ~ /[[:alpha:]]/){
                                                                                                system("printf " array[i] "| od -A n -t d1 >> temp_file")
                                                                                            }
                                                                else                        {
                                                                                                printf("%d",array[i]) >> "temp_file"
                                                                                            }
                                                           };
                                        system("awk " s1 "{sub(/^ +/,X,$0);W=W?W $0:$0} END{sub(/ +/,X,W);print W;} " s1 " temp_file;rm temp_file");
                                   }'

Output will be as follows.
Code:
65661236869

Solution 2nd:
Let's say we have following Input_file:
Code:
cat  Input_file
ASBVC123
1234ERDTSFGJ
ABSSFDG1231ASASSD
1221ASAWWRT1221

Then following is the solution for above Input_file.
Code:
awk -vs2="\"" -vs1="'"                    '{
                                        num=split($0,array,"");
                                        for(i=1;i<=num;i++){
                                                                if(array[i] ~ /[[:alpha:]]/){
                                                                                                system("printf " array[i] "| od -A n -t d1 >> tempfile")
                                                                                            }
                                                                if(array[i] !~ /[[:alpha:]]/){
                                                                                                system("printf " array[i] " >> tempfile")
                                                                                            }
                                                           };
                                        system("awk " s1 "{sub(/^ +/,X,$0);W=W?W $0:$0} END{sub(/ +/,X,W);print W;W=" s2 s2 "} " s1 " tempfile;rm tempfile");
                                           }
                                          '      Input_file

Output will be as follows.
Code:
6583668667123
12346982688483707174
656683837068711231658365838368
1221658365878782841221

Thanks,
R. Singh
# 12  
Old 10-04-2016
Thanks for the help disedorgue. Just the thing I wanted.....

But, can this one liner be acheived by any other command than PERL?
The reason for asking so is in our project, we've not used/abstained from using PERL so far.

Any help is much appreciated.


thanks.
Kumarjit
# 13  
Old 10-04-2016
Change the %s in post#6 to %02d.
# 14  
Old 10-04-2016
If you have an aversion to perl, and one-liner is a must, try sed -f command_file and put
Code:
s/A/65/
s/B/66/
etc

into the command_file.

Juha Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

<< generate alphabets and append in the input values >>

Hi Team, Pls help to get the desired output. I have a input like below nodecount=10 host=na7-db1-1-chi nodecount can be 10 or 8 based on this we need a output (in single line) like below na7-db1-1-chi:A na7-db1-2-chi:B na7-db1-3-chi:C na7-db1-4-chi:D na7-db1-5-chi:E... (4 Replies)
Discussion started by: kamauv234
4 Replies

2. UNIX for Dummies Questions & Answers

Replace alphabets from certain positions

Hi all, I have column 2 full of values like HIVE4A-56 and HIVE4-56. I want to convert all values like HIVE4A-56 to HIVE4-56. So basically I want to delete all single alphabets before the '-' which is always preceded by a number. Values already in the desired format should remain unchanged... (4 Replies)
Discussion started by: ames1983
4 Replies

3. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

4. Shell Programming and Scripting

Search for multiple string and replace with respective values

Hi, Can anyone help me to search for multiple strings within specified position and replace with respective string value. For example I need to search the string from the position 11 to 20 and if it contain ABC and then replace it by BCDEFGHIJ ... find AABZSDJIK and replace with QWE. and... (4 Replies)
Discussion started by: zooby
4 Replies

5. Shell Programming and Scripting

Incrementing ascii values

Hi All, I require some help with the below: I am trying to incriment the ascii value of a letter and then print it. So basically "a" becomes "b" and "z" becomes "A". Does anyone have any pointers? Cheers, Parks (10 Replies)
Discussion started by: bParks
10 Replies

6. Shell Programming and Scripting

Replace blank values for string using sed

Hi everyone, I was wondering how could from a file where each row is separated by tabulations, the row values where are in blank replace them by a string or value. My file has this form: 26/01/09 13:45:00 0 0 26/01/09 14:00:00 1495.601318 0 26/01/09 14:15:00 1495.601318 0 ... (4 Replies)
Discussion started by: tonet
4 Replies

7. Shell Programming and Scripting

How do i use sed to replace string with values from a dictionary file

I have file 1 with one million rows. one of the fields is "FIRSTNAME" (the string) I have a second file with about 20 first names. JUDE DAVID HOMER CANE ABEL MARTY CARL SONNY STEVE BERT OSCAR MICKY JAMES JOHN GLENN DOUG (3 Replies)
Discussion started by: yoyolandre
3 Replies

8. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below îîîîîîîîîîîî0î-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

10. Shell Programming and Scripting

Replace characters in a string using their ascii value

Hi All, In the HP Unix that i'm using when i initialise a string as Stalled="'30¬G'" Stalled=$Stalled" '30¬C'", it is taking the character ¬ as a comma. I need to grep for 30¬G 30¬C in a file and take its count. But since this character ¬ is not being understood, the count returns a zero. The... (2 Replies)
Discussion started by: roops
2 Replies
Login or Register to Ask a Question