Reputation: 77
I have a variable set to a specific number, i.e. num=56 and a file full of numbers and other strings. I would like to match both the number and some of the string and get the entire line which contains them.
For example,
I want to match this is my number: $num [mbits] in a file full of similar lines such as the below.
random text in front - this is my number: 56 [mbits]
random text in front - this is my number: 21 [mbits]
random text in front - this is my number: 15 [mbits]
random text in front - this is my number: 38 [mbits]
random text in front - this is my number: 49 [mbits]
random text in front - this is my number: 10 [mbits]
random text in front - this is my number: 89 [mbits]
I am using the below command, but it does not work when I include the entire string.
grep -n "this is my number: $num [mbits]" info.log >> info1.log
The file remains empty.
It works only as grep -n "this is my number:" info.log >> info1.log and I get all the lines as above. I just need the one with the specific number.
The desired output should be,
random text in front - this is my number: 56 [mbits]
Any help?
Thank you in advance.
Upvotes: 1
Views: 68
Reputation: 784898
You need to use grep -F
(fixed string match):
num=56
grep -F "this is my number: $num [mbits]" info.log
Or else:
grep "this is my number: $num \[mbits\]" info.log
As [
and ]
are special regex characters than need to be escaped.
Output:
random text in front - this is my number: 56 [mbits]
Upvotes: 2