How to do grep of string (which contains file directory path as value ) in a input file

Viewed 34

I have string value as /tmp/Sne/test1.list and I have a file called all_tests.txt

This all_tests.txt contains lines below:

/tmp/Sne/test1.list
/tmp/Sne/test2.list
/tmp/Sne/test3.list
/tmp/Sne/test4.list

How to use grep to search if string value /tmp/Sne/test1.list is present in the all_tests.txt. If present then I need to echo a message that file is present.

I tried using:

if [ grep -Fxq "$p" $Input_file ]; then echo "This file list is already checked"

where $p is /tmp/Sne/test1.list and $Input_file is all_tests.txt

This is not working.

Kindly request for help.

1 Answers

You are almost there:

if grep -Fxq "$p" "$Input_file"; then echo "This file list is already checked"; fi

You were mixing two commands: [ (man test) and grep.

PS: surround your variables with double quotes ($Input_file => "$Input_file") unless you are certain you must not.

Related