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
# 15  
Old 10-04-2016
Just with awk:
Code:
 echo ABCA341 | awk '{
  for(i=0;i<=255;i++){A[sprintf("%c", i + 0)]=i;}
}
{
  split($1,B,"")
  for(i=1;i<=length(B);++i){
     if(B[i]<=9){
       printf("%02d",B[i])
     }
     else{
       printf("%i",A[B[i]])
     }
  }
}'
65666765030401

Regards.
# 16  
Old 10-04-2016
Hi,
Perl suits perfectly for the work : the ord function converts a character to its ascii decimal value.
See:
Code:
$ echo -n This works fine  |perl  -ne 'for ($i=0; $i < length ; $i++) { print   ord (substr $_,$i,1) ," " } '
84 104 105 115 32 119 111 114 107 115 32 102 105 110 101

# 17  
Old 10-04-2016
Hi,
@blastit.fr : perl solution already proposed but it isn't possible to use (read post#12).

Otherwise, another way in pure builtins bash:
Code:
$ X=ABCD234;i=0;while ((i<${#X})); do [[ "${X:$i:1}" =~ [0-9] ]] || Y=\';printf "%02d" "${Y}${X:((i++)):1}";Y=""; done
65666768020304

or
Code:
echo -n ABCDE123 | while read -n 1 -d "" X ; do [[ $X =~ [0-9] ]] || Y=\';printf "%02d" "${Y}${X}";Y=""; done
6566676869010203

Regards.
This User Gave Thanks to disedorgue For This Post:
# 18  
Old 10-05-2016
Brilliant. Where can I learn more about this trick printing "'A"?
# 19  
Old 10-05-2016
Hi RudiC,
This trick (not just in bash) validate (in : printf ) :
Quote:
  • If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.
Regards.
This User Gave Thanks to disedorgue For This Post:
# 20  
Old 10-05-2016
Thanks for the reference. Even though it works as specified, it is not mentioned in the respective linux man page (although it is in FreeBSD).
# 21  
Old 10-05-2016
Quote:
Originally Posted by RudiC
Thanks for the reference. Even though it works as specified, it is not mentioned in the respective linux man page (although it is in FreeBSD).
It seems that some Linux developers update code to meet some POSIX requirements without taking the time to update the corresponding man page. (Do not read this as a slur against Linux; it happens in other development organizations too.)

It is a requirement in the POSIX description of the printf utility (although it is not allowed in the printf() family of C functions). From the 2016 edition (published by IEEE and The Open Group last week; still being balloted by ISO) of the POSIX standard printf utility EXTENDED DESCRIPTION section on P3114, L101141-104149:
Quote:
The argument operands shall be treated as strings if the corresponding conversion specifier is b, c, or s, and shall be evaluated as if by the strtod() function if the corresponding conversion specifier is a, A, e, E, f, F, g, or G. Otherwise, they shall be evaluated as unsuffixed C integer constants, as described by the ISO C standard, with the following extensions:
  • A leading <plus-sign> or <hyphen-minus> shall be allowed.
  • If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.
  • Suffixed integer constants may be allowed.
The "otherwise" marked in red in the quote includes at least the d, o, and x conversion specifiers. And the bullet point marked in red is the requirement disedorgue's code relied upon. The requirement has been there since 1992, but in early versions of the standard, it was ambiguous as to whether or not the bullet point in red also applied to the floating-point conversion specifiers.
These 2 Users Gave Thanks to Don Cragun For This Post:
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