0

I have a file that has random lines of text. My goal is to find a line which occurs exactly n times. So, I decided to:

sort [file] | uniq -c 

After executing these commands, I can see that there is some leading whitespace on each line, followed by the count (from uniq).

Now, when I was testing grep (I am new to learning grep, so I wanted to take it step-by-step), this command

sort [file] | uniq -c | grep \s

returned those lines that had the letter 's' in them. But as per my learning, \s should represent a whitespace character.

But when I quote the \s,

sort [file] | uniq -c | grep '\s'

it seems like the leading whitespace character on each line is being matched (because 's' characters aren't being highlighted).

My question is, why do I have to quote the \s (supposing '\s' is indeed matching the leading whitespace character). Why doesn't \s work; why is it evaluating to the letter 's'?

Edit: Even \s\+ doesn't yield the expected behavior unless it is enclosed within single quotes. Why do I have to quote the expression for it to work as expected? What do single quotes have to do with this?

  • **Because you're using a shell.** When you type input (possibly a command) to a shell, the shell processes that input first, and then if (and only if) the input specifies a program to be run, it runs that program and passes arguments to it. When you don't quote `\s` the shell interprets the backslash and passes only `s` to `grep`, so `grep` only searches for `s`. Doublequote `"\s"` would also work here; although singlequote and doublequote in shell differ in other respects they are the same here. Ordinary shell use is not programming and is offtopic for SO. – dave_thompson_085 Mar 13 '21 at 11:27
  • Ah! Thank you, @dave_thompson_085. Didn't realize it was because of the escape character. Sorry, I am a bit new to SO and SE too, so didn't realize I was putting question in the wrong place. –  Mar 13 '21 at 11:39

0 Answers0