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
Solution 1. let year = 'II'; let sem = 'I'; let regex = new RegExp(`${year} B. Tech ${sem} Sem`, "g"); You need to pass the options to the RegExp constructor, and remove the regex literal delimiters from your string.
Note: Regex can be created in two ways first one is regex literal and the second one is regex constructor method ( new RegExp() ). If we try to pass a variable to the regex literal pattern it won't work. The right way of doing it is by using a regular expression constructor new RegExp() .
So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.
But, what if you want to grep a string in a variable? If you pass a variable as an argument to grep, you will get an error (or several if your variable contains spaces). This happens because the variable is expanded by the shell. When expanded, grep recognizes it as multiple arguments where a filename should be.
Just Bash
searchfile="file"
read searchterm
shopt -s nocasematch
while read -r line
do
case "$line" in
*"$searchterm"*";"* ) echo "$line";;
esac
done < "$searchfile"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With