Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am looking for a bash script that would read a string
sdfsfsdf123
and output digits - 123
and the opposite, read the string and input digits in place of the current digits.
thanks to your request i got fun again writing these new scripts
(a)
Code:
#!/bin/bash
echo -n "input new string with numbers: "
read string
# get the digits, there are many ways to do this
digits=$(echo -n "$string" | sed 's/[^1-9]//g')
# count the number of digits
dcount=$(echo -n "$digits" | wc -c)
# check digit count
if [[ dcount -eq 0 ]]; then
echo "there where no digit found in the string"
exit 1
fi
# print the digits
echo "digits found: $digits"
# ask for a replacement
until echo -n "please enter a $dcount digit number to replace the digits: ";
read rdigits;
[ ${#rdigits} -eq $dcount ] && [[ $rdigits =~ ^[[:digit:]]+$ ]]; do
echo "that's not a 3 digit number!"
done
# split the strings to arrays
stringarray=( $(echo "$string" | sed 's/./& /g') )
rdigitsarray=( $(echo "$rdigits" | sed 's/./& /g') )
# print
rpos=0
for a in ${stringarray[*]}; do
if [[ $a =~ [[:digit:]] ]]; then
echo -n ${rdigitsarray[b++]}
else
echo -n ${a}
fi
done
echo
(b)
Code:
#!/bin/bash
until
# ask for strings
echo -n "input new string with numbers: "
read string
# get the digits, there are many ways to do this
digits=$(echo -n "$string" | sed 's/[^0-9]//g')
# count the number of digits
dcount=$(echo -n "$digits" | wc -c)
# check digit count
[[ dcount -gt 0 ]]; do
echo "there where no digit found in the string"
done
# print the digits
echo "digits found: $digits"
# ask for a replacement
until echo -n "please enter a number to replace the digits: ";
read rdigits;
[ ${#rdigits} -gt 0 ] && [[ $rdigits =~ ^[[:digit:]]+$ ]]; do
echo "that's not a number!"
done
# split the strings to arrays
stringarray=( $(echo "$string" | sed 's/./& /g') )
rdigitsarray=( $(echo "$rdigits" | sed 's/./& /g') )
# print
rpos=0
rcount=${#rdigitsarray[@]}
spos=0
scount=${#stringarray[@]}
while [[ spos -lt scount && rpos -lt rcount && rpos -lt dcount ]]; do
if s=${stringarray[spos++]}; [[ ${s} =~ [[:digit:]] ]]; then
echo -n "${rdigitsarray[rpos++]}"
else
echo -n "${s}"
fi
done
while [[ spos -lt scount ]]; do
echo -n "${stringarray[spos++]}"
done
while [[ rpos -lt rcount ]]; do
echo -n "${rdigitsarray[rpos++]}"
done
echo
The first code is strict to the actual number of digits you put in the first string
For the second one, as long as you enter a number to replace the string, it will use it.
hope this can help
regards
Edit:
btw if you want to append the extra digits of the replace string to the last digit of the first string instead of the last character, you just have to reverse the last parts of the code to:
Code:
while [[ rpos -lt rcount ]]; do
echo -n "${rdigitsarray[rpos++]}"
done
while [[ spos -lt scount ]]; do
echo -n "${stringarray[spos++]}"
done
Last edited by konsolebox; 03-13-2008 at 09:44 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.