Page 1 of 1

grep an exact number

Posted: Tue 09 Feb 2010, 12:52
by trio
Dear all,

Please help this dummy, I want to grep an exact number, how to do that? And yes, I am stupid.

Example:
in a file you have lines numbered from 1 to 100, you want to grep exactly line number 9, but I do:

Code: Select all

grep 9 /path/to/file.txt
I will get lines numbered 9, 19, 29, etc But I want only line number 9

Thanks

Posted: Tue 09 Feb 2010, 13:18
by dejan555
you can use sed:

Code: Select all

sed -n '9p' /path/to/file.txt

Posted: Tue 09 Feb 2010, 16:21
by seaside
Trio,

Also,

Code: Select all

grep -w 9 /path/to/file.txt
The "-w" matches a single word only

s

Posted: Wed 10 Feb 2010, 09:10
by Shel
If the number "9" is at the beginning of a line, you can check for that:

Code: Select all

grep '^9' filename
... of if it's followed with a space, you can check for that:

Code: Select all

grep '9 ' filename
or combine the two, etc. grep will do fairly complex regular expressions, so you can look for a "9" at the beginning of a line, followed by, say, a capital letter:

Code: Select all

grep '^9[A-Z]' filename
I think those are all correct, I'm not in a position to check them at the moment, but doco for grep is all over the 'net.

-Shel

Posted: Wed 10 Feb 2010, 09:26
by trio
Thx all...seaside's answer did the job