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 |
What is the error?
|
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 ) |
Quote:
Code:
output=$(echo $var | grep "$out") |
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:
But since the variable is close together : Quote:
|
Found the solution .
-i switch must be used in grep to achieve this goal . Code:
#!/bin/bash |
Your script with changes suggested regarding "grep $out", and it works perfectly fine on my system
|
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 |
that is overkill. As I told you already use [ ]:
Code:
echo $var | grep -q "[$out]" && echo found || echo not found |
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 |
Mayhaps you don't understand what the grep will return when there's a match?
Code:
output=$(echo $var | grep "$out") |
substring
Code:
#bin/bash results Code:
userx@slackwhere:~ |
thanks everyone , i forgot to set this topic as solved .
|
Please use the Thread Tools to change to SOLVED instead of changing the subject
|
thanks , i did not knew that option .
|
All times are GMT -5. The time now is 03:18 AM. |