Text Processing – Grep Up to 3 Spaces Using \s

grepregular expressiontext processing

According to the following tutorials

  1. https://linuxize.com/post/regular-expressions-in-grep/

\s Match a space.

and

  1. https://www.guru99.com/linux-regular-expressions.html

Some interval regular expressions are:

Expression Description

{n} Matches the preceding character appearing 'n' times exactly

{n,m} Matches the preceding character appearing 'n' times but not more than m

{n, } Matches the preceding character only when it appears 'n'
times or more

Sample file

wolf@linux:~$ cat space.txt
0space
1 spaces
2  spaces
3   spaces
4    spaces
wolf@linux:~$ 

I just want to grep up to 3 spaces only, minimum 1 space, maximum 3 spaces
Unfortunately, it doesn't really work as expected.

wolf@linux:~$ cat space.txt | grep -P '\s{1,3}'
1 spaces
2  spaces
3   spaces
4    spaces
wolf@linux:~$ 

wolf@linux:~$ cat space.txt | grep -P '\s{3}'
3   spaces
4    spaces
wolf@linux:~$ 

wolf@linux:~$ cat space.txt | grep -P '\s{3,3}'
3   spaces
4    spaces
wolf@linux:~$ 

wolf@linux:~$ cat space.txt | grep -P '\s{0,3}'
0space
1 spaces
2  spaces
3   spaces
4    spaces
wolf@linux:~$ 

Desired Output

wolf@linux:~$ cat space.txt | grep -P '\s{0,3}' <- need to fix it here
1 spaces
2  spaces
3   spaces
wolf@linux:~$ 

Best Answer

you need:

grep -P '\S\s{1,3}\S' infile

\s matches a whitespace-character, not only a space.
\S matches a non-whitespace-character

in your attempt, you are not limiting that before &after your matches should not be a whitespace.


to filter on space only and avoid using PCRE, you can do:

grep '[^ ] \{1,3\}[^ ]' infile

or to work on lines having leading/trailing 1~3spaces:

grep '\([^ ]\|^\) \{1,3\}\([^ ]\|$\)' infile

from https://regexper.com/

input data (cat -e infile):

0space$
1 spaces$
2  spaces$
3   spaces$
4    spaces$
   3spaces$
    4space$
3spaces   $
4spaces    $

output:

1 spaces$
2  spaces$
3   spaces$
   3spaces$
3spaces   $
Related Question