0

I am trying to grep for a word in a directory - but I want to check for numbers 1-9 in the word.

This is a simple grep:

grep -r L01auxillar /pr/solder/

I want the grep command to search: grep -r L0[1-9]auxillar /pr/solder/

PrakashG
  • 1,642
  • 5
  • 20
  • 30
rakesh
  • 61
  • 1
  • 5
  • 1
    your should know what is regex first. – Kent Apr 17 '19 at 14:08
  • 1
    Possible duplicate of [Grep regular expression for digits in character string of variable length](https://stackoverflow.com/q/1863204/608639). And maybe questions like [Why doesn't \[01-12\] range work as expected?](https://stackoverflow.com/q/3148240/608639) – jww Apr 17 '19 at 15:50
  • 1
    Always singe quote strings and scripts unless you have a **NEED** to use double quotes or no quotes. Just quote your string `grep -r 'L0[1-9]auxillar' /pr/solder/` – Ed Morton Apr 17 '19 at 23:41

3 Answers3

1

If you don't want to use regexes within grep, you could do the number expansion in the shell:

grep -r --regexp=L0{1..9}auxillar /pr/solder/
Tomasz Noinski
  • 456
  • 2
  • 10
0

Just pass an -E for extended regex:

grep -rE '(L0[1-9]auxillar)' /pr/solder/
PrakashG
  • 1,642
  • 5
  • 20
  • 30
alecxs
  • 701
  • 8
  • 17
0

You could use a wildcard for the one character.

grep -rn '/home/user/temp' -e '\<asd.f\>'
# use operator w if you want to catch whole words
grep -rnw '/home/user/temp' -e '\<asd.f\>'

Output:
/home/user/temp/test:1:asd4f
/home/user/temp/test:2:asd5f

But it would also give matches with other chars (eg asd0f or asdgf).

JWo
  • 650
  • 1
  • 6
  • 23