Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep match lines with n leading spaces

I'm getting stuck on this one. I want to match all lines that start with exactly, say, 8 spaces and then a double quote mark.

cat file.txt | grep '[[:space:]]\{8\}"'

What am I doing wrong there? It's matching lines that start with more than 8 spaces also.

like image 206
mark Avatar asked Jan 12 '13 04:01

mark


People also ask

How do you grep with spaces?

If you want to use a regex it depends on which type, but generally (grep regex and PCRE) it is ”\s” this will match a space or tab. For instance: grep -P ”(:?

What does \s mean in grep?

' \s ' Match whitespace, it is a synonym for ' [[:space:]] '. ' \S ' Match non-whitespace, it is a synonym for ' [^[:space:]] '. For example, ' \brat\b ' matches the separate word ' rat ', ' \Brat\B ' matches ' crate ' but not ' furry rat '.

How to escape special characters in grep command?

If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash ( \ ) in front of the character.


1 Answers

cat file.txt | grep '^[[:space:]]\{8\}"'

If you don't put ^, it will match 8 spaces which is near to your ".

like image 140
Suku Avatar answered Sep 22 '22 01:09

Suku