LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2017, 12:00 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 185

Rep: Reputation: Disabled
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 .

Last edited by pedropt; 12-29-2017 at 02:12 AM.
 
Old 12-24-2017, 12:34 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,596

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
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
 
1 members found this post helpful.
Old 12-24-2017, 12:38 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,772
Blog Entries: 6

Rep: Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586Reputation: 2586
Quote:
Originally Posted by pedropt View Post
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 View Post
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.

Last edited by astrogeek; 12-24-2017 at 12:51 PM. Reason: Make subst safe for non-digits
 
2 members found this post helpful.
Old 12-24-2017, 01:19 PM   #4
!!!
Member
 
Registered: Jan 2017
Posts: 666

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

Last edited by !!!; 12-24-2017 at 01:26 PM.
 
Old 12-24-2017, 03:24 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,682

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
You might like this ...
Code:
fold -w1 <<<"28173"
... or this ...
Code:
grep -o . <<<"28173"
Daniel B. Martin

.
 
2 members found this post helpful.
Old 12-24-2017, 11:47 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,596

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
Untested:
Code:
printf '%s' "$var" | sed $'s/./&\n/g' | while read Chr do;
...
done
 
Old 12-25-2017, 04:24 AM   #7
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 185

Original Poster
Rep: Reputation: Disabled
Thanks all , NevemTeve example worked great .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need a one-liner to change one digit in a variable brh1 Linux - General 3 02-15-2016 10:10 AM
mv a 2 digit to name....please help Drigo Linux - Newbie 8 02-11-2013 11:41 PM
[SOLVED] Last digit minus ONE... NetRock Programming 5 04-06-2011 12:39 PM
Substitue single-digit, two-digit, and 3-digit numbers with text using sed dmason165 Programming 13 08-07-2009 10:38 AM
perl: how to insert numerical digit immediately after regexp backreference variable? chadwick Programming 8 05-19-2008 12:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration