LinuxQuestions.org
Visit Jeremy's Blog.
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-26-2017, 06:12 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 134

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

Last edited by pedropt; 12-29-2017 at 02:09 AM.
 
Old 12-26-2017, 07:30 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,592

Rep: Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915
What is the error?
 
Old 12-26-2017, 07:41 PM   #3
ShadowCat8
Member
 
Registered: Nov 2004
Location: Ontario, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 250

Rep: Reputation: 52
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.
 
Old 12-26-2017, 07:42 PM   #4
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 766

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by pedropt View Post
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.
 
Old 12-27-2017, 02:48 AM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 134

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

Last edited by pedropt; 12-27-2017 at 03:19 AM.
 
Old 12-27-2017, 03:28 AM   #6
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 134

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-27-2017, 03:42 AM   #7
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: Devuan
Posts: 554

Rep: Reputation: 155Reputation: 155
Your script with changes suggested regarding "grep $out", and it works perfectly fine on my system
 
Old 12-27-2017, 03:56 AM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 134

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

Last edited by pedropt; 12-27-2017 at 04:05 AM.
 
Old 12-27-2017, 04:09 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,516

Rep: Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121Reputation: 3121
that is overkill. As I told you already use [ ]:
Code:
echo $var | grep -q "[$out]" && echo found || echo not found
 
Old 12-27-2017, 04:16 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,592

Rep: Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915
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
 
Old 12-27-2017, 11:39 AM   #11
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 766

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
Old 12-27-2017, 11:53 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Void Linux / Peppermint Eight
Posts: 5,926

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

Last edited by BW-userx; 12-27-2017 at 12:31 PM.
 
Old 12-28-2017, 12:48 PM   #13
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 134

Original Poster
Rep: Reputation: Disabled
thanks everyone , i forgot to set this topic as solved .
 
Old 12-28-2017, 06:33 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,592

Rep: Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915Reputation: 2915
Please use the Thread Tools to change to SOLVED instead of changing the subject
 
Old 12-29-2017, 02:09 AM   #15
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 134

Original Poster
Rep: Reputation: Disabled
thanks , i did not knew that option .
 
  


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] bash won't search within a variable with a user imputed variable hoisu Programming 8 04-02-2014 12:11 PM
how to find the 1 number out of 10 numbers, if I have taken 9 numbers from my brain ? rpittala Linux - Newbie 4 01-30-2012 05:40 PM
Get a number between two numbers question HarryBoy Programming 11 02-02-2010 11:46 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

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

All times are GMT -5. The time now is 03:42 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