Linux or Bash syntax that evaluates to the next ASCII character

I have a Linux machine ( Red Hat Linux ), and I need to add the following task to my Bash script.

Which Linux command or Bash syntax will calculate the next ASCII character?

Note. The command syntax can also be AWK / Perl, but this syntax should be in my Bash script.

Example:

input results a --> the next is b c --> the next is d A --> the next is B 
+7
source share
5 answers

The Perl ++ statement also processes strings:

 perl -nle 'print ++$_' 

Here we need the -l option with startup, since a\n , for example, otherwise returns 1 .

+4
source

Use translation ( tr ):

 echo "aA mM yY" | tr "a-yA-Y" "b-zB-Z" 

He prints:

bB nN zZ

+8
source

You can use the chr() and ord() functions for Bash (see How to convert an ASCII character to its decimal (or hexadecimal) value and vice versa? ):

 # POSIX # chr() - converts decimal value to its ASCII character representation # ord() - converts ASCII character to its decimal value 
+3
source
 perl -le "print chr(ord(<>) + 1)" 

Interactive:

 breqwas@buttonbox :~$ perl -le "print chr(ord(<>) + 1)" M N 

Non-interactive:

 breqwas@buttonbox :~$ echo a | perl -le "print chr(ord(<>) + 1)" b 
0
source

Symbol value:

 c="a" 

To convert a character to its ASCII value:

 v=$(printf %d "'$c") 

The value you want to add to this ASCII value:

 add=1 

To change its ASCII value by adding $ add to it:

 ((v+=add)) 

To convert the result to char:

 perl -X -e "printf('The character is %c\n', $v);" 

I used -X to disable all warnings


You can combine all this into one line and put the result in vairable $ r:

 c="a"; add=1; r=$(perl -X -e "printf('%c', $(($add+$(printf %d "'$c"))));") 

you can print the result:

 echo "$r" 

You can make a function to return the result:

 achar () { c="$1"; add=$2 printf "$(perl -X -e "printf('%c', $(($add+$(printf %d "'$c"))));")" } 

you can use the function:

 x=$(achar "a" 1) // x = the character that follows a by 1 

or you can do a loop:

 array=( akmo ) for l in "${array[@]}" do echo "$l" is followed by $(achar "$l" 1) done 
0
source

All Articles