Is there a simple way to search a text file (source code) for all instances of a specific integer. This should not trigger on larger numbers that happen to include the integer as a sub-string, but it can't simply exclude such lines since they could contain both cases:

searching for '6'...

int a=6; // found
int b=16; // not found (despite the '6' in '16')
int c=6, d=16; // found

I'm really looking for a command-line approach to this, but am also curious if there is a FOSS GUI-type editor that will do it.

3 Answers 3


grep -E '\b6\b'

\b is a "word boundary"

Edit: After pointing @nobar in the right direction, he found/pointed out the shortcut-option -w (word-regexp) in the manpage, which simplifies the above to:

grep -w 6

If used a lot, you could use a function similar to

wgrp(){ grep -w "$1" "$2"; }

Note (to @glenn-jackman): If you don't quote "$2" here, you can use the function as a pipeline filter. But yes, then it won't work with filenames with spaces.

After reading yet another great answer from @Gilles, I now propose

igrp(){ grep -E "(^|[^0-9])$1($|[^0-9])" "$2"; }

  • Great! After seeing your answer, I searched the grep man page for "word" and found that I could alternatively use this option: -w, --word-regexp. So just grep -w "6". Commented Jun 22, 2016 at 20:43
  • @nobar ooh, nice. Even better. Thank you :) Commented Jun 22, 2016 at 20:44
  • Also quote "$2", for filenames with spaces. Commented Jun 23, 2016 at 1:42

You can search for a specific integer with a regular expression. Search for the digits preceded by either the beginning of the data or a non-digit character, and likewise after the digits.

grep -e '^6$' -e '^6[^0-9]' -e '[^0-9]6$' -e '[^0-9]6[^0-9]'   # BRE syntax
grep -E '(^|[^0-9])6($|[^0-9])'                                # ERE syntax

An entire-word search as suggested in other answers misses hits like foo6bar. (This may not be a problem for your use case, but it's what you asked.)

Any halfway serious text editor or viewer (e.g. less) has regular expression search. Check its documentation to see what regex syntax it uses.

  • Yep -- kind of building the whole-word option up from scratch (this roughly matches the description of the --word-regexp option in the grep man page). A nice option if you need to customize a bit, and don't mind getting your hands dirty. And you're right, I left some ambiguity in the question. Commented Jun 23, 2016 at 1:38

I guess this wasn't such an obscure need. My regular programming editor, pluma supports this via the "find" dialog using the option "Match entire word only". I'm sure many other editors have this as well -- although at the moment, apparently not pluma's ancestor, gedit.

enter image description here

  • Hilariously, there is a gedit plugin "Find in files" that has the option to "Match entire word only", but it appears to have a bug in which it only finds the opposite case -- '16', but not '6'. Commented Jun 22, 2016 at 21:09
  • What OS/Version has this bug? Commented Jun 22, 2016 at 21:47
  • @AlexStragies: Ubuntu MATE 16.04. Commented Jun 22, 2016 at 22:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .