LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Search for a number in a variable with numbers (https://www.linuxquestions.org/questions/programming-9/search-for-a-number-in-a-variable-with-numbers-4175620351/)

pedropt 12-26-2017 06:12 PM

Search for a number in a variable with numbers
 
Objective :

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 .

grail 12-26-2017 07:30 PM

What is the error?

ShadowCat8 12-26-2017 07:41 PM

Greetings,

Well, what you have looks pretty good, except... Why are you offsetting $out when filling the output variable?

I think it should work just fine like this:
Code:

output=$( echo $var | grep $out )
HTH. Let us know.

scasey 12-26-2017 07:42 PM

Quote:

Originally Posted by pedropt (Post 5798170)
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


Not sure why you're enclosing $out in < >...change that line to
Code:

output=$(echo $var | grep "$out")
and the script will work.

pedropt 12-27-2017 02:48 AM

Code is not working because grep is looking to the whole number instead to each one of them .

with that code try to search for 0 (which exist in the variable) .
The output is that number does not exist when in reality exist .

I already changed grep to ( grep "$out" ) .

If the variable was like this :

Quote:

var="1 7 3 9 4 9 5 8 2 7 3 8 4 0"
then my code works fine on it .

But since the variable is close together :

Quote:

17394958273840
then it does not work .

pedropt 12-27-2017 03:28 AM

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


TheEzekielProject 12-27-2017 03:42 AM

Your script with changes suggested regarding "grep $out", and it works perfectly fine on my system

pedropt 12-27-2017 03:56 AM

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


pan64 12-27-2017 04:09 AM

that is overkill. As I told you already use [ ]:
Code:

echo $var | grep -q "[$out]" && echo found || echo not found

grail 12-27-2017 04:16 AM

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


scasey 12-27-2017 11:39 AM

Mayhaps you don't understand what the grep will return when there's a match?
Code:

output=$(echo $var | grep "$out")
will return the value of $var -- the entire string -- in $output when $out is present in $var.

BW-userx 12-27-2017 11:53 AM

substring
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) :D
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.

pedropt 12-28-2017 12:48 PM

thanks everyone , i forgot to set this topic as solved .

grail 12-28-2017 06:33 PM

Please use the Thread Tools to change to SOLVED instead of changing the subject

pedropt 12-29-2017 02:09 AM

thanks , i did not knew that option .


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