7

i'm trying to get working the following code:

searchfile="availables.txt"
read searchterm
grep_params="-i ^.*${searchterm}.*;.*$' $searchfile"
egrep $grep_params

which should echo all lines beginning with the $searchterm and followed by ";". But if the searchterm contains spaces it doesn't work (eg: "black eyed peas"), it gives me the following output:

egrep: eyed: No such file or directory
egrep: peas.*;.*$": No such file or directory
egrep: "availables.txt": No such file or directory
Alessio
  • 341
  • 2
  • 5
  • 7

6 Answers6

3

Just Bash

searchfile="file"
read searchterm
shopt -s nocasematch
while read -r line
do
    case "$line" in
        *"$searchterm"*";"* ) echo "$line";;
    esac
done < "$searchfile"
bash-o-logist
  • 6,665
  • 1
  • 17
  • 14
1

You need to control word splitting here. That is done through arrays. See http://mywiki.wooledge.org/WordSplitting

searchfile="availables.txt"
read searchterm
grep_params=(-i "^.*${searchterm}.*;.*$" $searchfile)
egrep "${grep_params[@]}"

But don't use egrep - use grep -E instead, as the first is deprecated. But I would have changed your code like that:

searchfile="availables.txt"
read searchterm
grep_params="^.*${searchterm}.*;.*$"
grep -E -i "$grep_params" $searchfile
Draco Ater
  • 20,820
  • 8
  • 62
  • 86
  • thanks, also: Is there any way to escape all spaces, brackets, etc.? So i can put in searchterm the string ".*" – Alessio Apr 19 '11 at 08:34
  • Didn't get you. What forbids you to enter `.*` when being asked for `searchterm`? – Draco Ater Apr 19 '11 at 08:41
  • if i enter ".*", it matches all the line – Alessio Apr 19 '11 at 08:46
  • Remove `.*$` in the grep_params, then it will match up to the ";" – Draco Ater Apr 19 '11 at 08:49
  • well, if i enter ".*", it should matches only lines that have "something.*something;something". Instead it matches all lines. I would have something that converts the string ".*" into "\.\*", in other words something that escape all the special character like .,\,/,*,$, etc – Alessio Apr 19 '11 at 08:54
  • 1
    http://stackoverflow.com/questions/2933474/escape-characters-contained-by-bash-variable-in-regex-pattern – Draco Ater Apr 19 '11 at 08:58
0

The code you're looking for is something like:

searchfile="availables.txt"
read searchterm
regex="${searchterm}"'.*;'
egrep "${grep_params}" "${searchfile}"

Note that I simplified your regex (I hope I got it right!).

However, as Ignacio Vazquez-Abrar noted, this will fail in complex cases.

static_rtti
  • 53,760
  • 47
  • 136
  • 192
0

As this is s regex, try to replace the " " with \s. This stands for a whitespace character.

"black\seyed\speas"

stema
  • 90,351
  • 20
  • 107
  • 135
0

Alternative way is to pass the params through xargs:

searchfile="availables.txt"
read searchterm
grep_params="-i '^.*${searchterm}.*;.*$' $searchfile"
echo "$grep_params" | xargs egrep
dogbane
  • 266,786
  • 75
  • 396
  • 414
0

I got here by searching for "bash variable in a regex"

I solved it by changing the regex delimiters from "/" to "+"

Even though it has nothing to do with egrep, I'm adding my solution for other people who arrive from similar searches:

SYBASELOG="/opt/sybase/ASE-12_5/install/SYBASE.log"
MAILBODY="Some text and then the replacement placeholder:

   [MSGFILE]

   and some more text"

# Some proecessing...

MAILBODY=`echo "${MAILBODY}" | sed -e "s+\[MSGFILE\]+"${SYBASELOG}"+"`

And yes, I see now that it had little to do with bash and everything to do with the slashes in the log file variable. D'oh!

mmrtnt
  • 382
  • 1
  • 8