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.
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 ”(:?
' \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 '.
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.
cat file.txt | grep '^[[:space:]]\{8\}"'
If you don't put ^
, it will match 8 spaces which is near to your "
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With