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
# 1  
Old 10-04-2016
Replace alphabets in a string with their ASCII values

hi. I have a requirement where I need to REPLACE all alphabets from an alphanumeric input string into their respective ASCII decimal value. For example:

If the input string is ABAC123, the output should be 65666567123

I am seeking a single line command, and I was trying searching for options of TR command.

Please help me.


thanks.
Kumarjit.

Last edited by rbatte1; 10-04-2016 at 07:05 AM.. Reason: Inserted ICODE tags
# 2  
Old 10-04-2016
Hello kumarjt,

Could you please try following and let us know if this helps you.
Code:
echo "ABAC123" | od -A n -t d1

Thanks,
R. Singh
# 3  
Old 10-04-2016
thanks for the lead Ravinder, but the output is not as expected.

I need 65666567123, but with the command you gave, output is 65 66 65 67 49 50 51 10


thanks.
Kumarjit


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read and preserves multiple spaces for indenting or fixed width data.
You can use ICODE tags for short strings in the flow of a sentence as I have done for you above.
[

Last edited by rbatte1; 10-04-2016 at 07:07 AM.. Reason: Added ICODE tags
# 4  
Old 10-04-2016
Hi R. Singh,
Note that the output kumarjt wants to produce contains no spaces and only translates alphabetic characters (leaving numeric input characters unchanged) producing ambiguous output when numeric characters are included in the input .

With kumarjt's specified behavior, the output from both of the following input strings:
ABCA653 and ABCAA3 is the same string 65666765653.

Hi kumarjt,
The tr utility translates one character to one character, it does not translate a single character to a pair of characters.
# 5  
Old 10-04-2016
Hi,
with perl:
Code:
echo ABAC123 | perl -ne 'chomp;print map /[0-9]/ ? $_ : ord, split //'
65666567123

Regards.
# 6  
Old 10-04-2016
Try
Code:
echo "ABAC123" | od -An -ctd1 | awk 'NR == 1 {split ($0, T); next} {for (i=1; i<=NF; i++) printf "%s", ($i>=65)?$i:($i<32)?"":T[i]; printf RS}' 
65666567123

... may need some refinement ...
# 7  
Old 10-04-2016
Quote:
Originally Posted by kumarjt
thanks for the lead Ravinder, but the output is not as expected.
I need 65666567123, but with the command you gave, output is 65 66 65 67 49 50 51 10
thanks.
Kumarjit
[
Hello kumarjt,

Could you please try following and let me know if this helps you.
Code:
awk -vs1="'" -vval="ABAC123" BEGIN'{num=split(val,array,"");for(i=1;i<=num;i++){if(array[i] ~ /[[:alpha:]]/){Q=Q?Q array[i]:array[i]} else {printf("%d",array[i]) >> "temp_file2"}};system("printf " Q "| od -A n -t d1 > temp_file;awk " s1 "{gsub(/ /,X,$0);V=V?V $0:$0} END{print V}" s1 " temp_file temp_file2;rm temp_file temp_file2");}'

Output will be as follows.
Code:
65666567123

So above command will look for only alphabets and convert them into their ASCII their values with printing digits values as it is.
EDIT: Adding a non-one liner form of above solution too now.
Code:
 awk -vs1="'" -vval="ABAC123" BEGIN'{
                                        num=split(val,array,"");
                                        for(i=1;i<=num;i++){
                                                                if(array[i] ~ /[[:alpha:]]/){
                                                                                                Q=Q?Q array[i]:array[i]
                                                                                            }
                                                                else                        {
                                                                                                printf("%d",array[i]) >> "temp_file2"
                                                                                            }
                                                           };
                                        system("printf " Q "| od -A n -t d1 > temp_file;awk " s1 "{gsub(/ /,X,$0);V=V?V $0:$0} END{print V}" s1 " temp_file temp_file2;rm temp_file temp_file2");
                                   }
                                  '

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-04-2016 at 09:21 AM.. Reason: Adding a non-one liner form of above solution too now.
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