LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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: 345

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: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
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,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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: 345

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


Reply



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 10:46 AM.

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
Open Source Consulting | Domain Registration