bash 我想要 grep 来 grep 一个有空格的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47428445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:37:12  来源:igfitidea点击:

I want grep to grep one word which is having spaces it

linuxbashshellgrep

提问by Sai Srikanth

I would like to grep the word “s a i”(which has spaces in it) in a xyz.txtfile which is saved in another directory. I tried to find an answer but I didn't manage to find any. I have to use the grepcommand only.

我想在另一个目录中保存的文件中grep 单词“sai”(其中有空格)xyz.txt。我试图找到答案,但我没有找到任何答案。我只需要使用grep命令。

回答by juanmah

You can escape the space with a backslash:

您可以使用反斜杠转义空格:

grep My\ Documents

回答by Ronan Boiteau

Just add quotes around your grepcommand:

只需在grep命令周围添加引号:

grep "s a i" "another directory/xyz.txt"

回答by yadav_1992

You can use regular expression:-

您可以使用正则表达式:-

grep -E "(s a i)" a.txt