LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read digit by digit in variable (https://www.linuxquestions.org/questions/programming-9/read-digit-by-digit-in-variable-4175620239/)

pedropt 12-24-2017 12:00 PM

Read digit by digit in variable
 
I have been looking all over the web to a simple solution for this , but none of them solved my issue , specially related to a variable and not a file .

The objective is to count the number of digits in a specific variable , and then display one by one .

A quick start

- first i need to count the number of digits in variable
- second i must set a loop with the number of digits and then start displaying them

Code:

#!/bin/bash

#this is the variable
var1=28173

# this is to get the number of digits in that variable
nmbd=$(echo $var1 | awk -F '[0-9]' '{print NF-1}')

# this is the loop
for i in (seq 0 $nmbd);
do

# Thins command was supposedly to read digit by digit
# But is not working
read -n1 out < "$var1"

#This will display the output number by number
# out variable is from the read command
echo $out
done

I have issues in the read command that is not working because it is expecting that $var1 is a file but instead it is a variable .

NevemTeve 12-24-2017 12:34 PM

You don't really want to 'read', you want to get a substring from a string. Something like this:

Code:

Var='258J'
for i in $(seq ${#Var}); do
    echo "_${Var:$i-1:1}_"
done


astrogeek 12-24-2017 12:38 PM

Quote:

Originally Posted by pedropt (Post 5797295)
I have been looking all over the web to a simple solution for this , but none of them solved my issue , specially related to a variable and not a file

Look for Here Strings in the bash man page. But that is probably not how you want to do it in this case.

Quote:

Originally Posted by pedropt (Post 5797295)
The objective is to count the number of digits in a specific variable , and then display one by one

Looking at your code I conclude that you only need the number of digits in order to iterate over the digits, and that your objective is to output the digits of the number one per line, left to right.

If that is correct then you don't need the count at all and can simply use the shell's built in substitution operators to iterate the number, among other ways. Perhaps something like this (I leave the loop test as an exercise)...

Code:

var=54321X
while [ variable not null ]; do
        echo "${var:0:1}"
        var=${var#?}
done

Note: I have modified the above to safely handle non-digit characters after seeing NevemTeve's example.

!!! 12-24-2017 01:19 PM

Try a 'here string' for the read: https://unix.stackexchange.com/quest...-variable-bash
Try: echo $var | fold -s1
For #digits, Append: | wc -l

danielbmartin 12-24-2017 03:24 PM

You might like this ...
Code:

fold -w1 <<<"28173"
... or this ...
Code:

grep -o . <<<"28173"
Daniel B. Martin

.

NevemTeve 12-24-2017 11:47 PM

Untested:
Code:

printf '%s' "$var" | sed $'s/./&\n/g' | while read Chr do;
...
done


pedropt 12-25-2017 04:24 AM

Thanks all , NevemTeve example worked great .


All times are GMT -5. The time now is 04:04 AM.