4

I would like to grep the word “s a i” (which has spaces in it) in a xyz.txt file 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 grep command only.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Sai Srikanth
  • 41
  • 1
  • 1
  • 4
  • There is zero problems grepping with spaces. There is a possible problem passing an argument with spaces in shell. Until you provide an examples and errors you encounter, it is hard to tell what answer should look like. – myaut Nov 22 '17 at 06:52
  • Just in case it helps the next person - I was searching for two words as well and found that the second word was not capitalized as I'd expected. Something else to double check if you've landed here like me. – DJ Quimby May 25 '21 at 21:12

3 Answers3

5

You can escape the space with a backslash:

grep My\ Documents
juanmah
  • 1,130
  • 1
  • 14
  • 21
4

Just add quotes around your grep command:

grep "s a i" "another directory/xyz.txt"
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
0

You can use regular expression:-

grep -E "(s a i)" a.txt
Buddy
  • 10,874
  • 5
  • 41
  • 58
yadav_1992
  • 43
  • 2
  • 11