grep'ing a variable that contains a metacharacter ($) with a while loop

This is driving me crazy, and I'm hoping someone can help me out with this. I'm trying to do a simple while loop to go through a log file. I'm pulling out all of the lines with a specific log line, getting an ID from that line, and once I have a list of IDs I want to loop back through the log and check for the times that this line has happened.

This is what I've got so far.

awk -F\= '/Asset could not be deleted because it does not exist: asset_id=/ {print $2}' manager.log|head
2_1900071424$41211333
2_1900071424$41211333
2_1900071424$8852047
2_1900071424$8852047
2_1900071552$9216019
2_1900071552$9216019
2_1900071424$9338764
2_1900071552$8026494
2_1900071552$9021614
2_1900071680$8770799

So this gives me a list of asset IDs. The problem is, lets say now I want to loop through the log and grep for these. No matter what I do I can't seem to make it work.

I've tried:

[root@mgmt01 logs]# awk -F\= '/Asset could not be deleted because it does not exist: asset_id=/ {print $2}' manager.log|while read i;do grep $i manager.log;done
grep: Unmatched [ or [^
grep: Unmatched [ or [^
grep: Unmatched [ or [^
grep: Unmatched [ or [^
grep: Unmatched [ or [^

But that doesn't work. I tried a bunch of combinations of single and double quotes and that didn't work. I even got creative and tried splitting up each variable into an array and then combining it and grep'ing for that...

[root@mgmt01 logs]# awk -F\= '/Asset could not be deleted because it does not exist: asset_id=/ {print $2}' manager.log|while read i;do IFS="$";i=($i);grep  ""${i[0]}"$"${i[1]}"" manager.log ;done
grep: Unmatched [ or [^
grep: Unmatched [ or [^
grep: Unmatched [ or [^
grep: Unmatched [ or [^
grep: Unmatched [ or [^

But that didn't work at all either. Frankly, at this point I'm getting a little frustrated. I've actually found a way to get the information I needed, but I'm sure at some point I'm going to want to loop through this log.

Any advice?

awk -F\= '/Asset could not be deleted because it does not exist: asset_id=/ {print $2}' manager.log > patternfile

grep -F -f patternfile [file I want to search]

-F turns regexp expansion off

1 Like

Yep, that did it. There's more to it than just comparing the two files, but the -F did the trick.

Thanks Jim.