grepping a specific word instead of the whole line

L

liadsh

Guest
Hey,

i have this script (just the beggining)

read porta
read portb
#awk -F ' ' '{print $1,$2}' /var/log/tmp_num.txt&
echo "<$porta,$portb>"
grep -Fx "<$porta,$portb>" /var/log/table.def.temp
if [ "<$porta,$portb>" = 'grep -Fxq "<$porta,$portb>" /var/log/table.def.temp' ]
then
echo "exist"
else
echo "does not exist"
fi

the file contains the following:
{ <4500,17>, <500, 17>, <259, 17>, <1701, 17>, <5500, 17>, <68,17>, <67,17>};
{ <4500,17>, <500, 17>, <259, 17>, <1701, 17> };

what i'm trying to do is, lets say i enter 68 as porta and 17 as portb, match this sintax in the /var/log/table.def.temp file and then use it in the if loop. i saw in a few articles that grep -Fxq should catch only a specific word but it doesn't catch anything.
after this is accomplished i will add other stuff and not just echo.

thanks :)
 


I'm still not entirely clear what you are trying to do in this script, but I know that grep -Fxq will not output anything to the screen because of the q switch.

If you take a look at the man page for grep and look at what -q does you will see that:
-q causes grep to become silent. No output is written to stdout. As soon as it finds a match it will exit immediately with a return value/status of 0;

So with the options -Fxq:
-F will interpret the search pattern as a list of fixed strings;
-x will only select results where the entire line in the file being searched exactly matches the search string;
and - q will cause grep to become silent, so you will not see any matches, but it will return 0 (success) or 1(failure).

So if you are going to use q, you just need to use the exit code of grep to determine whether the string was found or not. So that would just be a case of putting the call to grep into an if, then else statement.

Regarding this:
the file contains the following:
{ <4500,17>, <500, 17>, <259, 17>, <1701, 17>, <5500, 17>, <68,17>, <67,17>};
{ <4500,17>, <500, 17>, <259, 17>, <1701, 17> };

Is this the file that you are searching through using grep -Fxq? If so, you might want to remove the x option and use -Fq instead.

To illustrate my point, if you were searching for <68, 17> (which we can see in the 1st line of the file), if you use:
Code:
grep -Fxq "<68,17>" ./filename
echo $?
You will see from the echo that grep returned 1 (failure) because although <68,17> is in the file, the x switch causes grep to reject that result because the search string doesn't match the entire line which is:
{ <4500,17>, <500, 17>, <259, 17>, <1701, 17>, <5500, 17>, <68,17>, <67,17>};

However, using -Fq:
Code:
grep -Fq "<68,17>" ./filename
echo$?
grep should find the instance of <68,17> at line 1 and will return 0 (success).

So perhaps this snippet will help you:
Code:
if grep -Fq "<$porta,$portb>" /var/log/table.def.temp; then
    echo "exists";
else
    echo "does not exist";
fi
 
Last edited:
it works now, so basically the grep -F would do and the "x" was not needed in this case , thanks for the clarification.
btw i checked the grep man pages but now i understand what x does.
 

Members online


Top