[SOLVED] Search for a number in a variable with numbers
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
In a variable with numbers where some could be repeated , i need to search for a specific number and get the output if exists or not in the main variable .
here it is an example :
Code:
#!/bin/bash
var="17394958273840"
echo -n "Write a number to search in $var : "
read -r out
output=$(echo $var | grep "\<$out\>")
if [[ -z "$output" ]]
then
echo "Number $out does not exist in $var"
else
echo "Number $out exist in $var"
fi
I am trying with grep , but it could be with awk or sed in case you know how to do it with those .
#!/bin/bash
var="17394958273840"
echo -n "Write a number to search in $var : "
read -r out
output=$(echo $var | grep "\<$out\>")
if [[ -z "$output" ]]
then
echo "Number $out does not exist in $var"
else
echo "Number $out exist in $var"
fi
Not sure why you're enclosing $out in < >...change that line to
Found the solution .
-i switch must be used in grep to achieve this goal .
Code:
#!/bin/bash
var="17394958273840"
echo -n "Write a number to search in $var : "
read -r out
output=$(echo $var | grep -i "$out")
if [[ -z "$output" ]]
then
echo "Number $out does not exist in $var"
else
echo "Number $out exist in $var"
fi
right here it does not find the number without the -i switch .
Anyway , i did some new additional changes to the script , that will also count the number of numbers that exist in the main variable from user input .
Code:
#!/bin/bash
#Main variable
var="17394958273840"
#User input
echo -n "Write a number to search in $var : "
read -r out
# Search for user input
output=$(echo $var | grep -i "$out")
#Count number of characters in main variable
ori=$(echo $var | wc -c)
#Remove from main variable the user input and count the characters
nmb=$(echo $var | sed -r "s#$out##g" | wc -c)
#Subtract from main variable the user input variable and get the #difference
count=$(expr $ori - $nmb)
# check if in search on main variable is empty or not
if [[ -z "$output" ]]
then
# search is empty (Means that number does not exist)
echo "Number $out does not exist in $var"
else
# The number exist in main variable
echo "Number $out exist in $var and exist $count time/s"
fi
Maybe you need to advise of your version of grep, as I fail to see how ignoring the case would make searching for a number any better?
Here is a simpler alternative:
Code:
#!/usr/bin/env bash
var=17394958273840
read -r -p "Write a number to search in $var : " out
if [[ "$var" == "${var/$out}" ]]
then
echo "Number $out does not exist in $var"
else
echo "Number $out does exist in $var"
fi
Last edited by grail; 12-27-2017 at 04:26 AM.
Reason: Added code
#bin/bash
var="17394958273840"
#User input
echo -n "Write a number to search in $var : "
read -r out
#substring search
[[ "$var" =~ "$out" ]] && echo "found it" || echo "not there"
POS=0
LEN=1
#gets amount of chars in string
y="${var//^[0-9]}"
#assigns the amount to x
x=${#y}
while [ "$x" -gt $POS ] ; do
getSeperateChars="${var:$POS:$LEN}"
echo "$getSeperateChars"
#increments position in string being searched
POS=$((POS+=1))
done
BASH Does not have a true ternary operator, but this works for this example. if else statements for the particular. ( my disclaimer)
results
Code:
userx@slackwhere:~
$ ./substring
Write a number to search in 17394958273840 : 3
found it
1
7
3
9
4
9
5
8
2
7
3
8
4
0
Now all you'd have to do is find the amount of times a same number occurs by whatever means necessary.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.