How to iterate through all ASCII characters in Bash?

会有一股神秘感。 提交于 2019-12-22 05:53:16

问题


I know how to iterate through alphabets:

for c in {a..z}; do ...; done

But I can't figure out how to iterate through all ASCII characters. Does anyone know how?


回答1:


What you can do is to iterate from 0 to 127 and then convert the decimal value to its ASCII value(or back).

You can use these functions to do it:

# POSIX
# chr() - converts decimal value to its ASCII character representation
# ord() - converts ASCII character to its decimal value

chr() {
  [ ${1} -lt 256 ] || return 1
  printf \\$(printf '%03o' $1)
}

# Another version doing the octal conversion with arithmetic
# faster as it avoids a subshell
chr () {
  [ ${1} -lt 256 ] || return 1
  printf \\$(($1/64*100+$1%64/8*10+$1%8))
}

# Another version using a temporary variable to avoid subshell.
# This one requires bash 3.1.
chr() {
  local tmp
  [ ${1} -lt 256 ] || return 1
  printf -v tmp '%03o' "$1"
  printf \\"$tmp"
}

ord() {
  LC_CTYPE=C printf '%d' "'$1"
}

# hex() - converts ASCII character to a hexadecimal value
# unhex() - converts a hexadecimal value to an ASCII character

hex() {
   LC_CTYPE=C printf '%x' "'$1"
}

unhex() {
   printf \\x"$1"
}

# examples:

chr $(ord A)    # -> A
ord $(chr 65)   # -> 65



回答2:


A possibility using only echos octal escape sequences:

for n in {0..7}{0..7}{0..7}; do echo -ne "\\0$n"; done



回答3:


Here's how you print an integer as its corresponding ascii character with awk:

echo "65" | awk '{ printf("%c", $0); }'

which will print:

A

And here's how you might iterate through the uppercase alphabet this way:

# ascii for A starts at 65:
ascii=65
index=1
total=26
while [[ $total -ge $index ]]
do
    letter=$(echo "$ascii" | awk '{ printf("%c", $0); }')
    echo "The $index'th letter is $letter"

    # Increment the index counter as well as the ascii counter
    index=$((index+1))
    ascii=$((ascii+1))
done



回答4:


Here is what I came up with for a one-liner taking some pieces from sampson-chen's and mata's answers:

for n in {0..127}; do awk '{ printf("%c", $0); }' <<< $n; done

Or alternatively:

for n in {0..127}; do echo $n; done | awk '{ printf("%c", $0); }'



回答5:


Well... If you really want them all, and you want it to be something script-like, you could do this, I guess:

awk 'function utf32(i) {printf("%c%c%c%c",i%0x100,i/0x100%0x100,i/0x10000%0x100,i/0x1000000) } BEGIN{for(i=0;i<0x110000;i++){utf32(i);utf32(0xa)}}' | iconv --from-code=utf32 --to-code=utf8 | grep -a '[[:print:]]'

But the list is pretty huge, and not very useful. Awk may not be the most elegant way of generating the binary integers from 0 to 0x110000 - substitute something more elegant if you find it.

Edit: Oh, I see you only wanted ascii. Well, I will let this answer stay here in case somebody else actually wants all the UTF printable characters.



来源:https://stackoverflow.com/questions/13127950/how-to-iterate-through-all-ascii-characters-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!