简体   繁体   English

在Linux中剪切GREP命令

[英]Cut the GREP command in Linux

Okay so i have a variable called search which holds the string "Find this Code - Now". 好的,所以我有一个名为search的变量,其中包含字符串“ 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. 我想搜索该代码,如果找到该代码,系统将以true或在这些行上的内容进行答复,如果没有,那么它将仅以错误退出脚本。

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,但是我不知道如何只剪切搜索的内容,因此我可以运行if else语句

您可以使用-q开关并检查grep的退出状态,例如grep -q $var <file> && echo "true"

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,但是我不知道如何只剪切搜索的内容,因此我可以运行if else语句

Grep has a switch --only-matching Grep有一个--only-matching开关

 -o, --only-matching       show only the part of a line matching PATTERN
> X="not gonna find it"
> grep -qR "$X" .; echo $?
1
> X="Find this Code - Now"
> grep -qR "$X" .; echo $?
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM