shade917
shade917

Reputation: 105

Cut the GREP command in Linux

Okay so i have a variable called search which holds the string "Find this Code - Now". I want to search for that code and if it finds it the system would reply with true or something on those lines, if it doesn't then it just exits the script with error.

I tried to use grep but i couldn't figure out how to cut only what was searched for so i could run an if else statement

Upvotes: 0

Views: 1529

Answers (3)

drfloob
drfloob

Reputation: 3244

> X="not gonna find it"
> grep -qR "$X" .; echo $?
1
> X="Find this Code - Now"
> grep -qR "$X" .; echo $?
0

Upvotes: 0

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

You can use the -q switch and check the exit status of grep, i.e. something like grep -q $var <file> && echo "true".

Upvotes: 2

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

I tried to use grep but i couldn't figure out how to cut only what was searched for so i could run an if else statement

Grep has a switch --only-matching

 -o, --only-matching       show only the part of a line matching PATTERN

Upvotes: 1

Related Questions