Skip to main content
add regex graphic
Source Link
αғsнιη
  • 42k
  • 17
  • 76
  • 119

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   $

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

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   $

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   $
added 372 characters in body
Source Link
αғsнιη
  • 42k
  • 17
  • 76
  • 119

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

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   $

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

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

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   $
Source Link
αғsнιη
  • 42k
  • 17
  • 76
  • 119

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