Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep for a variable content with a dot

Tags:

grep

bash

i found many similar questions about my issue but i still don't find the correct one for me. I need to grep for the content of a variable plus a dot but it doesn't run escaping the dot after the variable. For example: The file content is

item.
newitem.

My variable content is item. and i want to grep for the exact word, therefore I must use -w and not -F but with the command I can't obtain the correct output:

cat file | grep -w "$variable\."

Do you have suggestions please?

Hi, I have to rectify my scenario. My file contains some FQDN and for some reasons I have to look for hostname. with the dot. Unfortunatelly the grep -wF doesn't run: My file is

hostname1.domain.com
hostname2.domain.com

and the command

cat file | grep -wF hostname1.

doesn't show any output. I have to find another solution and I'm not sure that grep could help.

like image 973
intore Avatar asked Oct 23 '25 15:10

intore


1 Answers

If $variable contains item., you're searching for item.\. which is not what you want. In fact, you want -F which interprets the pattern literally, not as a regular expression.

var=item.
echo $'item.\nnewitem.' | grep -F "$var"
like image 130
choroba Avatar answered Oct 26 '25 05:10

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!