Implement Ord function function in awk _linux shell

Source: Internet
Author: User
Tags ord sprintf

Ord functions are not provided directly in awk, so when you convert a character to a code, you need to implement it yourself

Copy Code code as follows:

awk ' begin{for (i = 0; i < 256 ++i) {ord[sprintf ("%c", i)] = i;}} '  ' {print ord[' a ']; Ord [' B ']; }'

This mainly constructs the data structure of the Ord for Key,val, the key is Char character, and Val corresponds to the ASCII code

With: awk to get the letter of the ASC value implementation

Copy Code code as follows:

-(Dearvoid@linuxeden:forum)-(~/tmp)-
[$$=18347 $?=0]; Cat Foo.awk
#!/bin/awk-f

BEGIN {
for (ii = 0; II < 256; ++ii) {
ch = sprintf ("%c", ii);
Ascii[ch] = II;
}

for (ii = 1; II < ARGC; ii++) {
Print "Ord" ("argv[ii]" = "ascii[argv[ii]";
}
}
-(Dearvoid@linuxeden:forum)-(~/tmp)-
[$$=18347 $?=0];/foo.awk a b C 1 2 3
Ord (a) = 97
Ord (b) = 98
Ord (c) = 99
Ord (1) = 49
Ord (2) = 50
Ord (3) = 51
-(Dearvoid@linuxeden:forum)-(~/tmp)-
[$$=18347 $?=0]; Bye

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.