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
1

6 Answers 6

3

Just Bash

searchfile="file"
read searchterm
shopt -s nocasematch
while read -r line
do
    case "$line" in
        *"$searchterm"*";"* ) echo "$line";;
    esac
done < "$searchfile"
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

thanks, also: Is there any way to escape all spaces, brackets, etc.? So i can put in searchterm the string ".*"
Didn't get you. What forbids you to enter .* when being asked for searchterm?
if i enter ".*", it matches all the line
Remove .*$ in the grep_params, then it will match up to the ";"
|
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.

Comments

0

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

"black\seyed\speas"

1 Comment

This will also match other whitespace characters, which may not be what is intended.
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

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.